Skip to content

Commit

Permalink
feat: support setting working directory from CLI arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
devbranch-vadym committed Nov 15, 2022
1 parent d1b98be commit 305ea25
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ portainerssh -c /bin/sh my-container-name
--api_key="" Portainer API key.
-c, --command="bash" Command to execute inside container.
-u, --run_as_user="" User to execute container command as.
-w, --workdir="" Working directory to execute command in.
```

### Arguments
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func ReadConfig(version string) (*Config, *portainer.ContainerExecParams) {
var container = app.Arg("container", "Container name, wildcards allowed").Required().String()
var command = app.Flag("command", "Command to execute inside container.").Default("bash").Short('c').String()
var runAs = app.Flag("run_as_user", "User to execute container command as.").Default("").Short('u').String()
var workdir = app.Flag("workdir", "Working directory to execute command in.").Default("").Short('w').String()

app.Parse(os.Args[1:])

Expand All @@ -114,6 +115,7 @@ func ReadConfig(version string) (*Config, *portainer.ContainerExecParams) {
ContainerName: *container,
Command: commandParts,
User: *runAs,
WorkingDir: *workdir,
}

}
4 changes: 4 additions & 0 deletions pkg/portainer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ContainerExecParams struct {
ContainerName string
Command []string
User string
WorkingDir string
}

// ShellSession contains details about remote shell connected via WebSocket.
Expand Down Expand Up @@ -184,6 +185,9 @@ func (r *API) spawnExecInstance(containerId string, params *ContainerExecParams)
if params.User != "" {
jsonBodyData["User"] = params.User
}
if params.WorkingDir != "" {
jsonBodyData["WorkingDir"] = params.WorkingDir
}
body, err := json.Marshal(jsonBodyData)
if err != nil {
return "", err
Expand Down

0 comments on commit 305ea25

Please sign in to comment.