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 @@
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

Check warning on line 27 in commands/configdelete.go

View check run for this annotation

Codecov / codecov/patch

commands/configdelete.go#L27

Added line #L27 was not covered by tests
return
}

if _, err := printResultf("Deleted secret %s\n", name); err != nil {
fmt.Fprintln(os.Stderr, err)
exitVal = 1

Check warning on line 33 in commands/configdelete.go

View check run for this annotation

Codecov / codecov/patch

commands/configdelete.go#L33

Added line #L33 was not covered by tests
return
}
}
Expand All @@ -43,6 +47,7 @@
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 @@
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 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

Check warning on line 84 in commands/configlist.go

View check run for this annotation

Codecov / codecov/patch

commands/configlist.go#L84

Added line #L84 was not covered by tests
}

secrets := c.GetSecrets()
Expand All @@ -94,6 +94,8 @@
} else {
listInfo(os.Stdout, secrets, all)
}

return 0
}

func getConfigListCmd() *cobra.Command {
Expand All @@ -108,9 +110,11 @@
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 @@
"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

Check warning on line 25 in commands/configrename.go

View check run for this annotation

Codecov / codecov/patch

commands/configrename.go#L25

Added line #L25 was not covered by tests
}

if _, err := printResultf("Renamed secret %s to %s\n", source, target); err != nil {
fmt.Fprintln(os.Stderr, err)
return
return 1

Check warning on line 30 in commands/configrename.go

View check run for this annotation

Codecov / codecov/patch

commands/configrename.go#L30

Added line #L30 was not covered by tests
}

return 0
}

func getConfigRenameCmd(rootCmd *cobra.Command) *cobra.Command {
Expand All @@ -41,10 +43,11 @@
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 @@
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 @@
}
}

_ = configReset(collectionFile.filename)
if err := configReset(collectionFile.filename); err != nil {
exitVal = 1
}

Check warning on line 45 in commands/configreset.go

View check run for this annotation

Codecov / codecov/patch

commands/configreset.go#L44-L45

Added lines #L44 - L45 were not covered by tests
},
}
)
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 @@
"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 @@
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

Check warning on line 28 in commands/configupdate.go

View check run for this annotation

Codecov / codecov/patch

commands/configupdate.go#L28

Added line #L28 was not covered by tests
}

action := "Updated"
Expand All @@ -35,8 +35,10 @@

if _, err := printResultf("%s secret %s\n", action, name); err != nil {
fmt.Fprintln(os.Stderr, err)
return
return 1

Check warning on line 38 in commands/configupdate.go

View check run for this annotation

Codecov / codecov/patch

commands/configupdate.go#L38

Added line #L38 was not covered by tests
}

return 0
}

func getConfigUpdateCmd(rootCmd *cobra.Command) *cobra.Command {
Expand All @@ -52,7 +54,7 @@
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 @@
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 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 @@
fmt.Fprintln(os.Stderr, err)
}

return
return 1
}

if cfg.qr {
Expand All @@ -184,8 +185,11 @@
secretName = args[0]
}

_ = qrCode(os.Stdout, secretName, cfg.secret)
return
if err := qrCode(os.Stdout, secretName, cfg.secret); err != nil {
return 1
}

Check warning on line 190 in commands/root.go

View check run for this annotation

Codecov / codecov/patch

commands/root.go#L189-L190

Added lines #L189 - L190 were not covered by tests

return 0
}

// Override if time was given
Expand All @@ -197,7 +201,7 @@
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 @@
// 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 @@
},
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 @@
// 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

Check warning on line 293 in commands/root.go

View check run for this annotation

Codecov / codecov/patch

commands/root.go#L293

Added line #L293 was not covered by tests
}

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
Loading