Skip to content

Commit

Permalink
config can be provided before command
Browse files Browse the repository at this point in the history
  • Loading branch information
pahatz committed Oct 16, 2024
1 parent b3ff28e commit 503a1ec
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
9 changes: 6 additions & 3 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions htsget/htsget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,23 @@ 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 {
prefix = Args.Args()[0]
}

// // 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)
}
Expand Down
30 changes: 21 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 503a1ec

Please sign in to comment.