Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #58 from dty1er/dty1er/ver
Browse files Browse the repository at this point in the history
enable to print out kubecolor version
  • Loading branch information
Hidetatsu Yaginuma authored Apr 27, 2021
2 parents 6020a1e + 1aacaf3 commit 5e2080d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ builds:
- id: kubecolor
main: ./cmd/kubecolor/main.go
binary: kubecolor
ldflags:
- -s -w -trimpath
- -X main.Version={{.Version}}
goos:
- windows
- darwin
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ alias kubectl="kubecolor"

### Flags

* `--kubecolor-version`

Prints the version of kubecolor (not kubectl one).

* `--plain`

When you don't want to colorize output, you can specify `--plain`. Kubecolor understands this option and
Expand Down
5 changes: 4 additions & 1 deletion cmd/kubecolor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"github.com/dty1er/kubecolor/command"
)

// this is overridden on build time by GoReleaser
var Version string = "unset"

func main() {
err := command.Run(os.Args[1:])
err := command.Run(os.Args[1:], Version)
if err != nil {
var ke *command.KubectlError
if errors.As(err, &ke) {
Expand Down
19 changes: 11 additions & 8 deletions command/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package command
import "os"

type KubecolorConfig struct {
Plain bool
DarkBackground bool
ForceColor bool
KubectlCmd string
Plain bool
DarkBackground bool
ForceColor bool
ShowKubecolorVersion bool
KubectlCmd string
}

func ResolveConfig(args []string) ([]string, *KubecolorConfig) {
args, plainFlagFound := findAndRemoveBoolFlagIfExists(args, "--plain")
args, lightBackgroundFlagFound := findAndRemoveBoolFlagIfExists(args, "--light-background")
args, forceColorFlagFound := findAndRemoveBoolFlagIfExists(args, "--force-colors")
args, kubecolorVersionFlagFound := findAndRemoveBoolFlagIfExists(args, "--kubecolor-version")

darkBackground := !lightBackgroundFlagFound

Expand All @@ -22,10 +24,11 @@ func ResolveConfig(args []string) ([]string, *KubecolorConfig) {
}

return args, &KubecolorConfig{
Plain: plainFlagFound,
DarkBackground: darkBackground,
ForceColor: forceColorFlagFound,
KubectlCmd: kubectlCmd,
Plain: plainFlagFound,
DarkBackground: darkBackground,
ForceColor: forceColorFlagFound,
ShowKubecolorVersion: kubecolorVersionFlagFound,
KubectlCmd: kubectlCmd,
}
}

Expand Down
7 changes: 6 additions & 1 deletion command/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ var getPrinters = func(subcommandInfo *kubectl.SubcommandInfo, darkBackground bo
}
}

func Run(args []string) error {
func Run(args []string, version string) error {
args, config := ResolveConfig(args)
shouldColorize, subcommandInfo := ResolveSubcommand(args, config)

if config.ShowKubecolorVersion {
fmt.Fprintf(os.Stdout, "%s\n", version)
return nil
}

cmd := exec.Command(config.KubectlCmd, args...)
cmd.Stdin = os.Stdin

Expand Down

0 comments on commit 5e2080d

Please sign in to comment.