Skip to content

Commit

Permalink
add filename to exec-file
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Groschupp committed Oct 23, 2020
1 parent 5f7d324 commit 1a810e1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
11 changes: 11 additions & 0 deletions cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func main() {
Name: "user",
Usage: "the user to run the command as",
},
cli.StringFlag{
Name: "filename",
Usage: "filename for the temporarily file (default: tmp-file)",
},

}, keyserviceFlags...),
Action: func(c *cli.Context) error {
if len(c.Args()) != 2 {
Expand Down Expand Up @@ -211,12 +216,18 @@ func main() {
return toExitError(err)
}

filename := c.String("filename")
if filename == "" {
filename = "tmp-file"
}

if err := exec.ExecWithFile(exec.ExecOpts{
Command: command,
Plaintext: output,
Background: c.Bool("background"),
Fifo: !c.Bool("no-fifo"),
User: c.String("user"),
Filename: c.String("filename"),
}); err != nil {
return toExitError(err)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/sops/subcommand/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ type ExecOpts struct {
Background bool
Fifo bool
User string
Filename string
}

func GetFile(dir string) *os.File {
handle, err := ioutil.TempFile(dir, "tmp-file")
func GetFile(dir, filename string) *os.File {
handle, err := ioutil.TempFile(dir, filename)
if err != nil {
log.Fatal(err)
}
Expand All @@ -55,10 +56,10 @@ func ExecWithFile(opts ExecOpts) error {
if opts.Fifo {
// fifo handling needs to be async, even opening to write
// will block if there is no reader present
filename = GetPipe(dir)
filename = GetPipe(dir, opts.Filename)
go WritePipe(filename, opts.Plaintext)
} else {
handle := GetFile(dir)
handle := GetFile(dir, opts.Filename)
handle.Write(opts.Plaintext)
handle.Close()
filename = handle.Name()
Expand Down
4 changes: 2 additions & 2 deletions cmd/sops/subcommand/exec/exec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func WritePipe(pipe string, contents []byte) {
handle.Close()
}

func GetPipe(dir string) string {
tmpfn := filepath.Join(dir, "tmp-file")
func GetPipe(dir, filename string) string {
tmpfn := filepath.Join(dir, filename)
err := syscall.Mkfifo(tmpfn, 0600)
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/sops/subcommand/exec/exec_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func WritePipe(pipe string, contents []byte) {
log.Fatal("fifos are not available on windows")
}

func GetPipe(dir string) string {
func GetPipe(dir, filename string) string {
log.Fatal("fifos are not available on windows")
return ""
}
Expand Down

0 comments on commit 1a810e1

Please sign in to comment.