Skip to content

Commit

Permalink
feat: Add state get-bucket command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 28, 2021
1 parent 988fcdc commit ff1e81d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,7 @@ $ chezmoi state delete --bucket=bucket --key=key
$ chezmoi state delete-bucket --bucket=bucket
$ chezmoi state dump
$ chezmoi state get --bucket=bucket --key=key
$ chezmoi state get-bucket --bucket=bucket
$ chezmoi state set --bucket=bucket --key=key --value=value
$ chezmoi state reset
```
Expand Down
38 changes: 19 additions & 19 deletions internal/chezmoi/persistentstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,33 @@ type PersistentState interface {
Set(bucket, key, value []byte) error
}

// PersistentStateBucketData returns the state data in bucket in s.
func PersistentStateBucketData(s PersistentState, bucket []byte) (map[string]interface{}, error) {
result := make(map[string]interface{})
if err := s.ForEach(bucket, func(k, v []byte) error {
var value map[string]interface{}
if err := stateFormat.Unmarshal(v, &value); err != nil {
return err
}
result[string(k)] = value
return nil
}); err != nil {
return nil, err
}
return result, nil
}

// PersistentStateData returns the structured data in s.
func PersistentStateData(s PersistentState) (interface{}, error) {
configStateData, err := persistentStateBucketData(s, ConfigStateBucket)
configStateData, err := PersistentStateBucketData(s, ConfigStateBucket)
if err != nil {
return nil, err
}
entryStateData, err := persistentStateBucketData(s, EntryStateBucket)
entryStateData, err := PersistentStateBucketData(s, EntryStateBucket)
if err != nil {
return nil, err
}
scriptStateData, err := persistentStateBucketData(s, scriptStateBucket)
scriptStateData, err := PersistentStateBucketData(s, scriptStateBucket)
if err != nil {
return nil, err
}
Expand All @@ -51,22 +67,6 @@ func PersistentStateData(s PersistentState) (interface{}, error) {
}, nil
}

// persistentStateBucketData returns the state data in bucket in s.
func persistentStateBucketData(s PersistentState, bucket []byte) (map[string]interface{}, error) {
result := make(map[string]interface{})
if err := s.ForEach(bucket, func(k, v []byte) error {
var value map[string]interface{}
if err := stateFormat.Unmarshal(v, &value); err != nil {
return err
}
result[string(k)] = value
return nil
}); err != nil {
return nil, err
}
return result, nil
}

// persistentStateGet gets the value associated with key in bucket in s, if it exists.
func persistentStateGet(s PersistentState, bucket, key []byte, value interface{}) (bool, error) {
data, err := s.Get(bucket, key)
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ func newConfig(options ...configOption) (*Config, error) {
dump: stateDumpCmdConfig{
format: defaultWriteDataFormat,
},
getBucket: stateGetBucketCmdConfig{
format: defaultWriteDataFormat,
},
},
status: statusCmdConfig{
exclude: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesNone),
Expand Down
28 changes: 28 additions & 0 deletions internal/cmd/statecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type stateCmdConfig struct {
deleteBucket stateDeleteBucketCmdConfig
dump stateDumpCmdConfig
get stateGetCmdConfig
getBucket stateGetBucketCmdConfig
set stateSetCmdConfig
}

Expand All @@ -41,6 +42,11 @@ type stateGetCmdConfig struct {
key string
}

type stateGetBucketCmdConfig struct {
bucket string
format writeDataFormat
}

type stateSetCmdConfig struct {
bucket string
key string
Expand Down Expand Up @@ -124,6 +130,20 @@ func (c *Config) newStateCmd() *cobra.Command {
stateGetPersistentFlags.StringVar(&c.state.get.key, "key", c.state.get.key, "key")
stateCmd.AddCommand(stateGetCmd)

stateGetBucketCmd := &cobra.Command{
Use: "get-bucket",
Short: "Get a bucket from the persistent state",
Args: cobra.NoArgs,
RunE: c.runStateGetBucketCmd,
Annotations: map[string]string{
persistentStateMode: persistentStateModeReadOnly,
},
}
stateGetBucketPersistentFlags := stateGetBucketCmd.PersistentFlags()
stateGetBucketPersistentFlags.StringVar(&c.state.getBucket.bucket, "bucket", c.state.getBucket.bucket, "bucket")
stateGetBucketPersistentFlags.VarP(&c.state.getBucket.format, "format", "f", "format")
stateCmd.AddCommand(stateGetBucketCmd)

stateResetCmd := &cobra.Command{
Use: "reset",
Short: "Reset the persistent state",
Expand Down Expand Up @@ -185,6 +205,14 @@ func (c *Config) runStateGetCmd(cmd *cobra.Command, args []string) error {
return c.writeOutput(value)
}

func (c *Config) runStateGetBucketCmd(cmd *cobra.Command, args []string) error {
data, err := chezmoi.PersistentStateBucketData(c.persistentState, []byte(c.state.getBucket.bucket))
if err != nil {
return err
}
return c.marshal(c.state.getBucket.format, data)
}

func (c *Config) runStateResetCmd(cmd *cobra.Command, args []string) error {
persistentStateFileAbsPath, err := c.persistentStateFile()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/testdata/scripts/state_unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ chezmoi state dump --format=yaml
stdout 70396a619400b7f78dbb83ab8ddb76ffe0b8e31557e64bab2ca9677818a52135:
stdout runAt:

# test that chezmoi get-bucket gets a bucket
chezmoi state get-bucket --bucket=scriptState
stdout "runAt":

# test that chezmoi delete-bucket deletes a bucket
chezmoi state delete-bucket --bucket=scriptState
chezmoi state dump --format=yaml
Expand Down

0 comments on commit ff1e81d

Please sign in to comment.