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

Warn/fail if the wrong number of arguments is provided #1342

Merged
merged 2 commits into from
Dec 16, 2023
Merged
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
28 changes: 26 additions & 2 deletions cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ func init() {
log = logging.NewLogger("CMD")
}

func warnMoreThanOnePositionalArgument(c *cli.Context) {
if c.NArg() > 1 {
log.Warn("More than one positional argument provided. Only the first one will be used!")
potentialFlag := ""
for i, value := range c.Args() {
if i > 0 && strings.HasPrefix(value, "-") {
potentialFlag = value
}
}
if potentialFlag != "" {
log.Warn(fmt.Sprintf("Note that one of the ignored positional argument is %q, which looks like a flag. Flags must always be provided before the first positional argument!", potentialFlag))
}
}
}

func main() {
cli.VersionPrinter = version.PrintVersion
app := cli.NewApp()
Expand Down Expand Up @@ -147,7 +162,7 @@ func main() {
},
}, keyserviceFlags...),
Action: func(c *cli.Context) error {
if len(c.Args()) != 2 {
if c.NArg() != 2 {
return common.NewExitError(fmt.Errorf("error: missing file to decrypt"), codes.ErrorGeneric)
}

Expand Down Expand Up @@ -215,7 +230,7 @@ func main() {
},
}, keyserviceFlags...),
Action: func(c *cli.Context) error {
if len(c.Args()) != 2 {
if c.NArg() != 2 {
return common.NewExitError(fmt.Errorf("error: missing file to decrypt"), codes.ErrorGeneric)
}

Expand Down Expand Up @@ -292,6 +307,7 @@ func main() {
if c.NArg() < 1 {
return common.NewExitError("Error: no file specified", codes.NoFileSpecified)
}
warnMoreThanOnePositionalArgument(c)
path := c.Args()[0]
info, err := os.Stat(path)
if err != nil {
Expand Down Expand Up @@ -429,6 +445,9 @@ func main() {
vaultURIs := c.StringSlice("hc-vault-transit")
azkvs := c.StringSlice("azure-kv")
ageRecipients := c.StringSlice("age")
if c.NArg() != 0 {
return common.NewExitError(fmt.Errorf("error: no positional arguments allowed"), codes.ErrorGeneric)
}
var group sops.KeyGroup
for _, fp := range pgpFps {
group = append(group, pgp.NewMasterKeyFromFingerprint(fp))
Expand Down Expand Up @@ -494,7 +513,11 @@ func main() {
},
}, keyserviceFlags...),
ArgsUsage: `[index]`,

Action: func(c *cli.Context) error {
if c.NArg() != 1 {
return common.NewExitError(fmt.Errorf("error: exactly one positional argument (index) required"), codes.ErrorGeneric)
}
group, err := strconv.ParseUint(c.Args().First(), 10, 32)
if err != nil {
return fmt.Errorf("failed to parse [index] argument: %s", err)
Expand Down Expand Up @@ -755,6 +778,7 @@ func main() {
if c.NArg() < 1 {
return common.NewExitError("Error: no file specified", codes.NoFileSpecified)
}
warnMoreThanOnePositionalArgument(c)
if c.Bool("in-place") && c.String("output") != "" {
return common.NewExitError("Error: cannot operate on both --output and --in-place", codes.ErrorConflictingParameters)
}
Expand Down
Loading