Skip to content

Commit

Permalink
validate arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjiangshu committed Sep 3, 2024
1 parent 678d8ba commit f97d05d
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions sda-admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit f97d05d

Please sign in to comment.