Skip to content

Commit

Permalink
Quote arguments in verbose mode if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 22, 2019
1 parent 7acba23 commit 84350bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -7,6 +7,7 @@ require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/alessio/shellescape v0.0.0-20190409004728-b115ca0f9053
github.com/coreos/go-semver v0.3.0
github.com/gobuffalo/packr/v2 v2.7.1
github.com/golang/protobuf v1.3.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -13,6 +13,8 @@ github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuN
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alessio/shellescape v0.0.0-20190409004728-b115ca0f9053 h1:H/GMMKYPkEIC3DF/JWQz8Pdd+Feifov2EIgGfNpeogI=
github.com/alessio/shellescape v0.0.0-20190409004728-b115ca0f9053/go.mod h1:xW8sBma2LE3QxFSzCnH9qe6gAE2yO9GvQaWwX89HxbE=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down
40 changes: 22 additions & 18 deletions internal/chezmoi/verbosemutator.go
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

"github.com/alessio/shellescape"
"github.com/pkg/diff"
)

Expand All @@ -34,7 +35,7 @@ func NewVerboseMutator(w io.Writer, m Mutator, colored bool) *VerboseMutator {

// Chmod implements Mutator.Chmod.
func (m *VerboseMutator) Chmod(name string, mode os.FileMode) error {
action := fmt.Sprintf("chmod %o %s", mode, name)
action := fmt.Sprintf("chmod %o %s", mode, shellescape.Quote(name))
err := m.m.Chmod(name, mode)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand All @@ -46,12 +47,7 @@ func (m *VerboseMutator) Chmod(name string, mode os.FileMode) error {

// IdempotentCmdOutput implements Mutator.IdempotentCmdOutput.
func (m *VerboseMutator) IdempotentCmdOutput(cmd *exec.Cmd) ([]byte, error) {
var action string
if cmd.Dir == "" {
action = cmd.String()
} else {
action = fmt.Sprintf("( cd %s && %s )", cmd.Dir, cmd.String())
}
action := cmdString(cmd)
output, err := m.m.IdempotentCmdOutput(cmd)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand All @@ -63,7 +59,7 @@ func (m *VerboseMutator) IdempotentCmdOutput(cmd *exec.Cmd) ([]byte, error) {

// Mkdir implements Mutator.Mkdir.
func (m *VerboseMutator) Mkdir(name string, perm os.FileMode) error {
action := fmt.Sprintf("mkdir -m %o %s", perm, name)
action := fmt.Sprintf("mkdir -m %o %s", perm, shellescape.Quote(name))
err := m.m.Mkdir(name, perm)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand All @@ -75,7 +71,7 @@ func (m *VerboseMutator) Mkdir(name string, perm os.FileMode) error {

// RemoveAll implements Mutator.RemoveAll.
func (m *VerboseMutator) RemoveAll(name string) error {
action := fmt.Sprintf("rm -rf %s", name)
action := fmt.Sprintf("rm -rf %s", shellescape.Quote(name))
err := m.m.RemoveAll(name)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand All @@ -87,7 +83,7 @@ func (m *VerboseMutator) RemoveAll(name string) error {

// Rename implements Mutator.Rename.
func (m *VerboseMutator) Rename(oldpath, newpath string) error {
action := fmt.Sprintf("mv %s %s", oldpath, newpath)
action := fmt.Sprintf("mv %s %s", shellescape.Quote(oldpath), shellescape.Quote(newpath))
err := m.m.Rename(oldpath, newpath)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand All @@ -99,12 +95,7 @@ func (m *VerboseMutator) Rename(oldpath, newpath string) error {

// RunCmd implements Mutator.RunCmd.
func (m *VerboseMutator) RunCmd(cmd *exec.Cmd) error {
var action string
if cmd.Dir == "" {
action = cmd.String()
} else {
action = fmt.Sprintf("( cd %s && %s )", cmd.Dir, cmd.String())
}
action := cmdString(cmd)
err := m.m.RunCmd(cmd)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand All @@ -121,7 +112,7 @@ func (m *VerboseMutator) Stat(name string) (os.FileInfo, error) {

// WriteFile implements Mutator.WriteFile.
func (m *VerboseMutator) WriteFile(name string, data []byte, perm os.FileMode, currData []byte) error {
action := fmt.Sprintf("install -m %o /dev/null %s", perm, name)
action := fmt.Sprintf("install -m %o /dev/null %s", perm, shellescape.Quote(name))
err := m.m.WriteFile(name, data, perm, currData)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand Down Expand Up @@ -157,7 +148,7 @@ func (m *VerboseMutator) WriteFile(name string, data []byte, perm os.FileMode, c

// WriteSymlink implements Mutator.WriteSymlink.
func (m *VerboseMutator) WriteSymlink(oldname, newname string) error {
action := fmt.Sprintf("ln -sf %s %s", oldname, newname)
action := fmt.Sprintf("ln -sf %s %s", shellescape.Quote(oldname), shellescape.Quote(newname))
err := m.m.WriteSymlink(oldname, newname)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
Expand All @@ -167,6 +158,19 @@ func (m *VerboseMutator) WriteSymlink(oldname, newname string) error {
return err
}

// cmdString returns a string representation of cmd.
func cmdString(cmd *exec.Cmd) string {
components := append([]string{cmd.Path}, cmd.Args...)
for i, component := range components {
components[i] = shellescape.Quote(component)
}
s := strings.Join(components, " ")
if cmd.Dir == "" {
return s
}
return fmt.Sprintf("( cd %s && %s )", shellescape.Quote(cmd.Dir), s)
}

func isBinary(data []byte) bool {
return len(data) != 0 && !strings.HasPrefix(http.DetectContentType(data), "text/")
}
Expand Down

0 comments on commit 84350bf

Please sign in to comment.