Skip to content

Commit

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

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

Expand All @@ -27,19 +28,17 @@ var _importCommand = &cobra.Command{
}

type importCommandConfig struct {
destination string
exact bool
removeDestination bool
stripComponents int
importTAROptions chezmoi.ImportTAROptions
}

func init() {
rootCommand.AddCommand(_importCommand)

persistentFlags := _importCommand.PersistentFlags()
persistentFlags.StringVarP(&config._import.destination, "destination", "d", "", "destination prefix")
persistentFlags.BoolVarP(&config._import.exact, "exact", "x", false, "import directories exactly")
persistentFlags.IntVar(&config._import.stripComponents, "strip-components", 0, "strip components")
persistentFlags.StringVarP(&config._import.importTAROptions.DestinationDir, "destination", "d", "", "destination prefix")
persistentFlags.BoolVarP(&config._import.importTAROptions.Exact, "exact", "x", false, "import directories exactly")
persistentFlags.IntVar(&config._import.importTAROptions.StripComponents, "strip-components", 0, "strip components")
persistentFlags.BoolVarP(&config._import.removeDestination, "remove-destination", "r", false, "remove destination before import")
}

Expand Down Expand Up @@ -74,7 +73,7 @@ func (c *Config) runImportCommand(fs vfs.FS, cmd *cobra.Command, args []string)
}
mutator := c.getDefaultMutator(fs)
if c._import.removeDestination {
entry, err := ts.Get(c._import.destination)
entry, err := ts.Get(c._import.importTAROptions.DestinationDir)
switch {
case err == nil:
if err := mutator.RemoveAll(filepath.Join(c.SourceDir, entry.SourceName())); err != nil {
Expand All @@ -85,5 +84,5 @@ func (c *Config) runImportCommand(fs vfs.FS, cmd *cobra.Command, args []string)
return err
}
}
return ts.ImportTAR(tar.NewReader(r), c._import.destination, c._import.exact, c._import.stripComponents, mutator)
return ts.ImportTAR(tar.NewReader(r), c._import.importTAROptions, mutator)
}
23 changes: 15 additions & 8 deletions lib/chezmoi/target_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type AddOptions struct {
Template bool
}

// An ImportTAROptions contains options for TargetState.ImportTAR.
type ImportTAROptions struct {
DestinationDir string
Exact bool
StripComponents int
}

// A TargetState represents the root target state.
type TargetState struct {
TargetDir string
Expand Down Expand Up @@ -201,7 +208,7 @@ func (ts *TargetState) Get(target string) (Entry, error) {
}

// ImportTAR imports a tar archive.
func (ts *TargetState) ImportTAR(r *tar.Reader, destinationDir string, exact bool, stripComponents int, mutator Mutator) error {
func (ts *TargetState) ImportTAR(r *tar.Reader, importTAROptions ImportTAROptions, mutator Mutator) error {
for {
header, err := r.Next()
if err == io.EOF {
Expand All @@ -211,7 +218,7 @@ func (ts *TargetState) ImportTAR(r *tar.Reader, destinationDir string, exact boo
}
switch header.Typeflag {
case tar.TypeDir, tar.TypeReg, tar.TypeSymlink:
if err := ts.importHeader(r, header, destinationDir, exact, stripComponents, mutator); err != nil {
if err := ts.importHeader(r, importTAROptions, header, mutator); err != nil {
return err
}
case tar.TypeXGlobalHeader:
Expand Down Expand Up @@ -489,13 +496,13 @@ func (ts *TargetState) findEntry(name string) (Entry, error) {
return entries[names[len(names)-1]], nil
}

func (ts *TargetState) importHeader(r io.Reader, header *tar.Header, destinationDir string, exact bool, stripComponents int, mutator Mutator) error {
func (ts *TargetState) importHeader(r io.Reader, importTAROptions ImportTAROptions, header *tar.Header, mutator Mutator) error {
targetPath := header.Name
if stripComponents > 0 {
targetPath = filepath.Join(strings.Split(targetPath, string(os.PathSeparator))[stripComponents:]...)
if importTAROptions.StripComponents > 0 {
targetPath = filepath.Join(strings.Split(targetPath, string(os.PathSeparator))[importTAROptions.StripComponents:]...)
}
if destinationDir != "" {
targetPath = filepath.Join(destinationDir, targetPath)
if importTAROptions.DestinationDir != "" {
targetPath = filepath.Join(importTAROptions.DestinationDir, targetPath)
} else {
targetPath = filepath.Join(ts.TargetDir, targetPath)
}
Expand All @@ -521,7 +528,7 @@ func (ts *TargetState) importHeader(r io.Reader, header *tar.Header, destination
case tar.TypeDir:
perm := os.FileMode(header.Mode).Perm()
empty := false // FIXME don't assume directory is empty
return ts.addDir(targetName, entries, parentDirSourceName, exact, perm, empty, mutator)
return ts.addDir(targetName, entries, parentDirSourceName, importTAROptions.Exact, perm, empty, mutator)
case tar.TypeReg:
info := header.FileInfo()
contents, err := ioutil.ReadAll(r)
Expand Down

0 comments on commit e66da3c

Please sign in to comment.