Skip to content

Commit

Permalink
Fix race conditions
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Mar 20, 2019
1 parent f826c4b commit c48c484
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
38 changes: 20 additions & 18 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,36 @@ var (
v string
defaultColor int
overwrite bool

updateMsg = make(chan string)
)

var rootCmd = &cobra.Command{
Use: "skaffold",
Short: "A tool that facilitates continuous development for Kubernetes applications.",
}

func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
updateMsg := make(chan string)

rootCmd := &cobra.Command{
Use: "skaffold",
Short: "A tool that facilitates continuous development for Kubernetes applications.",
SilenceErrors: true,
}

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := SetUpLogs(err, v); err != nil {
return err
}

rootCmd.SilenceUsage = true
logrus.Infof("Skaffold %+v", version.Get())
go func() {
if err := updateCheck(updateMsg); err != nil {
logrus.Infof("update check failed: %s", err)
}
}()

color.OverwriteDefault(color.Color(defaultColor))

if quietFlag {
logrus.Debugf("Update check is disabled because of quiet mode")
} else {
go func() {
if err := updateCheck(updateMsg); err != nil {
logrus.Infof("update check failed: %s", err)
}
}()
}

return nil
}

Expand All @@ -72,7 +79,6 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
}
}

rootCmd.SilenceErrors = true
rootCmd.SetOutput(out)
rootCmd.AddCommand(NewCmdCompletion(out))
rootCmd.AddCommand(NewCmdVersion(out))
Expand All @@ -95,10 +101,6 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
}

func updateCheck(ch chan string) error {
if quietFlag {
logrus.Debugf("Update check is disabled because of quiet mode")
return nil
}
if !update.IsUpdateCheckEnabled() {
logrus.Debugf("Update check not enabled, skipping.")
return nil
Expand Down
46 changes: 26 additions & 20 deletions cmd/skaffold/app/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,38 +218,44 @@ _complete skaffold 2>/dev/null
`
)

var completionCmd = &cobra.Command{
Use: "completion SHELL",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires 1 arg, found %d", len(args))
}
return cobra.OnlyValidArgs(cmd, args)
},
ValidArgs: []string{"bash", "zsh"},
Short: "Output shell completion for the given shell (bash or zsh)",
Long: longDescription,
Run: completion,
}

func completion(_cmd *cobra.Command, args []string) {
func completion(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
rootCmd.GenBashCompletion(os.Stdout)
rootCmd(cmd).GenBashCompletion(os.Stdout)
case "zsh":
runCompletionZsh(os.Stdout)
runCompletionZsh(cmd, os.Stdout)
}
}

// NewCmdCompletion returns the cobra command that outputs shell completion code
func NewCmdCompletion(out io.Writer) *cobra.Command {
return completionCmd
return &cobra.Command{
Use: "completion SHELL",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires 1 arg, found %d", len(args))
}
return cobra.OnlyValidArgs(cmd, args)
},
ValidArgs: []string{"bash", "zsh"},
Short: "Output shell completion for the given shell (bash or zsh)",
Long: longDescription,
Run: completion,
}
}

func runCompletionZsh(out io.Writer) {
func runCompletionZsh(cmd *cobra.Command, out io.Writer) {
io.WriteString(out, zshInitialization)
buf := new(bytes.Buffer)
rootCmd.GenBashCompletion(buf)
rootCmd(cmd).GenBashCompletion(buf)
out.Write(buf.Bytes())
io.WriteString(out, zshTail)
}

func rootCmd(cmd *cobra.Command) *cobra.Command {
parent := cmd
for parent.HasParent() {
parent = parent.Parent()
}
return parent
}

0 comments on commit c48c484

Please sign in to comment.