From f97d05df7150721305c0af2599c0a9dbac30aef7 Mon Sep 17 00:00:00 2001 From: nanjiangshu Date: Tue, 3 Sep 2024 11:38:06 +0200 Subject: [PATCH] validate arguments --- sda-admin/main.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/sda-admin/main.go b/sda-admin/main.go index 4c6e7e38c..fd49d3e35 100644 --- a/sda-admin/main.go +++ b/sda-admin/main.go @@ -81,8 +81,7 @@ Options: } func fileUsage() { - usageText := `File Operations: - + usageText := ` Ingest a File: Usage: sda-admin file ingest --filepath FILEPATH --user USERNAME Trigger the ingestion of a given file for a specific user. @@ -127,8 +126,7 @@ Options: } func datasetUsage() { - usageText := `Dataset Operations: - + usageText := ` Create a Dataset: Usage: sda-admin dataset create --dataset-id DATASET_ID [ACCESSION_ID ...] Create a dataset from a list of accession IDs and the dataset ID. @@ -309,6 +307,13 @@ func main() { fileIngestCmd.StringVar(&filepath, "filepath", "", "Filepath to ingest") fileIngestCmd.StringVar(&username, "user", "", "Username to associate with the file") fileIngestCmd.Parse(flag.Args()[2:]) + + if filepath == "" || username == "" { + fmt.Println("Error: both --filepath and --user are required.") + fileIngestUsage() + os.Exit(1) + } + err := file.FileIngest(api_uri, token, username, filepath) if err != nil { fmt.Printf("Error: failed to ingest file, reason: %v\n", err) @@ -322,6 +327,13 @@ func main() { fileAccessionCmd.StringVar(&username, "user", "", "Username to associate with the file") fileAccessionCmd.StringVar(&accessionID, "accession-id", "", "Accession ID to assign") fileAccessionCmd.Parse(flag.Args()[2:]) + + if filepath == "" || username == "" || accessionID == "" { + fmt.Println("Error: --filepath, --user, and --accession-id are required.") + fileAccessionUsage() + os.Exit(1) + } + err := file.FileAccession(api_uri, token, username, filepath, accessionID) if err != nil { fmt.Printf("Error: failed to assign accession ID to file, reason: %v\n", err) @@ -346,6 +358,13 @@ func main() { datasetCreateCmd.StringVar(&datasetID, "dataset-id", "", "ID of the dataset to create") datasetCreateCmd.Parse(flag.Args()[2:]) accessionIDs := datasetCreateCmd.Args() // Args() returns the non-flag arguments after parsing + + if datasetID == "" || len(accessionIDs) == 0 { + fmt.Println("Error: --dataset-id and at least one accession ID are required.") + datasetCreateUsage() + os.Exit(1) + } + err := dataset.DatasetCreate(api_uri, token, datasetID, accessionIDs) if err != nil { fmt.Printf("Error: failed to create dataset, reason: %v\n", err) @@ -357,6 +376,13 @@ func main() { var datasetID string datasetReleaseCmd.StringVar(&datasetID, "dataset-id", "", "ID of the dataset to release") datasetReleaseCmd.Parse(flag.Args()[2:]) + + if datasetID == "" { + fmt.Println("Error: --dataset-id is required.") + datasetReleaseUsage() + os.Exit(1) + } + err := dataset.DatasetRelease(api_uri, token, datasetID) if err != nil { fmt.Printf("Error: failed to release dataset, reason: %v\n", err)