Skip to content

Commit

Permalink
chore: Use choiceFlag for path style
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 5, 2024
1 parent efa6076 commit 68fb67e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 119 deletions.
88 changes: 0 additions & 88 deletions internal/chezmoi/pathstyle.go

This file was deleted.

13 changes: 6 additions & 7 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,9 @@ var (
whitespaceRx = regexp.MustCompile(`\s+`)

commonFlagCompletionFuncs = map[string]func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective){
"exclude": chezmoi.EntryTypeSetFlagCompletionFunc,
"include": chezmoi.EntryTypeSetFlagCompletionFunc,
"path-style": chezmoi.PathStyleFlagCompletionFunc,
"secrets": severityFlagCompletionFunc,
"exclude": chezmoi.EntryTypeSetFlagCompletionFunc,
"include": chezmoi.EntryTypeSetFlagCompletionFunc,
"secrets": severityFlagCompletionFunc,
}
)

Expand Down Expand Up @@ -383,7 +382,7 @@ func newConfig(options ...configOption) (*Config, error) {
},
managed: managedCmdConfig{
filter: chezmoi.NewEntryTypeFilter(chezmoi.EntryTypesAll, chezmoi.EntryTypesNone),
pathStyle: chezmoi.PathStyleRelative,
pathStyle: newChoiceFlag("relative", []string{"absolute", "relative", "source-absolute", "source-relative"}),
},
mergeAll: mergeAllCmdConfig{
recursive: true,
Expand All @@ -404,7 +403,7 @@ func newConfig(options ...configOption) (*Config, error) {
},
},
unmanaged: unmanagedCmdConfig{
pathStyle: chezmoi.PathStyleSimple(chezmoi.PathStyleRelative),
pathStyle: newChoiceFlag("relative", []string{"absolute", "relative"}),
},

// Configuration.
Expand Down Expand Up @@ -2938,7 +2937,7 @@ func newConfigFile(bds *xdg.BaseDirectorySpecification) ConfigFile {
},
Status: statusCmdConfig{
Exclude: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesNone),
PathStyle: chezmoi.PathStyleRelative.Copy(),
PathStyle: newChoiceFlag("relative", []string{"absolute", "relative"}),
include: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesAll),
recursive: true,
},
Expand Down
15 changes: 8 additions & 7 deletions internal/cmd/managedcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type managedCmdConfig struct {
filter *chezmoi.EntryTypeFilter
pathStyle chezmoi.PathStyle
pathStyle *choiceFlag
tree bool
}

