Skip to content

Commit

Permalink
Support shell environment variables with --env option
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed May 9, 2024
1 parent 467de1b commit ffc8c1e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ $ alpacon websh -u [USER NAME] -g [GROUP NAME] [SERVER NAME] [COMMAND]
$ alpacon websh --username=[USER NAME] --groupname=[GROUP NAME] [SERVER NAME] [COMMAND]
$ alpacon websh --env="KEY1=VALUE1" --env="KEY2=VALUE2" [SERVER NAME] [COMMAND]
# Use the current shell's value for the environment variable 'KEY'.
$ alpacon websh --env="KEY" [SERVER NAME] [COMMAND]
```
- Note: All flags must be placed before the `[SERVER NAME]`.

Expand Down
16 changes: 16 additions & 0 deletions cmd/websh/websh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
"github.com/spf13/cobra"
"os"
"strings"
)

Expand All @@ -23,6 +24,12 @@ var WebshCmd = &cobra.Command{
// Execute a command directly on a server and retrieve the output
alpacon websh [SERVER_NAME] [COMMAND]
// Set the environment variable 'KEY' to 'VALUE' for the command.
alpacon websh --env="KEY1=VALUE1" --env="KEY2=VALUE2" [SERVER NAME] [COMMAND]
// Use the current shell's value for the environment variable 'KEY'.
alpacon websh --env="KEY" [SERVER NAME] [COMMAND]
// Open a websh terminal as a root user
alpacon websh -r [SERVER_NAME]
Expand All @@ -45,6 +52,8 @@ var WebshCmd = &cobra.Command{
-r Run the websh terminal as the root user.
-u / --username [USER_NAME] Specify the username under which the command should be executed.
-g / --groupname [GROUP_NAME] Specify the group name under which the command should be executed.
--env="KEY=VALUE" Set the environment variable 'KEY' to 'VALUE' for the command.
--env="KEY" Use the current shell's value for the environment variable 'KEY'.
-s, --share Share the current terminal to others via a temporary link.
--url [SHARED_URL] Specify the URL of the shared session to join.
Expand Down Expand Up @@ -157,6 +166,13 @@ func extractEnvValue(args []string, i int, env map[string]string) int {
parts := strings.SplitN(envString, "=", 2)
if len(parts) == 2 {
env[parts[0]] = parts[1]
} else if len(parts) == 1 {
value, exists := os.LookupEnv(parts[0])
if !exists {
utils.CliWarning("No environment variable found for key '%s'\n", parts[0])
} else {
env[parts[0]] = value
}
} else {
utils.CliError("Invalid format for --env. Expected '--env=KEY=VALUE'.")
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/websh/websh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ func TestCommandParsing(t *testing.T) {
expectServerName: "server-name",
expectCommandArgs: []string{"cmd"},
},
{
testName: "EnvKeyWithoutValue",
args: []string{"--env=KEY", "server-name", "cmd"},
expectUsername: "",
expectGroupname: "",
expectEnv: map[string]string{}, // Assume no environment variable exists for 'KEY'
expectServerName: "server-name",
expectCommandArgs: []string{"cmd"},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit ffc8c1e

Please sign in to comment.