From c9a52c871f96ec28fe211fc5c8b728690107c8be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Farys?= Date: Tue, 16 Aug 2022 12:37:55 +0200 Subject: [PATCH] Added an optional flag to change user of the shell that enters into the container --- app/cmd/docker/shell.go | 10 +++++++++- app/modules/compose/compose.go | 11 ++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/cmd/docker/shell.go b/app/cmd/docker/shell.go index 3f454e4..e023572 100644 --- a/app/cmd/docker/shell.go +++ b/app/cmd/docker/shell.go @@ -12,10 +12,18 @@ var CmdComposeShell = cli.Command{ Usage: "run shell from main service", Description: `Execute shell cmd in main service`, Action: RunComposeShell, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "user", + Aliases: []string{"u"}, + Usage: "Username or UID (format: )", + Required: false, + }, + }, } func RunComposeShell(cmd *cli.Context) error { ctx := context.InitCommand(cmd) - compose.ExecComposerShell(ctx) + compose.ExecComposerShell(ctx, *cmd) return nil } diff --git a/app/modules/compose/compose.go b/app/modules/compose/compose.go index 09cf475..1fcd8c4 100644 --- a/app/modules/compose/compose.go +++ b/app/modules/compose/compose.go @@ -36,7 +36,7 @@ func CheckDockerComposeVersion() { composeSemVer, _ := semver.NewVersion(composeVersion) if !verConstraint.Check(composeSemVer) { - logger.Critical("Wrong docker-compose version, please update to " + DockerComposeVersion + " or higher.", nil) + logger.Critical("Wrong docker-compose version, please update to "+DockerComposeVersion+" or higher.", nil) } } @@ -164,10 +164,15 @@ func ExecComposerRm(ctx *context.LedoContext) { ctx.ExecCmd("docker-compose", args[0:]) } -func ExecComposerShell(ctx *context.LedoContext) { +func ExecComposerShell(ctx *context.LedoContext, command cli.Context) { PrintCurrentMode(ctx) args := ctx.ComposeArgs - args = append(args, "exec", strings.ToLower(ctx.Config.Docker.MainService), ctx.Config.Docker.Shell) + args = append(args, "exec") + user := command.String("user") + if user != "" { + args = append(args, "--user", user) + } + args = append(args, strings.ToLower(ctx.Config.Docker.MainService), ctx.Config.Docker.Shell) ctx.ExecCmd("docker-compose", args[0:]) }