@@ -2,22 +2,25 @@ package cliflags
22
33import (
44 "fmt"
5+ "github.com/forceu/gokapi/cmd/cli-uploader/cliconstants"
56 "github.com/forceu/gokapi/internal/environment"
67 "os"
78 "path/filepath"
89 "strconv"
910)
1011
1112const (
13+ // ModeLogin is the mode for the login command
1214 ModeLogin = iota
15+ // ModeLogout is the mode for the logout command
1316 ModeLogout
17+ // ModeUpload is the mode for the upload command
1418 ModeUpload
19+ // ModeInvalid is the mode for an invalid command
1520 ModeInvalid
1621)
1722
18- var dockerConfigFile string
19- var dockerUploadFolder string
20-
23+ // UploadConfig contains the parameters for the upload command.
2124type UploadConfig struct {
2225 File string
2326 JsonOutput bool
@@ -27,11 +30,7 @@ type UploadConfig struct {
2730 Password string
2831}
2932
30- func Init (dockerConfigPath , dockerUploadPath string ) {
31- dockerConfigFile = dockerConfigPath
32- dockerUploadFolder = dockerUploadPath
33- }
34-
33+ // Parse parses the command line arguments and returns the mode.
3534func Parse () int {
3635 if len (os .Args ) < 2 {
3736 printUsage (3 )
@@ -52,6 +51,7 @@ func Parse() int {
5251 return ModeInvalid
5352}
5453
54+ // GetUploadParameters parses the command line arguments and returns the parameters for the upload command.
5555func GetUploadParameters () UploadConfig {
5656 result := UploadConfig {}
5757 for i := 2 ; i < len (os .Args ); i ++ {
@@ -90,7 +90,7 @@ func GetUploadParameters() UploadConfig {
9090 if environment .IsDockerInstance () {
9191 ok , dockerFile := getDockerUpload ()
9292 if ! ok {
93- fmt .Println ("ERROR: Missing parameter --file and no file or more than one file found in " + dockerUploadFolder )
93+ fmt .Println ("ERROR: Missing parameter --file and no file or more than one file found in " + cliconstants . DockerFolderUpload )
9494 os .Exit (2 )
9595 }
9696 result .File = dockerFile
@@ -112,7 +112,7 @@ func getDockerUpload() (bool, string) {
112112 if ! environment .IsDockerInstance () {
113113 return false , ""
114114 }
115- entries , err := os .ReadDir (dockerUploadFolder )
115+ entries , err := os .ReadDir (cliconstants . DockerFolderUpload )
116116 if err != nil {
117117 return false , ""
118118 }
@@ -132,22 +132,23 @@ func getDockerUpload() (bool, string) {
132132 if ! fileFound {
133133 return false , ""
134134 }
135- return true , filepath .Join (dockerUploadFolder , fileName )
135+ return true , filepath .Join (cliconstants . DockerFolderUpload , fileName )
136136}
137137
138- func GetConfigLocation () string {
139- if environment .IsDockerInstance () {
140- return dockerConfigFile
141- }
138+ // GetConfigLocation returns the path to the configuration file. Returns true if the default file is used
139+ func GetConfigLocation () (string , bool ) {
142140 for i := 2 ; i < len (os .Args ); i ++ {
143141 switch os .Args [i ] {
144142 case "-c" :
145143 fallthrough
146144 case "--configuration" :
147- return getParameter (& i )
145+ return getParameter (& i ), false
148146 }
149147 }
150- return "gokapi-cli.json"
148+ if environment .IsDockerInstance () {
149+ return cliconstants .DockerFolderConfigFile , true
150+ }
151+ return cliconstants .DefaultConfigFileName , true
151152}
152153
153154func getParameter (position * int ) string {
@@ -183,9 +184,7 @@ func printUsage(exitCode int) {
183184
184185 fmt .Println ("Options:" )
185186 fmt .Println (" -f, --file <path> File to upload" )
186- if ! environment .IsDockerInstance () {
187- fmt .Println (" -c, --configuration <path> Path to configuration file (default: gokapi-cli.json)" )
188- }
187+ fmt .Println (" -c, --configuration <path> Path to configuration file (default: gokapi-cli.json)" )
189188 fmt .Println (" -j, --json Output the result in JSON only" )
190189 fmt .Println (" -n, --disable-e2e Disable end-to-end encryption" )
191190 fmt .Println (" -e, --expiry-days <int> Set file expiry in days (default: unlimited)" )
0 commit comments