Skip to content

kubectl: read kuberc alias flag groups, and give a short flag's value the next word whole (#140) - #145

Merged
paureis merged 6 commits into
mainfrom
fix/140-kuberc-groups
Nov 4, 2026
Merged

paureis merged 6 commits into
mainfrom
fix/140-kuberc-groups

Conversation

@paureis

@paureis paureis commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Closes #140. Target: 1.3.0.

What changes

1. kuberc aliases with a short flag group. Before, an alias whose prependArgs or appendArgs held a group such as -itnprod, -itn prod, -iA or -is <url> set the target without cloudpin noticing, because setsTarget read only the first letter of each word. Now each list is split with #133's splitShortGroups, using the letters of the alias's command, so -f and -p in an alias of logs take no value. The two lists are split separately because kubectl puts the user's words between them. If an unknown letter comes before a target letter, the alias is reported as "can't tell", and the message names the letter but never the word.

2. A value that looks like a flag. pflag gives a value letter at the end of its word the next word whole, whatever it holds. So in kubectl logs pod -c -n x the container is -n and x is not the namespace. splitShortGroups now also reads lone value letters and joins a value taken from the next word to its flag (-c=-n). That way flag() and allNamespaces can't mistake the value for a flag, and -c -- no longer ends the flags. The global long flags in VALUE_FLAGS get the same treatment (--namespace -n becomes --namespace=-n).

3. User flags after an alias. kubectl runs an alias as a copy of its command, so the user's own short flags follow that command's letters. For example, kubectl <alias of logs> -fn kube-system pod is now checked against kube-system.

Sources: pflag flag.go (parseSingleShortArg, parseLongArg, parseArgs), kubernetes.io/docs/reference/kubectl/kuberc/, and kubectl pkg/kuberc/kuberc.go (applyAliases).

Tests

All new tests are in test/kubernetes-kuberc-groups.test.ts: 25 tests, including wrong-pin controls through guard. They were committed failing first: 15 of the 25 fail on main. Typecheck passes. Full suite: 51 files, 1292 passed, 8 skipped.

As a mutation control, I went back to reading alias arguments without the split, and separately dropped the = join for short values. Each mutation makes 7 tests fail, and the file was restored afterwards.

Left open

  • Per-command long flags whose value looks like a flag (--container -n x). kubectl has too many to list them all.
  • A prependArgs ending in a value letter (["-c"]) takes the user's first word. Handling that would mean expanding the alias fully. It's rare, and the mistake only matters when the user also passes -n.
  • appendArgs that land after a user's -- are still scanned. This errs on the side of stopping the command.

🤖 Generated with Claude Code

paureis and others added 4 commits October 24, 2026 21:03
Fifteen of the new cases fail on main: a kuberc alias whose prependArgs or
appendArgs hold a short flag group that sets -n, -s or -A is not noticed;
the user's flags after an alias of logs are read with the wrong letters;
and a value letter's next word (-c -n x, -f -n, -c --) is read as a flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pflag (parseSingleShortArg) gives a value letter that ends its word the
next word, whatever it holds: in `kubectl logs -c -n x` the container is
"-n" and x is a positional argument, so the namespace is not x. The split
now handles a lone value letter too and joins a value taken from the next
word to its flag (`-c=-n`), so `flag` and `allNamespaces` can't read it as
a flag, and a `--` taken as a value doesn't end the flags. The global long
flags cloudpin knows take a value (VALUE_FLAGS) are joined the same way;
kubectl's per-command long flags (--container -n x) are left open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A kuberc alias's prependArgs and appendArgs were checked by their first
letter only, so `-itnprod`, `-itn prod` or `-iA` set the namespace without
cloudpin noticing. Each list is now split as pflag reads it, with the
letters of the alias's command (its `command` field's first word, since
logs and proxy change what -f, -p and -w mean), and the alias is a problem
when a split word picks the target or when an unknown letter comes before
a target letter (the message names the letter, never the word). The lists
are split apart because kubectl puts the user's words between them, and
words after `--` are left alone.

The new test's "only the letter is named" check now looks for the joined
value rather than "-xn", which a random temporary folder name could hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s letters (#140)

kubectl runs an alias as a copy of its command, flags included, so in an
alias of logs the user's -f is --follow and `-fn kube-system` sets the
namespace. The split used the alias's own name, which got the default
letters (-f takes a value) and missed the namespace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@paureis
paureis marked this pull request as ready for review November 4, 2026 23:28
@paureis
paureis merged commit 224116f into main Nov 4, 2026
5 checks passed
@paureis
paureis deleted the fix/140-kuberc-groups branch November 4, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kubectl: kuberc aliases with a short flag group, and values that look like -n

1 participant