Skip to content

Commit

Permalink
cmd: flag parsing cleanup
Browse files Browse the repository at this point in the history
- simplify flag parsing logic (fixes #83)
- hide all glog files but -v (regardless of running as plugin or not)
- call GetExecutedVersion only in "version" cmd
  - TODO: maybe remove GetExecutedVersion, not used in any decisions
- unvendor viper (and many of its dependencies) since unused.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
  • Loading branch information
ahmetb committed Oct 12, 2018
1 parent d55e01e commit 38d2ef2
Show file tree
Hide file tree
Showing 132 changed files with 26 additions and 24,000 deletions.
70 changes: 1 addition & 69 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Expand Up @@ -28,10 +28,6 @@
name = "github.com/spf13/cobra"
version = "0.0.3"

[[constraint]]
name = "github.com/spf13/viper"
version = "1.0.2"

[[constraint]]
name = "k8s.io/apimachinery"

Expand Down
43 changes: 10 additions & 33 deletions cmd/krew/cmd/root.go
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

var (
Expand All @@ -38,7 +37,7 @@ var rootCmd = &cobra.Command{
Use: "krew",
Short: "krew is the kubectl plugin manager",
Long: `krew is the kubectl plugin manager.
You can invoke krew through kubectl with: "kubectl plugin [krew] option..."`,
You can invoke krew through kubectl: "kubectl krew [command]..."`,
SilenceUsage: true,
SilenceErrors: true,
}
Expand All @@ -56,39 +55,22 @@ func Execute() {
}

func init() {
cobra.OnInitialize(initConfig)
// Set glog default to stderr
flag.Set("logtostderr", "true")
// Required by glog
flag.Parse()
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
flag.CommandLine.Parse([]string{}) // convince pkg/flag we parsed the flags
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if f.Name != "v" { // hide all glog flags except for -v
pflag.Lookup(f.Name).Hidden = true
}
})
flag.Set("logtostderr", "true") // Set glog default to stderr

paths = environment.MustGetKrewPaths()
if err := ensureDirs(paths.BasePath(),
paths.DownloadPath(),
paths.InstallPath(),
paths.BinPath()); err != nil {
glog.Fatal(err)
}

selfPath, err := os.Executable()
if err != nil {
glog.Fatalf("failed to get the own executable path")
}
if krewVersion, ok, err := environment.GetExecutedVersion(paths.InstallPath(), selfPath, environment.Realpath); err != nil {
glog.Fatalf("failed to find current krew version, err: %v", err)
} else if ok {
krewExecutedVersion = krewVersion
}

setGlogFlags(krewExecutedVersion != "")
}

// setGlogFlags will add glog flags to the CLI. This command can be executed multiple times.
func setGlogFlags(hidden bool) {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// Add glog flags if not run as a plugin.
flag.CommandLine.VisitAll(func(f *flag.Flag) {
pflag.Lookup(f.Name).Hidden = hidden
})
}

func checkIndex(_ *cobra.Command, _ []string) error {
Expand All @@ -109,8 +91,3 @@ func ensureDirs(paths ...string) error {
}
return nil
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.AutomaticEnv()
}
17 changes: 15 additions & 2 deletions cmd/krew/cmd/version.go
Expand Up @@ -18,7 +18,10 @@ import (
"fmt"
"os"

"github.com/GoogleContainerTools/krew/pkg/environment"
"github.com/GoogleContainerTools/krew/pkg/version"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -35,9 +38,19 @@ IndexURI is the URI where the index is updated from.
InstallPath is the base path for all plugin installations.
DownloadPath is the path used to store download binaries.`,
RunE: func(cmd *cobra.Command, args []string) error {
selfPath, err := os.Executable()
if err != nil {
glog.Fatalf("failed to get the own executable path")
}

executedVersion, runningAsPlugin, err := environment.GetExecutedVersion(paths.InstallPath(), selfPath, environment.Realpath)
if err != nil {
return errors.Wrap(err, "failed to find current krew version")
}

conf := [][]string{
{"IsPlugin", fmt.Sprintf("%v", krewExecutedVersion != "")},
{"ExecutedVersion", krewExecutedVersion},
{"IsPlugin", fmt.Sprintf("%v", runningAsPlugin)},
{"ExecutedVersion", executedVersion},
{"GitTag", version.GitTag()},
{"GitCommit", version.GitCommit()},
{"IndexURI", IndexURI},
Expand Down
5 changes: 0 additions & 5 deletions vendor/github.com/fsnotify/fsnotify/.editorconfig

This file was deleted.

6 changes: 0 additions & 6 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

This file was deleted.

30 changes: 0 additions & 30 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

This file was deleted.

52 changes: 0 additions & 52 deletions vendor/github.com/fsnotify/fsnotify/AUTHORS

This file was deleted.

0 comments on commit 38d2ef2

Please sign in to comment.