Skip to content

Commit

Permalink
fix: Fix panic in commands that do not use persistent state
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Oct 28, 2024
1 parent f514a7e commit c6f5787
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 14 deletions.
3 changes: 3 additions & 0 deletions internal/cmd/agecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (c *Config) newAgeCmd() *cobra.Command {
Use: "age",
Args: cobra.NoArgs,
Short: "Interact with age",
Annotations: newAnnotations(
persistentStateModeReadOnly,
),
}

ageDecryptCmd := &cobra.Command{
Expand Down
15 changes: 1 addition & 14 deletions internal/cmd/annotation.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"slices"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -39,17 +37,6 @@ type annotation interface {
type annotationsSet map[string]string

func getAnnotations(cmd *cobra.Command) annotationsSet {
thirdPartyCommandNames := []string{
"__complete",
}
if !slices.Contains(thirdPartyCommandNames, cmd.Name()) {
if cmd.Annotations == nil {
panic(cmd.Name() + ": no annotations")
}
if cmd.Annotations[string(persistentStateModeKey)] == "" {
panic(cmd.Name() + ": persistent state mode not set")
}
}
return annotationsSet(cmd.Annotations)
}

Expand All @@ -66,7 +53,7 @@ func (a annotationsSet) hasTag(tag annotation) bool {
}

func (a annotationsSet) persistentStateMode() persistentStateModeValue {
return persistentStateModeValue(a[string(persistentStateModeKey)])
return persistentStateModeValue(a[persistentStateModeKey.key()])
}

type persistentStateModeValue string
Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,16 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
}
}

for _, cmd := range rootCmd.Commands() {
annotations := getAnnotations(cmd)
if len(annotations) == 0 {
panic(cmd.Name() + ": no annotations")
}
if annotations.persistentStateMode() == "" {
panic(cmd.Name() + ": persistent state mode not set")
}
}

return rootCmd, nil
}

Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/internaltestcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func (c *Config) newInternalTestCmd() *cobra.Command {
Use: "internal-test",
Short: "Expose functionality for testing",
Hidden: true,
Annotations: newAnnotations(
persistentStateModeNone,
),
}

internalTestPromptBoolCmd := &cobra.Command{
Expand All @@ -23,6 +26,7 @@ func (c *Config) newInternalTestCmd() *cobra.Command {
RunE: c.runInternalTestPromptBoolCmd,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
internalTestCmd.AddCommand(internalTestPromptBoolCmd)
Expand All @@ -34,6 +38,7 @@ func (c *Config) newInternalTestCmd() *cobra.Command {
RunE: c.runInternalTestPromptChoiceCmd,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
internalTestCmd.AddCommand(internalTestPromptChoiceCmd)
Expand All @@ -45,6 +50,7 @@ func (c *Config) newInternalTestCmd() *cobra.Command {
RunE: c.runInternalTestPromptIntCmd,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
internalTestCmd.AddCommand(internalTestPromptIntCmd)
Expand All @@ -56,6 +62,7 @@ func (c *Config) newInternalTestCmd() *cobra.Command {
RunE: c.runInternalTestPromptStringCmd,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
internalTestCmd.AddCommand(internalTestPromptStringCmd)
Expand All @@ -67,6 +74,7 @@ func (c *Config) newInternalTestCmd() *cobra.Command {
RunE: c.runInternalTestReadPasswordCmd,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
internalTestCmd.AddCommand(internalTestReadPasswordCmd)
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/mackupcmd_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (c *Config) newMackupCmd() *cobra.Command {
Use: "mackup",
Short: "Interact with Mackup",
Hidden: true,
Annotations: newAnnotations(
persistentStateModeNone,
),
}

mackupAddCmd := &cobra.Command{
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/removecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (c *Config) newRemoveCmd() *cobra.Command {
Long: mustLongHelp("remove"),
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/secretcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func (c *Config) newSecretCmd() *cobra.Command {
Short: "Interact with a secret manager",
Long: mustLongHelp("secret"),
Example: example("secret"),
Annotations: newAnnotations(
persistentStateModeReadOnly,
),
}

if secretKeyringCmd := c.newSecretKeyringCmd(); secretKeyringCmd != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/secretkeyringcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (c *Config) newSecretKeyringCmd() *cobra.Command {
Use: "keyring",
Args: cobra.NoArgs,
Short: "Interact with keyring",
Annotations: newAnnotations(
persistentStateModeNone,
),
}

secretKeyringDeleteCmd := &cobra.Command{
Expand All @@ -43,6 +46,7 @@ func (c *Config) newSecretKeyringCmd() *cobra.Command {
RunE: c.runSecretKeyringDeleteCmdE,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
secretKeyringDeleteCmd.Flags().StringVar(&c.secret.keyring.delete.service, "service", "", "service")
Expand All @@ -57,6 +61,7 @@ func (c *Config) newSecretKeyringCmd() *cobra.Command {
RunE: c.runSecretKeyringGetCmdE,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
secretKeyringGetCmd.Flags().StringVar(&c.secret.keyring.get.service, "service", "", "service")
Expand All @@ -71,6 +76,7 @@ func (c *Config) newSecretKeyringCmd() *cobra.Command {
RunE: c.runSecretKeyringSetCmdE,
Annotations: newAnnotations(
doesNotRequireValidConfig,
persistentStateModeNone,
),
}
secretKeyringSetCmd.Flags().StringVar(&c.secret.keyring.set.service, "service", "", "service")
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/statecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (c *Config) newStateCmd() *cobra.Command {
Short: "Manipulate the persistent state",
Long: mustLongHelp("state"),
Example: example("state"),
Annotations: newAnnotations(
persistentStateModeNone,
),
}

stateDataCmd := &cobra.Command{
Expand Down

0 comments on commit c6f5787

Please sign in to comment.