Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exec-env/-file support multiple args #1626

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ substituted with the temporary file path (whether a FIFO or an actual file).
.. code:: sh

# operating on the same file as before, but as a file this time
$ sops exec-file out.json 'echo your temporary file: {}; cat {}'
$ sops exec-file out.json -- sh -c "echo your temporary file: {}; cat {}"
your temporary file: /tmp/.sops894650499/tmp-file
{
"database_password": "jf48t9wfw094gf4nhdf023r",
Expand All @@ -1077,7 +1077,7 @@ substituted with the temporary file path (whether a FIFO or an actual file).
}

# launch a shell with a variable TMPFILE pointing to the temporary file
$ sops exec-file --no-fifo out.json 'TMPFILE={} sh'
$ sops exec-file --no-fifo out.json -- sh -c 'TMPFILE={} sh'
sh-3.2$ echo $TMPFILE
/tmp/.sops506055069/tmp-file291138648
sh-3.2$ cat $TMPFILE
Expand Down
8 changes: 4 additions & 4 deletions cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func main() {
},
}, keyserviceFlags...),
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
if c.NArg() < 2 {
return common.NewExitError(fmt.Errorf("error: missing file to decrypt"), codes.ErrorGeneric)
}

fileName := c.Args()[0]
command := c.Args()[1]
command := c.Args()[1:]

inputStore := inputStore(c, fileName)

Expand Down Expand Up @@ -265,12 +265,12 @@ func main() {
},
}, keyserviceFlags...),
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
if c.NArg() < 2 {
return common.NewExitError(fmt.Errorf("error: missing file to decrypt"), codes.ErrorGeneric)
}

fileName := c.Args()[0]
command := c.Args()[1]
command := c.Args()[1:]

inputStore := inputStore(c, fileName)
outputStore := outputStore(c, fileName)
Expand Down
12 changes: 8 additions & 4 deletions cmd/sops/subcommand/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand All @@ -23,7 +24,7 @@ func init() {
}

type ExecOpts struct {
Command string
Command []string
Plaintext []byte
Background bool
Pristine bool
Expand Down Expand Up @@ -95,8 +96,11 @@ func ExecWithFile(opts ExecOpts) error {
}
env = append(env, opts.Env...)

placeholdered := strings.Replace(opts.Command, "{}", filename, -1)
cmd := BuildCommand(placeholdered)
args := opts.Command[1:]
for i, arg := range args {
args[i] = strings.Replace(arg, "{}", filename, -1)
}
cmd := exec.Command(opts.Command[0], args...)
cmd.Env = env

if opts.Background {
Expand Down Expand Up @@ -134,7 +138,7 @@ func ExecWithEnv(opts ExecOpts) error {

env = append(env, opts.Env...)

cmd := BuildCommand(opts.Command)
cmd := exec.Command(opts.Command[0], opts.Command[1:]...)
cmd.Env = env

if opts.Background {
Expand Down
5 changes: 0 additions & 5 deletions cmd/sops/subcommand/exec/exec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ package exec

import (
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"syscall"
)

func BuildCommand(command string) *exec.Cmd {
return exec.Command("/bin/sh", "-c", command)
}

func WritePipe(pipe string, contents []byte) {
handle, err := os.OpenFile(pipe, os.O_WRONLY, 0600)

Expand Down
Loading