Skip to content

Commit

Permalink
Merge pull request #44 from razvan-agape/feat/interactive-terminal-se…
Browse files Browse the repository at this point in the history
…ssion

Interactive mode in interpreter sessions
  • Loading branch information
alexei-led committed May 13, 2024
2 parents fbd5a1e + e858599 commit 73eaa84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ FROM busybox:1.36
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# copy the binary to the production image from the builder stage.
COPY --from=builder /go/src/app/.bin/secrets-init /secrets-init
RUN adduser -D -u 1000 secrets-init
USER 1000

ENTRYPOINT ["/secrets-init"]
CMD ["--version"]
19 changes: 16 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func main() {
Name: "google-project",
Usage: "the google cloud project for secrets without a project prefix",
},
&cli.BoolFlag{
Name: "interactive",
Aliases: []string{"i"},
Usage: "use this flag if the command expects some input from the stdin",
},
},
Commands: []*cli.Command{
{
Expand Down Expand Up @@ -149,7 +154,7 @@ func mainCmd(c *cli.Context) error {

// Launch main command
var childPid int
childPid, err = run(ctx, provider, c.Bool("exit-early"), c.Args().Slice())
childPid, err = run(ctx, provider, c.Bool("exit-early"), c.Bool("interactive"), c.Args().Slice())
if err != nil {
log.WithError(err).Error("failed to run")
os.Exit(1)
Expand Down Expand Up @@ -188,7 +193,7 @@ func removeZombies(childPid int) {
}

// run passed command
func run(ctx context.Context, provider secrets.Provider, exitEarly bool, commandSlice []string) (childPid int, err error) {
func run(ctx context.Context, provider secrets.Provider, exitEarly, interactive bool, commandSlice []string) (childPid int, err error) {
var commandStr string
var argsSlice []string

Expand All @@ -213,7 +218,15 @@ func run(ctx context.Context, provider secrets.Provider, exitEarly bool, command
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// create a dedicated pidgroup used to forward signals to the main process and its children
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
procAttrs := &syscall.SysProcAttr{Setpgid: true}
// rebind stdin if -i flag is set
if interactive {
cmd.Stdin = os.Stdin
// setting 'Foreground' to true will bind current TTY to the child process
procAttrs = &syscall.SysProcAttr{Setpgid: true, Foreground: true}
}
// set child process attributes
cmd.SysProcAttr = procAttrs

// set environment variables
if provider != nil {
Expand Down

0 comments on commit 73eaa84

Please sign in to comment.