Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass full command name to executables #136

Merged
merged 7 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions gh-pages/content/en/docs/overview/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ AUTH_TOKEN=${COLA_AUTH_TOKEN}

## Available resources

| Resource Name | Require User Consent | Description |
|---------------|----------------------|---------------------------------------------------------|
| USERNAME | Yes | the username collected from `login` command |
| PASSWORD | Yes | the password collected from `login` command |
| AUTH_TOKEN | Yes | the authentication token collected from `login` command |
| LOG_LEVEL | Yes | the log level of command launcher |
| DEBUG_FLAGS | Yes | the debug flags defined in command launcher's config |
| PACKAGE_DIR | No | the absolute path to the package directory |
| Resource Name | Require User Consent | Description |
|-------------------|----------------------|-----------------------------------------------------------|
| USERNAME | Yes | the username collected from `login` command |
| PASSWORD | Yes | the password collected from `login` command |
| AUTH_TOKEN | Yes | the authentication token collected from `login` command |
| LOG_LEVEL | Yes | the log level of command launcher |
| DEBUG_FLAGS | Yes | the debug flags defined in command launcher's config |
| PACKAGE_DIR | No | the absolute path to the package directory |
| FULL_COMMAND_NAME | No | the name of the command executed (includes app and group) |
2 changes: 2 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type LauncherContext interface {

CmdPackageDirEnvVar() string

FullCmdNameEnvVar() string

/* General function to get a environment variable name with prefix conventions */
EnvVarName(name string) string
}
4 changes: 4 additions & 0 deletions internal/context/default-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (ctx *defaultContext) CmdPackageDirEnvVar() string {
return ctx.EnvVarName("PACKAGE_DIR")
}

func (ctx *defaultContext) FullCmdNameEnvVar() string {
return ctx.EnvVarName("FULL_COMMAND_NAME")
}

func (ctx *defaultContext) EnvVarName(name string) string {
return fmt.Sprintf("%s_%s", ctx.prefix(), name)
}
Expand Down
23 changes: 23 additions & 0 deletions internal/frontend/default-frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -261,6 +262,25 @@ func formatExamples(examples []command.ExampleEntry) string {
return strings.Join(output, "\n")
}

func (self *defaultFrontend) getFullCommandName(group, name string) string {
var programName string
// Attempt getting the name of the program that is currently running
// but fall back to the configured app name if that fails.
execPath, err := os.Executable()
if err != nil {
programName = self.appCtx.AppName()
} else {
programName = filepath.Base(execPath)
}

tokens := []string{programName}
if group != "" {
tokens = append(tokens, group)
}
tokens = append(tokens, name)
return strings.Join(tokens, " ")
}

func (self *defaultFrontend) getExecutableCommand(group, name string) (command.Command, error) {
iCmd, err := self.backend.FindCommand(group, name)
return iCmd, err
Expand All @@ -277,7 +297,10 @@ func (self *defaultFrontend) executeCommand(group, name string, args []string, i
}

envCtx := self.getCmdEnvContext(initialEnvCtx, consent)

// Resources that do not depend on consent:
envCtx = append(envCtx, fmt.Sprintf("%s=%s", self.appCtx.CmdPackageDirEnvVar(), iCmd.PackageDir()))
envCtx = append(envCtx, fmt.Sprintf("%s=%s", self.appCtx.FullCmdNameEnvVar(), self.getFullCommandName(group, name)))

exitCode, err := iCmd.Execute(envCtx, args...)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ To run all the integration test simply run the script: `integration.sh`

```bash
cd command-launcher
./test/interation.sh
./test/integration.sh
jdevera marked this conversation as resolved.
Show resolved Hide resolved
```

To run particular integration tests, pass the integration test file name (without .sh) in the `integration` folder as the arguments.
Expand All @@ -15,7 +15,7 @@ For example, the following command runs the tests in:

```bash
cd command-launcher
./test/interation.sh test-basic test-exit-code
./test/integration.sh test-basic test-exit-code
```

Copy the test-template.sh to create a new test suite
23 changes: 23 additions & 0 deletions test/integration/test-cmd-context.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,26 @@ else
exit 1
fi

# Make a copy and run the copy to ensure FULL_COMMAND_NAME starts
# with the name of the actual executable that runs the launcher
cp "$OUTPUT_DIR/"{cl,clcopy}

echo "> test FULL_COMMAND_NAME environment variable (with group)"
RESULT=$("$OUTPUT_DIR"/clcopy greeting saybonjour)
echo "$RESULT" | grep -q "^command name: clcopy greeting saybonjour$"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO - should have FULL_COMMAND_NAME environment variable"
exit 1
fi

echo "> test FULL_COMMAND_NAME environment variable (no group)"
RESULT=$("$OUTPUT_DIR"/clcopy bonjour)
echo "$RESULT" | grep -q "^command name: clcopy bonjour$"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO - should have FULL_COMMAND_NAME environment variable"
exit 1
fi
1 change: 1 addition & 0 deletions test/packages-src/bonjour/bonjour.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ECHO %CL_FLAG_LANGUAGE%
ECHO %CL_ARG_1%

ECHO packge dir: %CL_PACKAGE_DIR%
ECHO command name: %CL_COMMAND_NAME%
jdevera marked this conversation as resolved.
Show resolved Hide resolved

ECHO cola flag: %COLA_FLAG_NAME%
ECHO cola flag: %COLA_FLAG_LANGUAGE%
Expand Down
1 change: 1 addition & 0 deletions test/packages-src/bonjour/bonjour.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ echo $CL_FLAG_LANGUAGE
echo $CL_ARG_1

echo "packge dir: $CL_PACKAGE_DIR"
echo "command name: $CL_FULL_COMMAND_NAME"

echo "cola flag: $COLA_FLAG_NAME"
echo "cola flag: $COLA_FLAG_LANGUAGE"
Expand Down
Loading