Skip to content

Commit

Permalink
Factor out AddOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 5, 2019
1 parent 5004108 commit 2abc9a9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
16 changes: 7 additions & 9 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

Expand All @@ -16,22 +17,19 @@ var addCommand = &cobra.Command{
RunE: makeRunE(config.runAddCommand),
}

// An AddCommandConfig is a configuration for the add command.
type addCommandConfig struct {
empty bool
exact bool
recursive bool
template bool
options chezmoi.AddOptions
}

func init() {
rootCommand.AddCommand(addCommand)

persistentFlags := addCommand.PersistentFlags()
persistentFlags.BoolVarP(&config.add.empty, "empty", "e", false, "add empty files")
persistentFlags.BoolVarP(&config.add.exact, "exact", "x", false, "add directories exactly")
persistentFlags.BoolVarP(&config.add.options.Empty, "empty", "e", false, "add empty files")
persistentFlags.BoolVarP(&config.add.options.Exact, "exact", "x", false, "add directories exactly")
persistentFlags.BoolVarP(&config.add.recursive, "recursive", "r", false, "recurse in to subdirectories")
persistentFlags.BoolVarP(&config.add.template, "template", "T", false, "add files as templates")
persistentFlags.BoolVarP(&config.add.options.Template, "template", "T", false, "add files as templates")
}

func (c *Config) runAddCommand(fs vfs.FS, command *cobra.Command, args []string) error {
Expand Down Expand Up @@ -67,12 +65,12 @@ func (c *Config) runAddCommand(fs vfs.FS, command *cobra.Command, args []string)
if err != nil {
return err
}
return ts.Add(fs, path, info, c.add.exact, c.add.empty, c.add.template, mutator)
return ts.Add(fs, c.add.options, path, info, mutator)
}); err != nil {
return err
}
} else {
if err := ts.Add(fs, path, nil, c.add.exact, c.add.empty, c.add.template, mutator); err != nil {
if err := ts.Add(fs, c.add.options, path, nil, mutator); err != nil {
return err
}
}
Expand Down
17 changes: 13 additions & 4 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"testing"

"github.com/twpayne/chezmoi/lib/chezmoi"
"github.com/twpayne/go-vfs/vfst"
)

Expand Down Expand Up @@ -35,7 +36,9 @@ func TestAddCommand(t *testing.T) {
name: "add_template",
args: []string{"/home/user/.gitconfig"},
add: addCommandConfig{
template: true,
options: chezmoi.AddOptions{
Template: true,
},
},
root: map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0755},
Expand Down Expand Up @@ -86,7 +89,9 @@ func TestAddCommand(t *testing.T) {
name: "add_exact_dir",
args: []string{"/home/user/dir"},
add: addCommandConfig{
exact: true,
options: chezmoi.AddOptions{
Exact: true,
},
},
root: map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0755},
Expand All @@ -103,8 +108,10 @@ func TestAddCommand(t *testing.T) {
name: "add_exact_dir_recursive",
args: []string{"/home/user/dir"},
add: addCommandConfig{
exact: true,
recursive: true,
options: chezmoi.AddOptions{
Exact: true,
},
},
root: map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0755},
Expand All @@ -127,7 +134,9 @@ func TestAddCommand(t *testing.T) {
name: "add_empty_file",
args: []string{"/home/user/empty"},
add: addCommandConfig{
empty: true,
options: chezmoi.AddOptions{
Empty: true,
},
},
root: map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0755},
Expand Down
19 changes: 13 additions & 6 deletions lib/chezmoi/target_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import (
vfs "github.com/twpayne/go-vfs"
)

// An AddOptions contains options for TargetState.Add.
type AddOptions struct {
Empty bool
Exact bool
Template bool
}

// A TargetState represents the root target state.
type TargetState struct {
TargetDir string
Expand All @@ -40,7 +47,7 @@ func NewTargetState(targetDir string, umask os.FileMode, sourceDir string, data
}

// Add adds a new target to ts.
func (ts *TargetState) Add(fs vfs.FS, targetPath string, info os.FileInfo, exact, addEmpty, addTemplate bool, mutator Mutator) error {
func (ts *TargetState) Add(fs vfs.FS, addOptions AddOptions, targetPath string, info os.FileInfo, mutator Mutator) error {
if !filepath.HasPrefix(targetPath, ts.TargetDir) {
return fmt.Errorf("%s: outside target directory", targetPath)
}
Expand All @@ -65,7 +72,7 @@ func (ts *TargetState) Add(fs vfs.FS, targetPath string, info os.FileInfo, exact
return err
}
if parentEntry == nil {
if err := ts.Add(fs, filepath.Join(ts.TargetDir, parentDirName), nil, false, false, false, mutator); err != nil {
if err := ts.Add(fs, addOptions, filepath.Join(ts.TargetDir, parentDirName), nil, mutator); err != nil {
return err
}
parentEntry, err = ts.findEntry(parentDirName)
Expand All @@ -88,19 +95,19 @@ func (ts *TargetState) Add(fs vfs.FS, targetPath string, info os.FileInfo, exact
return err
}
empty := len(infos) == 0
return ts.addDir(targetName, entries, parentDirSourceName, exact, perm, empty, mutator)
return ts.addDir(targetName, entries, parentDirSourceName, addOptions.Exact, perm, empty, mutator)
case info.Mode().IsRegular():
if info.Size() == 0 && !addEmpty {
if info.Size() == 0 && !addOptions.Empty {
return nil
}
contents, err := fs.ReadFile(targetPath)
if err != nil {
return err
}
if addTemplate {
if addOptions.Template {
contents = autoTemplate(contents, ts.Data)
}
return ts.addFile(targetName, entries, parentDirSourceName, info, addTemplate, contents, mutator)
return ts.addFile(targetName, entries, parentDirSourceName, info, addOptions.Template, contents, mutator)
case info.Mode()&os.ModeType == os.ModeSymlink:
linkname, err := fs.Readlink(targetPath)
if err != nil {
Expand Down

0 comments on commit 2abc9a9

Please sign in to comment.