Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 4c4b9a4

Browse files
Merge pull request #191 from secrethub/feature/read-out-file
Add --out-file and --file-mode flags to read
2 parents 015f4f3 + 57654fa commit 4c4b9a4

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

internals/secrethub/read.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package secrethub
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"time"
67

78
"github.com/secrethub/secrethub-cli/internals/cli/clip"
9+
"github.com/secrethub/secrethub-cli/internals/cli/filemode"
810
"github.com/secrethub/secrethub-cli/internals/cli/posix"
911
"github.com/secrethub/secrethub-cli/internals/cli/ui"
1012

@@ -20,6 +22,8 @@ type ReadCommand struct {
2022
useClipboard bool
2123
clearClipboardAfter time.Duration
2224
clipper clip.Clipper
25+
outFile string
26+
fileMode filemode.FileMode
2327
newClient newClientFunc
2428
}
2529

@@ -44,6 +48,8 @@ func (cmd *ReadCommand) Register(r Registerer) {
4448
units.HumanDuration(cmd.clearClipboardAfter),
4549
),
4650
).Short('c').BoolVar(&cmd.useClipboard)
51+
clause.Flag("out-file", "Write the secret value to this file.").Short('o').StringVar(&cmd.outFile)
52+
clause.Flag("file-mode", "Set filemode for the output file. Defaults to 0600 (read and write for current user) and is ignored without the --out-file flag.").Default("0600").SetValue(&cmd.fileMode)
4753

4854
BindAction(clause, cmd.Run)
4955
}
@@ -72,7 +78,16 @@ func (cmd *ReadCommand) Run() error {
7278
cmd.path,
7379
units.HumanDuration(cmd.clearClipboardAfter),
7480
)
75-
} else {
81+
}
82+
83+
if cmd.outFile != "" {
84+
err = ioutil.WriteFile(cmd.outFile, posix.AddNewLine(secret.Data), cmd.fileMode.FileMode())
85+
if err != nil {
86+
return ErrCannotWrite(cmd.outFile, err)
87+
}
88+
}
89+
90+
if cmd.outFile == "" && !cmd.useClipboard {
7691
fmt.Fprintf(cmd.io.Stdout(), "%s", string(posix.AddNewLine(secret.Data)))
7792
}
7893

0 commit comments

Comments
 (0)