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

Add configurable command rendering (for kubectl plugins) #113

Open
applejag opened this issue Apr 30, 2024 · 0 comments
Open

Add configurable command rendering (for kubectl plugins) #113

applejag opened this issue Apr 30, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@applejag
Copy link
Collaborator

applejag commented Apr 30, 2024

Expected Behavior

Would be nice if kubecolor adds colors to some popular kubectl plugins.

As we can't really assume too much here on a plugin name meaning a specific plugin from a specific author, then this must be configurable to be able to opt-out.

Would be nice with being able to configure printer/parser rules, such as:

# Commands to match printer logic on.
# Matching rules:
# - First one to match will be used.
# - By default args must match exactly.
# - If using "hello" then it will match 1 required arg of value "hello"
# - If using "[hello]" then it will match 1 optional arg of value "hello"
# - If using "<*>" then it will match 1 required arg of any value
# - If using "[*]" then it will match 1 optional arg of any value
# - If using "/.*/" then it will match 1 optional arg that matches regex
# - If using "..." then it will match any number of args of any values.
# - Flags are by default ignored.
# - Specifying a flag makes it required.
commands:
  # match if 1st arg is "krew", has flag "--help", and may have any other number of args
  - match: ['krew --help ...']
    printer: help

  # match if 1st arg is "krew", and has no other args
  - match: ['krew']
    printer: help

  # match if 1st arg is "krew", 2nd arg has any value, and has no other args
  - match: ['krew info <*>']
    printer: table

  # match if 1st arg is "krew", 2nd arg is "search", and 3rd is optional and can have any value
  - match: ['krew search [*]']
    printer: table

  # match if 1st arg is "krew", 2nd is "list", and has no other args
  - match: ['krew list']
    printer: table
    flagsWithValue:
      - --v # says that the --v flag can have a value, so treat "krew list --v debug" as "krew list --v=debug"

  # https://github.com/tohjustin/kube-lineage
  # Matches if --help or -h is used, and any number of arguments
  - match: ['lineage --help ...', 'lineage -h ...']
    printer: help

  # Matches any other lineage commands, like "lineage clusterrole cluster-admin"
  - match: ['lineage ...']
    printer: table

The above config format is me brainstorming a bit. But something as simple as that would still be super useful and quite powerful.

Maybe to speed things up, and to make the config easier to read, it could do nested matches:

commands:
  - match: ['krew ...']
    # defining flagsWithValue here means that it is also applied to child commands
    flagsWithValue:
      - --v # says that the --v flag can have a value, so treat "krew list --v debug" as "krew list --v=debug"
    commands:
      # match if 1st arg is "krew", has flag "--help", and may have any other number of args
      - match: ['krew --help ...']
        printer: help

      # match if 1st arg is "krew", and has no other args
      - match: ['krew']
        printer: help

      # match if 1st arg is "krew", 2nd arg has any value, and has no other args
      - match: ['krew info <*>']
        printer: table

      # match if 1st arg is "krew", 2nd arg is "search", and 3rd is optional and can have any value
      - match: ['krew search [*]']
        printer: table

      # match if 1st arg is "krew", 2nd is "list", and has no other args
      - match: ['krew list']
        printer: table

  # https://github.com/tohjustin/kube-lineage
  - match: ['lineage']
    commands:
      # Matches if --help or -h is used, and any number of arguments
      - match: ['lineage --help ...', 'lineage -h ...']
        printer: help

      # Matches any other lineage commands, like "lineage clusterrole cluster-admin"
      - match: ['lineage ...']
        printer: table

The logic for kubecolor would be to just traverse the list on demand, and it can basically do a tree walk. On the first occurrence of it matching (and that match defines the printer) then use the given printer.

As an implementation detail, this could also be used for the regular kubectl commands too, but let kubecolor add those configs to the end of the commands list so they're only used as fallback.

That way we could probably clean up a bunch of the printer/*.go code as a lot of it is just repetition.

Current Behavior

kubecolor strictly disables coloring if its a kubectl plugin, and has no configurable printer rules.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Operating System: linux
  • kubectl version: v1.29.4
  • kubecolor version: v0.3.1
@applejag applejag added the enhancement New feature or request label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant