`LAGOON_CONTEXT` sets the config directory instead of the Lagoon context

Open Beginner friendly
#501 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
cli

Research direction

Start at getLagoonContext in cmd/root.go (around lines 391-394), where the LAGOON_CONTEXT lookup assigns to configFilePath. Compare it with the neighbouring LAGOONCONTEXT handling and with where lagoonContext is later used, then change the assignment target to lagoonContext. Reproduce with the YAML file from the issue and LAGOON_CONTEXT=second lagoon config current, which should print second; go test ./cmd/... guards against regressions.

Written by the indexing model from the issue text.

Description

Description

The LAGOON_CONTEXT environment variable is meant to select the Lagoon context (like --lagoon / LAGOONCONTEXT), but in getLagoonContext its value is assigned to the global configFilePath instead of lagoonContext:

https://github.com/uselagoon/lagoon-cli/blob/38a7b36e6b1f13cabcc01a5009ddd01f51f2ca29/cmd/root.go#L391-L394

// prefer LAGOON_CONTEXT
if lagoonContextEnvar, ok := os.LookupEnv("LAGOON_CONTEXT"); ok {
    configFilePath = lagoonContextEnvar
}

As a result, LAGOON_CONTEXT is ignored: commands silently run against the default context instead. That can send a command to a different Lagoon than intended (for example production instead of a local one). When the CLI needs to refresh its token, it also tries to write the config file into a directory named after the LAGOON_CONTEXT value, and fails.

Steps to reproduce

cat > /tmp/ctx-test.yml <<'EOF'
current: first
default: first
lagoons:
    first:
        graphql: http://first.invalid/graphql
        hostname: first.invalid
        port: "2223"
    second:
        graphql: http://second.invalid/graphql
        hostname: second.invalid
        port: "2223"
EOF
export LAGOON_CONFIG_FILE=/tmp/ctx-test.yml

lagoon -l second config current               # second
LAGOONCONTEXT=second lagoon config current    # second
LAGOON_CONTEXT=second lagoon config current   # first  <- ignored
LAGOON_CONTEXT=second lagoon whoami           # dial tcp: lookup first.invalid: no such host

Expected

LAGOON_CONTEXT=second selects the second context, like -l second and LAGOONCONTEXT=second.

Actual

The default context first is used.

When that default context needs a token refresh, the command fails instead, because the CLI tries to save the config file into a directory named after the LAGOON_CONTEXT value:

Error: couldn't refresh token: couldn't write config: open second/ctx-test.yml: no such file or directory

Suggested fix

if lagoonContextEnvar, ok := os.LookupEnv("LAGOON_CONTEXT"); ok {
    lagoonContext = lagoonContextEnvar
}

Version

lagoon-cli v0.33.0 (macOS arm64). Still present on main at 38a7b36.

Dominant language
Go
Stars
28
Forks
14
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from uselagoon/lagoon-cli

All issues in uselagoon/lagoon-cli

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.