Expand All @@ -30,7 +30,8 @@ func (c *Config) newManagedCmd() *cobra.Command {

managedCmd.Flags().VarP(c.managed.filter.Exclude, "exclude", "x", "Exclude entry types")
managedCmd.Flags().VarP(c.managed.filter.Include, "include", "i", "Include entry types")
managedCmd.Flags().VarP(&c.managed.pathStyle, "path-style", "p", "Path style")
managedCmd.Flags().VarP(c.managed.pathStyle, "path-style", "p", "Path style")
must(managedCmd.RegisterFlagCompletionFunc("path-style", c.managed.pathStyle.FlagCompletionFunc()))
managedCmd.Flags().BoolVarP(&c.managed.tree, "tree", "t", c.managed.tree, "Print paths as a tree")

return managedCmd
Expand Down Expand Up @@ -80,14 +81,14 @@ func (c *Config) runManagedCmd(cmd *cobra.Command, args []string, sourceState *c
}

var path fmt.Stringer
switch c.managed.pathStyle {
case chezmoi.PathStyleAbsolute:
switch c.managed.pathStyle.String() {
case "absolute":
path = c.DestDirAbsPath.Join(targetRelPath)
case chezmoi.PathStyleRelative:
case "relative":
path = targetRelPath
case chezmoi.PathStyleSourceAbsolute:
case "source-absolute":
path = c.SourceDirAbsPath.Join(sourceStateEntry.SourceRelPath().RelPath())
case chezmoi.PathStyleSourceRelative:
case "source-relative":
path = sourceStateEntry.SourceRelPath().RelPath()
}
paths = append(paths, path)
Expand Down
16 changes: 7 additions & 9 deletions internal/cmd/statuscmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"fmt"
"io/fs"
"log/slog"
Expand All @@ -15,7 +14,7 @@ import (

type statusCmdConfig struct {
Exclude *chezmoi.EntryTypeSet `json:"exclude" mapstructure:"exclude" yaml:"exclude"`
PathStyle *chezmoi.PathStyle `json:"pathStyle" mapstructure:"pathStyle" yaml:"pathStyle"`
PathStyle *choiceFlag `json:"pathStyle" mapstructure:"pathStyle" yaml:"pathStyle"`
include *chezmoi.EntryTypeSet
init bool
parentDirs bool
Expand All @@ -39,6 +38,7 @@ func (c *Config) newStatusCmd() *cobra.Command {

statusCmd.Flags().VarP(c.Status.Exclude, "exclude", "x", "Exclude entry types")
statusCmd.Flags().VarP(c.Status.PathStyle, "path-style", "p", "Path style")
must(statusCmd.RegisterFlagCompletionFunc("path-style", c.Status.PathStyle.FlagCompletionFunc()))
statusCmd.Flags().VarP(c.Status.include, "include", "i", "Include entry types")
statusCmd.Flags().BoolVar(&c.Status.init, "init", c.Status.init, "Recreate config file from template")
statusCmd.Flags().
Expand Down Expand Up @@ -72,15 +72,13 @@ func (c *Config) runStatusCmd(cmd *cobra.Command, args []string) error {

if x != ' ' || y != ' ' {
var path string
switch *c.Status.PathStyle {
case chezmoi.PathStyleAbsolute:
switch pathStyle := c.Status.PathStyle.String(); pathStyle {
case "absolute":
path = c.DestDirAbsPath.Join(targetRelPath).String()
case chezmoi.PathStyleRelative:
case "relative":
path = targetRelPath.String()
case chezmoi.PathStyleSourceAbsolute:
return errors.New("source-absolute not supported for status")
case chezmoi.PathStyleSourceRelative:
return errors.New("source-relative not supported for status")
default:
return fmt.Errorf("%s: invalid path style", pathStyle)
}

fmt.Fprintf(&builder, "%c%c %s\n", x, y, path)
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/testdata/scripts/completion.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ cmp stdout golden/output-format-with-empty

# test that managed path style values are completed
exec chezmoi __complete managed --path-style=
cmp stdout golden/path-style
cmp stdout golden/path-style-with-source

# test that state data --format values are completed
exec chezmoi __complete state data --format=
Expand Down Expand Up @@ -137,6 +137,10 @@ yaml
-- golden/path-style --
absolute
relative
:4
-- golden/path-style-with-source --
absolute
relative
source-absolute
source-relative
:4
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/testdata/scripts/unmanaged.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ cmp stdout golden/unmanaged-with-some-managed

# test chezmoi unmanaged --path-style=source-absolute
! exec chezmoi unmanaged --path-style=source-absolute
stderr 'source-absolute: unknown path style'
stderr 'flag: invalid value'

-- golden/unmanaged --
.local
Expand Down
14 changes: 8 additions & 6 deletions internal/cmd/unmanagedcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type unmanagedCmdConfig struct {
pathStyle chezmoi.PathStyleSimple
pathStyle *choiceFlag
tree bool
}

Expand All @@ -29,11 +29,10 @@ func (c *Config) newUnmanagedCmd() *cobra.Command {
),
}

unmanagedCmd.Flags().VarP(&c.unmanaged.pathStyle, "path-style", "p", "Path style")
unmanagedCmd.Flags().VarP(c.unmanaged.pathStyle, "path-style", "p", "Path style")
must(unmanagedCmd.RegisterFlagCompletionFunc("path-style", c.unmanaged.pathStyle.FlagCompletionFunc()))
unmanagedCmd.Flags().BoolVarP(&c.unmanaged.tree, "tree", "t", c.unmanaged.tree, "Print paths as a tree")

must(unmanagedCmd.RegisterFlagCompletionFunc("path-style", chezmoi.PathStyleSimpleFlagCompletionFunc))

return unmanagedCmd
}

Expand Down Expand Up @@ -101,10 +100,13 @@ func (c *Config) runUnmanagedCmd(cmd *cobra.Command, args []string, sourceState
paths := make([]fmt.Stringer, 0, len(unmanagedRelPaths.Elements()))
for relPath := range unmanagedRelPaths {
var path fmt.Stringer
if c.unmanaged.pathStyle.ToPathStyle() == chezmoi.PathStyleAbsolute {
switch pathStyle := c.unmanaged.pathStyle.String(); pathStyle {
case "absolute":
path = c.DestDirAbsPath.Join(relPath)
} else {
case "relative":
path = relPath
default:
return fmt.Errorf("%s: invalid path style", pathStyle)
}
paths = append(paths, path)
}
Expand Down

0 comments on commit 68fb67e

Please sign in to comment.