Fix commands.

Add username to config struct, if present execute `sudo -E -u username`
in command
This commit is contained in:
Aleksander Cynarski 2021-10-21 18:01:31 +02:00
parent d9c1c42150
commit 537db057f3
Signed by: paramah
GPG Key ID: C4340BA42B9C173A
7 changed files with 19 additions and 5 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ dist
.env
.ledo-mode
main
ledo

View File

@ -7,7 +7,8 @@ import (
)
var CmdComposeBuild = cli.Command{
Name: "pull",
Name: "build",
Aliases: []string{"b"},
Usage: "build docker image",
Description: `Build all docker images`,
Action: RunComposeBuild,

View File

@ -8,14 +8,19 @@ import (
var CmdComposeRun = cli.Command{
Name: "run",
Aliases: []string{"r"},
Usage: "run cmd in main container",
Description: `Run command in main container`,
ArgsUsage: "[<cmd>]",
Action: RunComposeRun,
}
func RunComposeRun(cmd *cli.Context) error {
ctx := context.InitCommand(cmd)
compose.ExecComposerRun(ctx)
if cmd.Args().Len() >= 1 {
compose.ExecComposerRun(ctx, cmd.Args())
return nil
}
return nil
}

View File

@ -8,6 +8,7 @@ import (
var CmdComposeShell = cli.Command{
Name: "shell",
Aliases: []string{"sh"},
Usage: "run shell from main service",
Description: `Execute shell cmd in main service`,
Action: RunComposeShell,

View File

@ -10,7 +10,7 @@ var CmdComposeStop = cli.Command{
Name: "stop",
Usage: "stop containers",
Description: `Stop all containers defined in docker-compose stack mode`,
Action: RunComposePull,
Action: RunComposeStop,
}
func RunComposeStop(cmd *cli.Context) error {

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/Masterminds/semver"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"io/ioutil"
"ledo/app/modules/context"
@ -147,9 +148,13 @@ func ExecComposerDebug(ctx *context.LedoContext) {
ctx.ExecCmd("docker-compose", args[0:])
}
func ExecComposerRun(ctx *context.LedoContext) {
func ExecComposerRun(ctx *context.LedoContext, command cli.Args) {
args := ctx.ComposeArgs
args = append(args, "run", strings.ToLower(ctx.Config.Docker.MainService), ctx.Config.Docker.Shell)
args = append(args, "run", strings.ToLower(ctx.Config.Docker.MainService))
if ctx.Config.Docker.Username != "" {
args = append(args, "sudo", "-E", "-u", ctx.Config.Docker.Username)
}
args = append(args, command.Slice()...)
ctx.ExecCmd("docker-compose", args[0:])
}

View File

@ -17,6 +17,7 @@ type DockerMap struct {
Name string `yaml:"name,omitempty"`
MainService string `yaml:"main_service,omitempty"`
Shell string `yaml:"shell,omitempty"`
Username string `yaml:"username,omitempty"`
}
func NewLedoFile(s string) (*LedoFile, error) {