Skip to content

Commit 392bdc6

Browse files
Merge pull request #75 from tommy-heyde-olsen/refactored-add-exec-support
Adds shell completion for bash, zsh & fish
2 parents 544f344 + 3cfa7fa commit 392bdc6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3014
-1550
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.7.2
1+
v0.8.0

cmd/main.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,6 @@ import (
2424
func main() {
2525
rootCommand := switcher.NewCommandStartSwitcher()
2626

27-
// if first argument is not found, assume it is a context name
28-
// hence call default subcommand
29-
if len(os.Args) > 1 {
30-
cmd, _, err := rootCommand.Find(os.Args[1:])
31-
if err != nil || cmd == nil {
32-
args := append([]string{"set-context"}, os.Args[1:]...)
33-
rootCommand.SetArgs(args)
34-
}
35-
36-
// cobra somehow does not recognize - as a valid command
37-
if os.Args[1] == "-" {
38-
args := append([]string{"set-previous-context"}, os.Args[1:]...)
39-
rootCommand.SetArgs(args)
40-
}
41-
42-
if os.Args[1] == "." {
43-
args := append([]string{"set-last-context"}, os.Args[1:]...)
44-
rootCommand.SetArgs(args)
45-
}
46-
}
47-
4827
if err := rootCommand.Execute(); err != nil {
4928
fmt.Print(err)
5029
os.Exit(1)

cmd/switcher/alias.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2021 The Kubeswitch authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package switcher
15+
16+
import (
17+
"fmt"
18+
"os"
19+
"strings"
20+
21+
"github.com/danielfoehrkn/kubeswitch/pkg/subcommands/alias"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var (
26+
aliasContextCmd = &cobra.Command{
27+
Use: "alias",
28+
Short: "Create an alias for a context. Use ALIAS=CONTEXT_NAME",
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
if len(args) != 1 || !strings.Contains(args[0], "=") || len(strings.Split(args[0], "=")) != 2 {
31+
return fmt.Errorf("please provide the alias in the form ALIAS=CONTEXT_NAME")
32+
}
33+
34+
arguments := strings.Split(args[0], "=")
35+
ctxName, err := resolveContextName(arguments[1])
36+
if err != nil {
37+
return err
38+
}
39+
40+
stores, config, err := initialize()
41+
if err != nil {
42+
return err
43+
}
44+
45+
return alias.Alias(arguments[0], ctxName, stores, config, stateDirectory, noIndex)
46+
},
47+
SilenceErrors: true,
48+
}
49+
50+
aliasLsCmd = &cobra.Command{
51+
Use: "ls",
52+
Short: "List all existing aliases",
53+
Args: cobra.NoArgs,
54+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
55+
return nil, cobra.ShellCompDirectiveNoFileComp
56+
},
57+
RunE: func(cmd *cobra.Command, args []string) error {
58+
return alias.ListAliases(stateDirectory)
59+
},
60+
}
61+
62+
aliasRmCmd = &cobra.Command{
63+
Use: "rm",
64+
Short: "Remove an existing alias",
65+
Args: cobra.ExactArgs(1),
66+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
67+
if len(args) != 0 {
68+
return nil, cobra.ShellCompDirectiveNoFileComp
69+
}
70+
aliases, _ := alias.GetAliases(stateDirectory)
71+
return aliases, cobra.ShellCompDirectiveNoFileComp
72+
},
73+
RunE: func(cmd *cobra.Command, args []string) error {
74+
return alias.RemoveAlias(args[0], stateDirectory)
75+
},
76+
SilenceErrors: true,
77+
}
78+
)
79+
80+
func init() {
81+
aliasRmCmd.Flags().StringVar(
82+
&stateDirectory,
83+
"state-directory",
84+
os.ExpandEnv("$HOME/.kube/switch-state"),
85+
"path to the state directory.")
86+
87+
aliasContextCmd.AddCommand(aliasLsCmd)
88+
aliasContextCmd.AddCommand(aliasRmCmd)
89+
90+
setFlagsForContextCommands(aliasContextCmd)
91+
92+
rootCommand.AddCommand(aliasContextCmd)
93+
}

cmd/switcher/clean.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2021 The Kubeswitch authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package switcher
15+
16+
import (
17+
"github.com/danielfoehrkn/kubeswitch/pkg/subcommands/clean"
18+
"github.com/spf13/cobra"
19+
)
20+
21+
var (
22+
cleanCmd = &cobra.Command{
23+
Use: "clean",
24+
Short: "Cleans all temporary and cached kubeconfig files",
25+
Long: `Cleans the temporary kubeconfig files created in the directory $HOME/.kube/switch_tmp and flushes every cache`,
26+
Args: cobra.NoArgs,
27+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
28+
return nil, cobra.ShellCompDirectiveNoFileComp
29+
},
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
stores, _, err := initialize()
32+
if err != nil {
33+
return err
34+
}
35+
return clean.Clean(stores)
36+
},
37+
}
38+
)
39+
40+
func init() {
41+
rootCommand.AddCommand(cleanCmd)
42+
}

cmd/switcher/completion.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2021 The Kubeswitch authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package switcher
15+
16+
import (
17+
"fmt"
18+
"os"
19+
20+
"github.com/spf13/cobra"
21+
)
22+
23+
var (
24+
setName string
25+
26+
completionCmd = &cobra.Command{
27+
Use: "completion [bash|zsh|fish]",
28+
Short: "generate completion script",
29+
Long: "load the completion script for switch into the current shell",
30+
DisableFlagsInUseLine: true,
31+
ValidArgs: []string{"bash", "zsh", "fish"},
32+
Args: cobra.ExactArgs(1),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
root := cmd.Root()
35+
if setName != "" {
36+
root.Use = setName
37+
}
38+
switch args[0] {
39+
case "bash":
40+
return root.GenBashCompletion(os.Stdout)
41+
case "zsh":
42+
return root.GenZshCompletion(os.Stdout)
43+
case "fish":
44+
return root.GenFishCompletion(os.Stdout, true)
45+
}
46+
return fmt.Errorf("unsupported shell type: %s", args[0])
47+
},
48+
}
49+
)
50+
51+
func init() {
52+
completionCmd.Flags().StringVarP(&setName, "cmd", "c", "", "generate completion for the specified command")
53+
54+
rootCommand.AddCommand(completionCmd)
55+
}

0 commit comments

Comments
 (0)