diff --git a/commands/configdelete.go b/commands/configdelete.go index 59a7124..74ba5a7 100644 --- a/commands/configdelete.go +++ b/commands/configdelete.go @@ -12,21 +12,25 @@ func deleteSecret(name string) { s, err := collectionFile.loader() if err != nil { fmt.Fprintln(os.Stderr, "Error loading settings:", err) + exitVal = 1 return } if _, err := s.DeleteSecret(name); err != nil { fmt.Fprintln(os.Stderr, "Error deleting secret:", err) + exitVal = 1 return } if err := s.Save(); err != nil { fmt.Fprintln(os.Stderr, "Error saving settings:", err) + exitVal = 1 return } if _, err := printResultf("Deleted secret %s\n", name); err != nil { fmt.Fprintln(os.Stderr, err) + exitVal = 1 return } } @@ -43,6 +47,7 @@ func getConfigDeleteCmd() *cobra.Command { Run: func(_ *cobra.Command, args []string) { if len(args) != 1 { fmt.Fprintln(os.Stderr, "Must provide a secret name to delete.") + exitVal = 1 return } @@ -53,6 +58,7 @@ func getConfigDeleteCmd() *cobra.Command { fmt.Sprintf("This will delete secret %s.", secretName)) if err != nil { fmt.Fprintln(os.Stderr, "Error getting response:", err) + exitVal = 1 return } diff --git a/commands/configlist.go b/commands/configlist.go index fca32d5..294ef15 100644 --- a/commands/configlist.go +++ b/commands/configlist.go @@ -77,11 +77,11 @@ func listInfo(writer io.Writer, secrets []totp.Secret, all bool) { } } -func listSecrets(names, all bool) { +func listSecrets(names, all bool) int { c, err := collectionFile.loader() if err != nil { fmt.Fprintln(os.Stderr, "Error loading collection", err) - return + return 1 } secrets := c.GetSecrets() @@ -94,6 +94,8 @@ func listSecrets(names, all bool) { } else { listInfo(os.Stdout, secrets, all) } + + return 0 } func getConfigListCmd() *cobra.Command { @@ -108,9 +110,11 @@ func getConfigListCmd() *cobra.Command { Run: func(listCmd *cobra.Command, _ []string) { if names && all { fmt.Fprintln(os.Stderr, "Only one of --names or --all can be used.") + exitVal = 1 return } - listSecrets(names, all) + + exitVal = listSecrets(names, all) }, } ) diff --git a/commands/configrename.go b/commands/configrename.go index 23decb7..8633fb2 100644 --- a/commands/configrename.go +++ b/commands/configrename.go @@ -8,27 +8,29 @@ import ( "github.com/spf13/cobra" ) -func renameSecret(source, target string) { +func renameSecret(source, target string) int { if isReservedCommand(target) { fmt.Fprintln(os.Stderr, "The name \""+target+"\" is reserved for the "+target+" command") - return + return 1 } s, _ := collectionFile.loader() if _, err := s.RenameSecret(source, target); err != nil { fmt.Fprintln(os.Stderr, "Error renaming secret:", err) - return + return 1 } if err := s.Save(); err != nil { fmt.Fprintln(os.Stderr, "Error saving settings:", err) - return + return 1 } if _, err := printResultf("Renamed secret %s to %s\n", source, target); err != nil { fmt.Fprintln(os.Stderr, err) - return + return 1 } + + return 0 } func getConfigRenameCmd(rootCmd *cobra.Command) *cobra.Command { @@ -41,10 +43,11 @@ func getConfigRenameCmd(rootCmd *cobra.Command) *cobra.Command { Run: func(_ *cobra.Command, args []string) { if len(args) != 2 { fmt.Fprintln(os.Stderr, "Must provide source and target.") + exitVal = 1 return } - renameSecret(args[0], args[1]) + exitVal = renameSecret(args[0], args[1]) }, } diff --git a/commands/configreset.go b/commands/configreset.go index 59fc9ba..939ba38 100644 --- a/commands/configreset.go +++ b/commands/configreset.go @@ -30,6 +30,7 @@ func getConfigResetCmd() *cobra.Command { confirm, err := userConfirm(bufio.NewReader(os.Stdin), "This will remove all secrets.") if err != nil { fmt.Fprintln(os.Stderr, "Error getting response:", err) + exitVal = 1 return } @@ -39,7 +40,9 @@ func getConfigResetCmd() *cobra.Command { } } - _ = configReset(collectionFile.filename) + if err := configReset(collectionFile.filename); err != nil { + exitVal = 1 + } }, } ) diff --git a/commands/configupdate.go b/commands/configupdate.go index ed13f60..ea02a32 100644 --- a/commands/configupdate.go +++ b/commands/configupdate.go @@ -8,10 +8,10 @@ import ( "github.com/spf13/cobra" ) -func updateSecret(name, value string) { +func updateSecret(name, value string) int { if isReservedCommand(name) { fmt.Fprintln(os.Stderr, "The name \""+name+"\" is reserved for the "+name+" command") - return + return 1 } // ignore error because file may not exist @@ -20,12 +20,12 @@ func updateSecret(name, value string) { secret, err := s.UpdateSecret(name, value) if err != nil { fmt.Fprintln(os.Stderr, "Error updating secret:", err) - return + return 1 } if err := s.Save(); err != nil { fmt.Fprintln(os.Stderr, "Error saving settings:", err) - return + return 1 } action := "Updated" @@ -35,8 +35,10 @@ func updateSecret(name, value string) { if _, err := printResultf("%s secret %s\n", action, name); err != nil { fmt.Fprintln(os.Stderr, err) - return + return 1 } + + return 0 } func getConfigUpdateCmd(rootCmd *cobra.Command) *cobra.Command { @@ -52,7 +54,7 @@ func getConfigUpdateCmd(rootCmd *cobra.Command) *cobra.Command { return } - updateSecret(args[0], args[1]) + exitVal = updateSecret(args[0], args[1]) }, } diff --git a/commands/root.go b/commands/root.go index 08f2b83..730dd43 100644 --- a/commands/root.go +++ b/commands/root.go @@ -37,7 +37,10 @@ type runVars struct { qr bool } -var generateCodesService generateCodesAPI +var ( + generateCodesService generateCodesAPI + exitVal int = 0 +) func getSecretNamesForCompletion(toComplete string) []string { var ( @@ -150,9 +153,7 @@ func generateCodes(timeOffset time.Duration, durationToRun time.Duration, interv }) } -func run(cmd *cobra.Command, args []string, cfg runVars) { - // var err error - +func run(cmd *cobra.Command, args []string, cfg runVars) int { secretLen := len(cfg.secret) argsLen := len(args) @@ -175,7 +176,7 @@ func run(cmd *cobra.Command, args []string, cfg runVars) { fmt.Fprintln(os.Stderr, err) } - return + return 1 } if cfg.qr { @@ -184,8 +185,11 @@ func run(cmd *cobra.Command, args []string, cfg runVars) { secretName = args[0] } - _ = qrCode(os.Stdout, secretName, cfg.secret) - return + if err := qrCode(os.Stdout, secretName, cfg.secret); err != nil { + return 1 + } + + return 0 } // Override if time was given @@ -197,7 +201,7 @@ func run(cmd *cobra.Command, args []string, cfg runVars) { codeTime, err = time.Parse(time.RFC3339, cfg.timeString) if err != nil { fmt.Fprintln(os.Stderr, "Error parsing the time option:", err) - return + return 1 } } else { codeTime = time.Now() @@ -213,12 +217,14 @@ func run(cmd *cobra.Command, args []string, cfg runVars) { // If here then a stored shared secret is wanted if err := generateCode(os.Stdout, secretName, cfg.secret, codeTime.Add(cfg.forward-cfg.backward)); err != nil { // generateCode will output error text - return + return 1 } if cfg.follow { generateCodesService(time.Until(codeTime)-cfg.backward+cfg.forward, 0, 30*time.Second, time.Sleep, secretName, cfg.secret) } + + return 0 } func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { @@ -250,7 +256,7 @@ func getRootCmd() *cobra.Command { }, ValidArgsFunction: validArgs, Run: func(cmd *cobra.Command, args []string) { - run(cmd, args, cfg) + exitVal = run(cmd, args, cfg) }, } @@ -279,15 +285,13 @@ func getRootCmd() *cobra.Command { // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() int { - retVal := 0 - defaults() rootCmd := getRootCmd() if err := rootCmd.Execute(); err != nil { fmt.Println(err) - retVal = 1 + exitVal = 1 } - return retVal + return exitVal } diff --git a/scripts/totp-test.sh b/scripts/totp-test.sh index 3b2a4ae..5f9e408 100755 --- a/scripts/totp-test.sh +++ b/scripts/totp-test.sh @@ -1,6 +1,6 @@ #!/bin/bash -set -e +# set -e TOTP=./totp-test COLLECTION=testcollection.json