Skip to content

Commit

Permalink
Merge pull request #740 from r2d4/completion
Browse files Browse the repository at this point in the history
Add simple bash completion for minikube
  • Loading branch information
dlorenc authored Oct 24, 2016
2 parents 76896c0 + 24bced3 commit 2a47032
Show file tree
Hide file tree
Showing 5 changed files with 1,097 additions and 0 deletions.
96 changes: 96 additions & 0 deletions cmd/minikube/cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"io"
"os"

"github.com/spf13/cobra"
cmdutil "k8s.io/minikube/cmd/util"
)

const longDescription = `
Outputs minikube shell completion for the given shell (bash)
This depends on the bash-completion binary. Example installation instructions:
OS X:
$ brew install bash-completion
$ source $(brew --prefix)/etc/bash_completion
$ source <(minikube completion bash)
Ubuntu:
$ apt-get install bash-completion
$ source /etc/bash-completion
$ source <(minikube completion bash)
Additionally, you may want to output completion to a file and source in your .bashrc
`

const boilerPlate = `
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
`

var completionCmd = &cobra.Command{
Use: "completion SHELL",
Short: "Outputs minikube shell completion for the given shell (bash)",
Long: longDescription,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Usage: minikube completion SHELL")
os.Exit(1)
}
if args[0] != "bash" {
fmt.Println("Only bash is supported for minikube completion")
os.Exit(1)
}
err := GenerateBashCompletion(os.Stdout, cmd.Parent())
if err != nil {
cmdutil.MaybeReportErrorAndExit(err)
}
},
}

func GenerateBashCompletion(w io.Writer, cmd *cobra.Command) error {
_, err := w.Write([]byte(boilerPlate))
if err != nil {
return err
}

err = cmd.GenBashCompletion(w)
if err != nil {
return err
}

return nil
}

func init() {
RootCmd.AddCommand(completionCmd)
}
Loading

0 comments on commit 2a47032

Please sign in to comment.