Skip to content

Commit

Permalink
add support for shell user
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmiko committed Feb 6, 2022
1 parent 93615a4 commit 7ff9909
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.20-beta
v0.2.21-beta
2 changes: 2 additions & 0 deletions cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ var Cmd = &cobra.Command{

func init() {
Cmd.Flags().StringVar(&commands.ShellContainer, "container", "php-fpm", "the container you want to get in")
Cmd.Flags().StringVar(&commands.DefaultShellCommand, "command", "", "the container you want to get in")
Cmd.Flags().StringVar(&commands.ShellUser, "user", "", "the user inside the container")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/protobuf v1.3.4 // indirect
github.com/google/go-cmp v0.5.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
Expand Down
48 changes: 40 additions & 8 deletions internal/commands/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ var (
ShellCommand []string
// ShellContainer is the container used for shell command.
ShellContainer string
defaultShellCommand = "bash"
DefaultShellCommand = "bash"
defaultShellCommandsMap = map[string]string{
"default": "bash",
"pwa-studio": "sh",
"php-fpm": "bash",
"default": "sh",
}
defaultShellContainersMap = map[string]string{
"pwa-studio": "node",
"default": "php-fpm",
}

ShellUser = ""
shellUserMap = map[string]string{
"php-fpm": "www-data",
"pwa-studio": "node",
"default": "root",
}
)

Expand All @@ -30,21 +37,24 @@ func ShellCmd(cmd *cobra.Command, args []string) error {
if ShellContainer == defaultShellContainersMap["default"] {
SetShellContainer(defaultShellContainersMap[core.GetEnvType()])
}

SetDefaultShellCommand(defaultShellCommandsMap[core.GetEnvType()])
}

SetDefaultShellCommand(ShellContainer)
SetShellUser(ShellContainer)

if len(args) > 0 {
ShellCommand = core.ExtractUnknownArgs(cmd.Flags(), args)
} else {
ShellCommand = core.ExtractUnknownArgs(cmd.Flags(), []string{defaultShellCommand})
ShellCommand = core.ExtractUnknownArgs(cmd.Flags(), []string{DefaultShellCommand})
}

log.Debugln("command:", ShellCommand)
log.Debugln("container:", ShellContainer)

var passedArgs []string
passedArgs = append(passedArgs, "exec", ShellContainer)
passedArgs = append(passedArgs, "exec")
passedArgs = append(passedArgs, "--user", ShellUser)
passedArgs = append(passedArgs, ShellContainer)
passedArgs = append(passedArgs, ShellCommand...)

err := EnvRunDockerCompose(passedArgs, false)
Expand All @@ -62,5 +72,27 @@ func SetShellContainer(s string) {

// SetDefaultShellCommand changes the command invoked by reward shell command.
func SetDefaultShellCommand(s string) {
defaultShellCommand = s
if DefaultShellCommand == "" {
if keyExists(defaultShellCommandsMap, s) {
DefaultShellCommand = defaultShellCommandsMap[s]
} else {
DefaultShellCommand = defaultShellCommandsMap["default"]
}
}
}

// SetShellUser changes the user of the reward shell command.
func SetShellUser(s string) {
if ShellUser == "" {
if keyExists(shellUserMap, s) {
ShellUser = shellUserMap[s]
} else {
ShellUser = shellUserMap["default"]
}
}
}

func keyExists(decoded map[string]string, key string) bool {
val, ok := decoded[key]
return ok && val != ""
}

0 comments on commit 7ff9909

Please sign in to comment.