Skip to content
Permalink
Browse files

cmd: flag parsing cleanup (#94)

- 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.
  • Loading branch information...
ahmetb committed Oct 14, 2018
1 parent 3e4b6e0 commit cde8c631847d9766c3d98fc3fdb002fa97278c19
Showing with 39 additions and 24,005 deletions.
  1. +1 βˆ’69 Gopkg.lock
  2. +0 βˆ’4 Gopkg.toml
  3. +12 βˆ’36 cmd/krew/cmd/root.go
  4. +10 βˆ’1 cmd/krew/cmd/upgrade.go
  5. +15 βˆ’2 cmd/krew/cmd/version.go
  6. +1 βˆ’1 hack/make-release-artifacts.sh
  7. +0 βˆ’5 vendor/github.com/fsnotify/fsnotify/.editorconfig
  8. +0 βˆ’6 vendor/github.com/fsnotify/fsnotify/.gitignore
  9. +0 βˆ’30 vendor/github.com/fsnotify/fsnotify/.travis.yml
  10. +0 βˆ’52 vendor/github.com/fsnotify/fsnotify/AUTHORS
  11. +0 βˆ’317 vendor/github.com/fsnotify/fsnotify/CHANGELOG.md
  12. +0 βˆ’77 vendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md
  13. +0 βˆ’28 vendor/github.com/fsnotify/fsnotify/LICENSE
  14. +0 βˆ’79 vendor/github.com/fsnotify/fsnotify/README.md
  15. +0 βˆ’37 vendor/github.com/fsnotify/fsnotify/fen.go
  16. +0 βˆ’66 vendor/github.com/fsnotify/fsnotify/fsnotify.go
  17. +0 βˆ’337 vendor/github.com/fsnotify/fsnotify/inotify.go
  18. +0 βˆ’187 vendor/github.com/fsnotify/fsnotify/inotify_poller.go
  19. +0 βˆ’521 vendor/github.com/fsnotify/fsnotify/kqueue.go
  20. +0 βˆ’11 vendor/github.com/fsnotify/fsnotify/open_mode_bsd.go
  21. +0 βˆ’12 vendor/github.com/fsnotify/fsnotify/open_mode_darwin.go
  22. +0 βˆ’561 vendor/github.com/fsnotify/fsnotify/windows.go
  23. +0 βˆ’9 vendor/github.com/hashicorp/hcl/.gitignore
  24. +0 βˆ’13 vendor/github.com/hashicorp/hcl/.travis.yml
  25. +0 βˆ’354 vendor/github.com/hashicorp/hcl/LICENSE
  26. +0 βˆ’18 vendor/github.com/hashicorp/hcl/Makefile
  27. +0 βˆ’125 vendor/github.com/hashicorp/hcl/README.md
  28. +0 βˆ’19 vendor/github.com/hashicorp/hcl/appveyor.yml
  29. +0 βˆ’729 vendor/github.com/hashicorp/hcl/decoder.go
  30. +0 βˆ’11 vendor/github.com/hashicorp/hcl/hcl.go
  31. +0 βˆ’219 vendor/github.com/hashicorp/hcl/hcl/ast/ast.go
  32. +0 βˆ’52 vendor/github.com/hashicorp/hcl/hcl/ast/walk.go
  33. +0 βˆ’17 vendor/github.com/hashicorp/hcl/hcl/parser/error.go
  34. +0 βˆ’532 vendor/github.com/hashicorp/hcl/hcl/parser/parser.go
  35. +0 βˆ’789 vendor/github.com/hashicorp/hcl/hcl/printer/nodes.go
  36. +0 βˆ’66 vendor/github.com/hashicorp/hcl/hcl/printer/printer.go
  37. +0 βˆ’652 vendor/github.com/hashicorp/hcl/hcl/scanner/scanner.go
  38. +0 βˆ’241 vendor/github.com/hashicorp/hcl/hcl/strconv/quote.go
  39. +0 βˆ’46 vendor/github.com/hashicorp/hcl/hcl/token/position.go
  40. +0 βˆ’219 vendor/github.com/hashicorp/hcl/hcl/token/token.go
  41. +0 βˆ’117 vendor/github.com/hashicorp/hcl/json/parser/flatten.go
  42. +0 βˆ’313 vendor/github.com/hashicorp/hcl/json/parser/parser.go
  43. +0 βˆ’451 vendor/github.com/hashicorp/hcl/json/scanner/scanner.go
  44. +0 βˆ’46 vendor/github.com/hashicorp/hcl/json/token/position.go
  45. +0 βˆ’118 vendor/github.com/hashicorp/hcl/json/token/token.go
  46. +0 βˆ’38 vendor/github.com/hashicorp/hcl/lex.go
  47. +0 βˆ’39 vendor/github.com/hashicorp/hcl/parse.go
  48. +0 βˆ’6 vendor/github.com/magiconair/properties/.gitignore
  49. +0 βˆ’10 vendor/github.com/magiconair/properties/.travis.yml
  50. +0 βˆ’131 vendor/github.com/magiconair/properties/CHANGELOG.md
  51. +0 βˆ’25 vendor/github.com/magiconair/properties/LICENSE
  52. +0 βˆ’129 vendor/github.com/magiconair/properties/README.md
  53. +0 βˆ’289 vendor/github.com/magiconair/properties/decode.go
  54. +0 βˆ’156 vendor/github.com/magiconair/properties/doc.go
  55. +0 βˆ’34 vendor/github.com/magiconair/properties/integrate.go
  56. +0 βˆ’407 vendor/github.com/magiconair/properties/lex.go
  57. +0 βˆ’292 vendor/github.com/magiconair/properties/load.go
  58. +0 βˆ’95 vendor/github.com/magiconair/properties/parser.go
  59. +0 βˆ’833 vendor/github.com/magiconair/properties/properties.go
  60. +0 βˆ’31 vendor/github.com/magiconair/properties/rangecheck.go
  61. +0 βˆ’8 vendor/github.com/mitchellh/mapstructure/.travis.yml
  62. +0 βˆ’21 vendor/github.com/mitchellh/mapstructure/LICENSE
  63. +0 βˆ’46 vendor/github.com/mitchellh/mapstructure/README.md
  64. +0 βˆ’171 vendor/github.com/mitchellh/mapstructure/decode_hooks.go
  65. +0 βˆ’50 vendor/github.com/mitchellh/mapstructure/error.go
  66. +0 βˆ’1,061 vendor/github.com/mitchellh/mapstructure/mapstructure.go
  67. +0 βˆ’2 vendor/github.com/pelletier/go-toml/.gitignore
  68. +0 βˆ’22 vendor/github.com/pelletier/go-toml/.travis.yml
  69. +0 βˆ’21 vendor/github.com/pelletier/go-toml/LICENSE
  70. +0 βˆ’131 vendor/github.com/pelletier/go-toml/README.md
  71. +0 βˆ’164 vendor/github.com/pelletier/go-toml/benchmark.json
  72. +0 βˆ’32 vendor/github.com/pelletier/go-toml/benchmark.sh
  73. +0 βˆ’244 vendor/github.com/pelletier/go-toml/benchmark.toml
  74. +0 βˆ’121 vendor/github.com/pelletier/go-toml/benchmark.yml
  75. +0 βˆ’23 vendor/github.com/pelletier/go-toml/doc.go
  76. +0 βˆ’29 vendor/github.com/pelletier/go-toml/example-crlf.toml
  77. +0 βˆ’29 vendor/github.com/pelletier/go-toml/example.toml
  78. +0 βˆ’31 vendor/github.com/pelletier/go-toml/fuzz.go
  79. +0 βˆ’15 vendor/github.com/pelletier/go-toml/fuzz.sh
  80. +0 βˆ’85 vendor/github.com/pelletier/go-toml/keysparsing.go
  81. +0 βˆ’750 vendor/github.com/pelletier/go-toml/lexer.go
  82. +0 βˆ’600 vendor/github.com/pelletier/go-toml/marshal.go
  83. +0 βˆ’38 vendor/github.com/pelletier/go-toml/marshal_test.toml
  84. +0 βˆ’430 vendor/github.com/pelletier/go-toml/parser.go
  85. +0 βˆ’29 vendor/github.com/pelletier/go-toml/position.go
  86. +0 βˆ’91 vendor/github.com/pelletier/go-toml/test.sh
  87. +0 βˆ’144 vendor/github.com/pelletier/go-toml/token.go
  88. +0 βˆ’309 vendor/github.com/pelletier/go-toml/toml.go
  89. +0 βˆ’142 vendor/github.com/pelletier/go-toml/tomltree_create.go
  90. +0 βˆ’289 vendor/github.com/pelletier/go-toml/tomltree_write.go
  91. +0 βˆ’21 vendor/github.com/spf13/afero/.travis.yml
  92. +0 βˆ’174 vendor/github.com/spf13/afero/LICENSE.txt
  93. +0 βˆ’452 vendor/github.com/spf13/afero/README.md
  94. +0 βˆ’108 vendor/github.com/spf13/afero/afero.go
  95. +0 βˆ’15 vendor/github.com/spf13/afero/appveyor.yml
  96. +0 βˆ’180 vendor/github.com/spf13/afero/basepath.go
  97. +0 βˆ’290 vendor/github.com/spf13/afero/cacheOnReadFs.go
  98. +0 βˆ’22 vendor/github.com/spf13/afero/const_bsds.go
  99. +0 βˆ’25 vendor/github.com/spf13/afero/const_win_unix.go
  100. +0 βˆ’292 vendor/github.com/spf13/afero/copyOnWriteFs.go
  101. +0 βˆ’110 vendor/github.com/spf13/afero/httpFs.go
  102. +0 βˆ’230 vendor/github.com/spf13/afero/ioutil.go
  103. +0 βˆ’27 vendor/github.com/spf13/afero/lstater.go
  104. +0 βˆ’110 vendor/github.com/spf13/afero/match.go
  105. +0 βˆ’37 vendor/github.com/spf13/afero/mem/dir.go
  106. +0 βˆ’43 vendor/github.com/spf13/afero/mem/dirmap.go
  107. +0 βˆ’314 vendor/github.com/spf13/afero/mem/file.go
  108. +0 βˆ’365 vendor/github.com/spf13/afero/memmap.go
  109. +0 βˆ’101 vendor/github.com/spf13/afero/os.go
  110. +0 βˆ’106 vendor/github.com/spf13/afero/path.go
  111. +0 βˆ’80 vendor/github.com/spf13/afero/readonlyfs.go
  112. +0 βˆ’214 vendor/github.com/spf13/afero/regexpfs.go
  113. +0 βˆ’305 vendor/github.com/spf13/afero/unionFile.go
  114. +0 βˆ’330 vendor/github.com/spf13/afero/util.go
  115. +0 βˆ’25 vendor/github.com/spf13/cast/.gitignore
  116. +0 βˆ’14 vendor/github.com/spf13/cast/.travis.yml
  117. +0 βˆ’21 vendor/github.com/spf13/cast/LICENSE
  118. +0 βˆ’38 vendor/github.com/spf13/cast/Makefile
  119. +0 βˆ’75 vendor/github.com/spf13/cast/README.md
  120. +0 βˆ’159 vendor/github.com/spf13/cast/cast.go
  121. +0 βˆ’1,166 vendor/github.com/spf13/cast/caste.go
  122. +0 βˆ’22 vendor/github.com/spf13/jwalterweatherman/.gitignore
  123. +0 βˆ’21 vendor/github.com/spf13/jwalterweatherman/LICENSE
  124. +0 βˆ’148 vendor/github.com/spf13/jwalterweatherman/README.md
  125. +0 βˆ’113 vendor/github.com/spf13/jwalterweatherman/default_notepad.go
  126. +0 βˆ’55 vendor/github.com/spf13/jwalterweatherman/log_counter.go
  127. +0 βˆ’194 vendor/github.com/spf13/jwalterweatherman/notepad.go
  128. +0 βˆ’24 vendor/github.com/spf13/viper/.gitignore
  129. +0 βˆ’28 vendor/github.com/spf13/viper/.travis.yml
  130. +0 βˆ’21 vendor/github.com/spf13/viper/LICENSE
  131. +0 βˆ’643 vendor/github.com/spf13/viper/README.md
  132. +0 βˆ’57 vendor/github.com/spf13/viper/flags.go
  133. +0 βˆ’221 vendor/github.com/spf13/viper/util.go
  134. +0 βˆ’1,771 vendor/github.com/spf13/viper/viper.go

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -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"

@@ -26,20 +26,18 @@ import (
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

var (
paths environment.Paths // krew paths used by the process
krewExecutedVersion string // resolved version of krew
paths environment.Paths // krew paths used by the process
)

// rootCmd represents the base command when called without any subcommands
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,
}
@@ -57,46 +55,29 @@ 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 {
if ok, err := gitutil.IsGitCloned(paths.IndexPath()); err != nil {
return errors.Wrap(err, "failed to check local index git repository")
} else if !ok {
return errors.New(`krew local plugin index is not initialized (run "krew update")`)
return errors.New(`krew local plugin index is not initialized (run "kubectl krew update")`)
}
return nil
}
@@ -114,8 +95,3 @@ func ensureDirs(paths ...string) error {
func isTerminal(f *os.File) bool {
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.AutomaticEnv()
}
@@ -18,6 +18,7 @@ import (
"fmt"
"os"

"github.com/GoogleContainerTools/krew/pkg/environment"
"github.com/GoogleContainerTools/krew/pkg/index/indexscanner"
"github.com/GoogleContainerTools/krew/pkg/installation"

@@ -53,14 +54,22 @@ kubectl plugin upgrade foo bar"`,
pluginNames = args
}

execPath, err := os.Executable()
if err != nil {
return errors.Wrap(err, "could not get krew's own executable path")
}
executedKrewVersion, _, err := environment.GetExecutedVersion(paths.InstallPath(), execPath, environment.Realpath)
if err != nil {
return errors.Wrap(err, "failed to find current krew version")
}
for _, name := range pluginNames {
plugin, err := indexscanner.LoadPluginFileFromFS(paths.IndexPath(), name)
if err != nil {
return errors.Wrapf(err, "failed to load the index file for plugin %s", plugin.Name)
}

glog.V(2).Infof("Upgrading plugin: %s\n", plugin.Name)
err = installation.Upgrade(paths, plugin, krewExecutedVersion)
err = installation.Upgrade(paths, plugin, executedKrewVersion)
if ignoreUpgraded && err == installation.ErrIsAlreadyUpgraded {
fmt.Fprintf(os.Stderr, "Skipping plugin %s, it is already on the newest version\n", plugin.Name)
continue
@@ -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"
)

@@ -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},
@@ -41,7 +41,7 @@ echo >&2 "Written ${sumfile}."

# Copy and process krew manifest
cp ./hack/krew.yaml ./out/krew.yaml
tag="$(git describe --tags HEAD)"
tag="$(git describe --tags --always HEAD)"
sed -i "s/KREW_ZIP_CHECKSUM/${zip_checksum}/g" ./out/krew.yaml
sed -i "s/KREW_TAG/${tag}/g" ./out/krew.yaml
echo >&2 "Written krew.yaml."

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit cde8c63

Please sign in to comment.
You can’t perform that action at this time.