From 456197a38b15b642a78bbec11ead3cc4ca0b30c0 Mon Sep 17 00:00:00 2001 From: consolethinks Date: Thu, 18 Jul 2024 13:38:58 +0200 Subject: [PATCH] add common flags that are shared between commands (user, token, config) --- cmd/commands/args_test.go | 2 +- cmd/commands/datasetArchiver.go | 2 -- cmd/commands/datasetCleaner.go | 2 -- cmd/commands/datasetGetProposal.go | 2 -- cmd/commands/datasetIngestor.go | 2 -- cmd/commands/datasetPublishData.go | 2 -- cmd/commands/datasetPublishDataRetrieve.go | 2 -- cmd/commands/datasetRetriever.go | 2 -- cmd/commands/flags_test.go | 6 ++++++ cmd/commands/root.go | 4 ++++ cmd/commands/waitForJobFinished.go | 2 -- 11 files changed, 11 insertions(+), 17 deletions(-) diff --git a/cmd/commands/args_test.go b/cmd/commands/args_test.go index 670eb47..6d50c6e 100644 --- a/cmd/commands/args_test.go +++ b/cmd/commands/args_test.go @@ -49,7 +49,7 @@ func TestArgs(t *testing.T) { //flag.CommandLine = flag.NewFlagSet(test.name, flag.ExitOnError) datasetUtils.TestArgs = func(args []interface{}) { passing := true - for i, _ := range test.expectedArgs { + for i := range test.expectedArgs { if test.expectedArgs[i] != args[i] { t.Logf("'%v' is not correct, expected: '%s'", args[i], test.expectedArgs[i]) passing = false diff --git a/cmd/commands/datasetArchiver.go b/cmd/commands/datasetArchiver.go index 756b47b..490e6fd 100644 --- a/cmd/commands/datasetArchiver.go +++ b/cmd/commands/datasetArchiver.go @@ -134,8 +134,6 @@ For further help see "` + MANUAL + `"`, func init() { rootCmd.AddCommand(datasetArchiverCmd) - datasetArchiverCmd.Flags().String("user", "", "Defines optional username and password") - datasetArchiverCmd.Flags().String("token", "", "Defines optional API token instead of username:password") datasetArchiverCmd.Flags().Int("tapecopies", 1, "Number of tapecopies to be used for archiving") datasetArchiverCmd.Flags().Bool("testenv", false, "Use test environment (qa) instead or production") datasetArchiverCmd.Flags().Bool("localenv", false, "Use local environment (local) instead or production") diff --git a/cmd/commands/datasetCleaner.go b/cmd/commands/datasetCleaner.go index 6644bf4..db95dba 100644 --- a/cmd/commands/datasetCleaner.go +++ b/cmd/commands/datasetCleaner.go @@ -118,8 +118,6 @@ func init() { datasetCleanerCmd.Flags().Bool("nonInteractive", false, "Defines if no questions will be asked, just do it - make sure you know what you are doing") datasetCleanerCmd.Flags().Bool("testenv", false, "Use test environment (qa) instead of production environment") datasetCleanerCmd.Flags().Bool("devenv", false, "Use development environment instead of production environment (developers only)") - datasetCleanerCmd.Flags().String("user", "", "Defines optional username:password string") - datasetCleanerCmd.Flags().String("token", "", "Defines optional API token instead of username:password") datasetCleanerCmd.Flags().Bool("version", false, "Show version number and exit") datasetCleanerCmd.MarkFlagsMutuallyExclusive("testenv", "devenv") diff --git a/cmd/commands/datasetGetProposal.go b/cmd/commands/datasetGetProposal.go index a93a39c..8ffc39c 100644 --- a/cmd/commands/datasetGetProposal.go +++ b/cmd/commands/datasetGetProposal.go @@ -104,8 +104,6 @@ For further help see "` + MANUAL + `"`, func init() { rootCmd.AddCommand(datasetGetProposalCmd) - datasetGetProposalCmd.Flags().String("user", "", "Defines optional username and password") - datasetGetProposalCmd.Flags().String("token", "", "Defines optional API token instead of username:password") datasetGetProposalCmd.Flags().String("field", "", "Defines optional field name , whose value should be returned instead of full information") datasetGetProposalCmd.Flags().Bool("testenv", false, "Use test environment (qa) instead or production") datasetGetProposalCmd.Flags().Bool("devenv", false, "Use development environment instead or production") diff --git a/cmd/commands/datasetIngestor.go b/cmd/commands/datasetIngestor.go index dc8f9d7..03d7cbd 100644 --- a/cmd/commands/datasetIngestor.go +++ b/cmd/commands/datasetIngestor.go @@ -392,8 +392,6 @@ func init() { datasetIngestorCmd.Flags().Bool("localenv", false, "Use local environment instead of production environment (developers only)") datasetIngestorCmd.Flags().Bool("tunnelenv", false, "Use tunneled API server at port 5443 to access development instance (developers only)") datasetIngestorCmd.Flags().Bool("noninteractive", false, "If set no questions will be asked and the default settings for all undefined flags will be assumed") - datasetIngestorCmd.Flags().String("user", "", "Defines optional username:password string. This can be used both for access to the data catalog API and for access to the intermediate storage server for the decentral use case") - datasetIngestorCmd.Flags().String("token", "", "Defines API token for access to the data catalog API. It is now mandatory for normal user accounts, but optional for functional accounts. It takes precedence over username/pw.") datasetIngestorCmd.Flags().Bool("copy", false, "Defines if files should be copied from your local system to a central server before ingest (i.e. your data is not centrally available and therefore needs to be copied ='decentral' case). copyFlag has higher priority than nocopyFlag. If neither flag is defined the tool will try to make the best guess.") datasetIngestorCmd.Flags().Bool("nocopy", false, "Defines if files should *not* be copied from your local system to a central server before ingest (i.e. your data is centrally available and therefore does not need to be copied ='central' case).") datasetIngestorCmd.Flags().Int("tapecopies", 0, "Number of tapecopies to be used for archiving") diff --git a/cmd/commands/datasetPublishData.go b/cmd/commands/datasetPublishData.go index 8733188..b78ce12 100644 --- a/cmd/commands/datasetPublishData.go +++ b/cmd/commands/datasetPublishData.go @@ -335,8 +335,6 @@ func init() { datasetPublishDataCmd.Flags().String("publisheddata", "", "Defines to publish data froma given publishedData document ID") // datasetPublishDataCmd.Flags().String("dataset", "", "Defines single datasetId to publish") // datasetPublishDataCmd.Flags().String("ownergroup", "", "Defines to publish only datasets of the specified ownerGroup") - datasetPublishDataCmd.Flags().String("user", "", "Defines optional username:password string") - datasetPublishDataCmd.Flags().String("token", "", "Defines optional API token instead of username:password") datasetPublishDataCmd.Flags().Bool("testenv", false, "Use test environment (qa) (default is to use production system)") datasetPublishDataCmd.Flags().Bool("devenv", false, "Use development environment (default is to use production system)") datasetPublishDataCmd.Flags().Bool("version", false, "Show version number and exit") diff --git a/cmd/commands/datasetPublishDataRetrieve.go b/cmd/commands/datasetPublishDataRetrieve.go index a8a458d..bce71f2 100644 --- a/cmd/commands/datasetPublishDataRetrieve.go +++ b/cmd/commands/datasetPublishDataRetrieve.go @@ -120,8 +120,6 @@ func init() { datasetPublishDataRetrieveCmd.Flags().Bool("retrieve", false, "Defines if this command is meant to actually retrieve data (default: retrieve actions are only displayed)") datasetPublishDataRetrieveCmd.Flags().String("publisheddata", "", "Defines to publish data from a given publishedData document ID") - datasetPublishDataRetrieveCmd.Flags().String("user", "", "Defines optional username:password string") - datasetPublishDataRetrieveCmd.Flags().String("token", "", "Defines optional API token instead of username:password") datasetPublishDataRetrieveCmd.Flags().Bool("testenv", false, "Use test environment (qa) (default is to use production system)") datasetPublishDataRetrieveCmd.Flags().Bool("devenv", false, "Use development environment (default is to use production system)") datasetPublishDataRetrieveCmd.Flags().Bool("version", false, "Show version number and exit") diff --git a/cmd/commands/datasetRetriever.go b/cmd/commands/datasetRetriever.go index bc9c257..4f51c66 100644 --- a/cmd/commands/datasetRetriever.go +++ b/cmd/commands/datasetRetriever.go @@ -211,8 +211,6 @@ func init() { rootCmd.AddCommand(datasetRetrieverCmd) datasetRetrieverCmd.Flags().Bool("retrieve", false, "Defines if this command is meant to actually copy data to the local system (default nothing is done)") - datasetRetrieverCmd.Flags().String("user", "", "Defines optional username and password (default is to prompt for username and password)") - datasetRetrieverCmd.Flags().String("token", "", "Defines optional API token instead of username:password") datasetRetrieverCmd.Flags().Bool("nochksum", false, "Switch off chksum verification step (default checksum tests are done)") datasetRetrieverCmd.Flags().String("dataset", "", "Defines single dataset to retrieve (default all available datasets)") datasetRetrieverCmd.Flags().String("ownergroup", "", "Defines to fetch only datasets of the specified ownerGroup (default is to fetch all available datasets)") diff --git a/cmd/commands/flags_test.go b/cmd/commands/flags_test.go index 7556645..6208850 100644 --- a/cmd/commands/flags_test.go +++ b/cmd/commands/flags_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/paulscherrerinstitute/scicat/datasetUtils" + "github.com/spf13/pflag" ) func TestMainFlags(t *testing.T) { @@ -380,6 +381,11 @@ func TestMainFlags(t *testing.T) { } } + rootCmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) { + flag.Value.Set(flag.DefValue) + flag.Changed = false + }) + rootCmd.SetArgs(test.args) Execute() }) diff --git a/cmd/commands/root.go b/cmd/commands/root.go index aa9beb3..1f4f822 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -25,4 +25,8 @@ func Execute() { func init() { rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + rootCmd.PersistentFlags().StringP("user", "u", "", "Defines optional username:password string") + rootCmd.PersistentFlags().String("token", "", "Defines optional API token instead of username:password") + rootCmd.PersistentFlags().StringP("config", "c", "", "A path to a config file for connecting to SciCat and transfer services") } diff --git a/cmd/commands/waitForJobFinished.go b/cmd/commands/waitForJobFinished.go index 59a1567..f388b2d 100644 --- a/cmd/commands/waitForJobFinished.go +++ b/cmd/commands/waitForJobFinished.go @@ -168,8 +168,6 @@ var waitForJobFinishedCmd = &cobra.Command{ func init() { rootCmd.AddCommand(waitForJobFinishedCmd) - waitForJobFinishedCmd.Flags().String("user", "", "Defines optional username and password") - waitForJobFinishedCmd.Flags().String("token", "", "Defines optional API token instead of username:password") waitForJobFinishedCmd.Flags().String("job", "", "Defines the job id to poll") waitForJobFinishedCmd.Flags().Bool("testenv", false, "Use test environment (qa) instead or production") waitForJobFinishedCmd.Flags().Bool("devenv", false, "Use development environment instead or production")