Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix exit value #47

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commands/configdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions commands/configlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -94,6 +94,8 @@ func listSecrets(names, all bool) {
} else {
listInfo(os.Stdout, secrets, all)
}

return 0
}

func getConfigListCmd() *cobra.Command {
Expand All @@ -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)
},
}
)
Expand Down
15 changes: 9 additions & 6 deletions commands/configrename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])
},
}

Expand Down
5 changes: 4 additions & 1 deletion commands/configreset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -39,7 +40,9 @@ func getConfigResetCmd() *cobra.Command {
}
}

_ = configReset(collectionFile.filename)
if err := configReset(collectionFile.filename); err != nil {
exitVal = 1
}
},
}
)
Expand Down
14 changes: 8 additions & 6 deletions commands/configupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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 {
Expand All @@ -52,7 +54,7 @@ func getConfigUpdateCmd(rootCmd *cobra.Command) *cobra.Command {
return
}

updateSecret(args[0], args[1])
exitVal = updateSecret(args[0], args[1])
},
}

Expand Down
32 changes: 18 additions & 14 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ type runVars struct {
qr bool
}

var generateCodesService generateCodesAPI
var (
generateCodesService generateCodesAPI
exitVal int = 0
)

func getSecretNamesForCompletion(toComplete string) []string {
var (
Expand Down Expand Up @@ -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)

Expand All @@ -175,7 +176,7 @@ func run(cmd *cobra.Command, args []string, cfg runVars) {
fmt.Fprintln(os.Stderr, err)
}

return
return 1
}

if cfg.qr {
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion scripts/totp-test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

set -e
# set -e

TOTP=./totp-test
COLLECTION=testcollection.json
Expand Down