From 503a1ecbbc2e4f91f961f8ba69c6c1464be357e6 Mon Sep 17 00:00:00 2001 From: Panos Chatzopoulos Date: Thu, 17 Oct 2024 00:55:10 +0200 Subject: [PATCH] config can be provided before command --- download/download.go | 9 ++++++--- htsget/htsget.go | 7 +++++-- list/list.go | 7 +++++-- main.go | 30 +++++++++++++++++++++--------- upload/upload.go | 7 +++++-- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/download/download.go b/download/download.go index 5c629773..14e1819f 100644 --- a/download/download.go +++ b/download/download.go @@ -96,14 +96,17 @@ type File struct { // Download function downloads files from the SDA by using the // download's service APIs -func Download(args []string) error { +func Download(args []string, configPathF string) error { // Call ParseArgs to take care of all the flag parsing err := helpers.ParseArgs(args, Args) if err != nil { return fmt.Errorf("failed parsing arguments, reason: %v", err) } + if configPathF == "" { + configPathF = *configPath + } - if *datasetID == "" || *URL == "" || *configPath == "" { + if *datasetID == "" || *URL == "" || configPathF == "" { return fmt.Errorf("missing required arguments, dataset, config and url are required") } @@ -136,7 +139,7 @@ func Download(args []string) error { } // Get the configuration file or the .sda-cli-session - config, err := helpers.GetAuth(*configPath) + config, err := helpers.GetAuth(configPathF) if err != nil { return err } diff --git a/htsget/htsget.go b/htsget/htsget.go index 7fbf6ad0..60b1a621 100644 --- a/htsget/htsget.go +++ b/htsget/htsget.go @@ -73,19 +73,22 @@ type htsgetResponse struct { // Htsget function downloads the files included in the urls_list.txt file. // The argument can be a local file or a url to an S3 folder -func Htsget(args []string) error { +func Htsget(args []string, configPathF string) error { // Call ParseArgs to take care of all the flag parsing err := helpers.ParseArgs(args, Args) if err != nil { return fmt.Errorf("failed parsing arguments, reason: %v", err) } + if configPathF == "" { + configPathF = *configPath + } if *datasetID == "" || *fileName == "" || *htsgetHost == "" || *publicKeyFile == "" { return fmt.Errorf("missing required arguments, dataset, filename, host and key are required") } - config, err := helpers.GetAuth(*configPath) + config, err := helpers.GetAuth(configPathF) if err != nil { return err } diff --git a/list/list.go b/list/list.go index feecdbe7..f2275959 100644 --- a/list/list.go +++ b/list/list.go @@ -52,12 +52,15 @@ var bytesFormat = Args.Bool("bytes", false, "Print file sizes in bytes (not huma var dataset = Args.String("dataset", "", "List all files in the specified dataset.") // List function lists the contents of an s3 -func List(args []string) error { +func List(args []string, configPathF string) error { // Call ParseArgs to take care of all the flag parsing err := helpers.ParseArgs(args, Args) if err != nil { return fmt.Errorf("failed parsing arguments, reason: %v", err) } + if configPathF == "" { + configPathF = *configPath + } prefix := "" if len(Args.Args()) == 1 { @@ -65,7 +68,7 @@ func List(args []string) error { } // // Get the configuration file or the .sda-cli-session - config, err := helpers.GetAuth(*configPath) + config, err := helpers.GetAuth(configPathF) if err != nil { return fmt.Errorf("failed to load config file, reason: %v", err) } diff --git a/main.go b/main.go index dff4c4ae..0a23e621 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ var Commands = map[string]commandInfo{ func main() { log.SetLevel(log.WarnLevel) - command, args := ParseArgs() + command, args, configPath := ParseArgs() var err error @@ -61,15 +61,15 @@ func main() { case "decrypt": err = decrypt.Decrypt(args) case "upload": - err = upload.Upload(args) + err = upload.Upload(args, configPath) case "list": - err = list.List(args) + err = list.List(args, configPath) case "htsget": - err = htsget.Htsget(args) + err = htsget.Htsget(args, configPath) case "login": err = login.NewLogin(args) case "download": - err = download.Download(args) + err = download.Download(args, configPath) case "version": err = version.Version(Version) default: @@ -84,7 +84,8 @@ func main() { // Parses the command line arguments into a command, and keep the rest // of the arguments for the subcommand. -func ParseArgs() (string, []string) { +func ParseArgs() (string, []string, string) { + var configPath string // Print usage if no arguments are provided. if len(os.Args) < 2 { _ = Help("help") @@ -98,7 +99,18 @@ func ParseArgs() (string, []string) { os.Exit(0) } - return "version", os.Args + return "version", os.Args, "" + } + + // If config comes before the command, we remove it from the + // arguments and set the global config path. + if len(os.Args) > 1 && (os.Args[1] == "--config" || os.Args[1] == "-config") { + if len(os.Args) < 3 { + fmt.Fprintf(os.Stderr, "Error: --config requires an argument\n") + os.Exit(1) + } + configPath = os.Args[2] + os.Args = append(os.Args[:1], os.Args[3:]...) } // Extract the command from the 1st argument, then remove it @@ -130,7 +142,7 @@ func ParseArgs() (string, []string) { // The "list" command can have no arguments since it can use the // config from login so we immediately return in that case. if command == "list" { - return command, os.Args + return command, os.Args, configPath } // If no arguments are provided to the subcommand, it's not @@ -141,7 +153,7 @@ func ParseArgs() (string, []string) { os.Exit(1) } - return command, os.Args + return command, os.Args, configPath } // Prints the main usage string, and the global help or command help diff --git a/upload/upload.go b/upload/upload.go index 5f465d12..b4c5f806 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -256,7 +256,7 @@ func createFilePaths(dirPath string) ([]string, []string, error) { // Upload function uploads files to the s3 bucket. Input can be files or // directories to be uploaded recursively -func Upload(args []string) error { +func Upload(args []string, configPathF string) error { var files []string var outFiles []string *pubKeyPath = "" @@ -267,6 +267,9 @@ func Upload(args []string) error { if err != nil { return fmt.Errorf("failed parsing arguments, reason: %v", err) } + if configPathF == "" { + configPathF = *configPath + } // Dereference the pointer to a string var targetDirString string @@ -288,7 +291,7 @@ func Upload(args []string) error { } // Get the configuration file or the .sda-cli-session - config, err := helpers.GetAuth(*configPath) + config, err := helpers.GetAuth(configPathF) if err != nil { return err }