Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Disable cobra flags after kubectl sub-command #621

Merged
merged 2 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/tarmak/cmd/cluster_instances_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

var clusterInstancesSshCmd = &cobra.Command{
Use: "ssh [instance alias]",
Use: "ssh [instance alias] [optional ssh command]",
Short: "Log into an instance with SSH",
Run: func(cmd *cobra.Command, args []string) {
t := tarmak.New(globalFlags)
defer t.Cleanup()
t.SSHPassThrough(args)
},
DisableFlagsInUseLine: true,
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions cmd/tarmak/cmd/cluster_kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var clusterKubectlCmd = &cobra.Command{
t := tarmak.New(globalFlags)
t.Perform(t.NewCmdTarmak(cmd.Flags(), args).Kubectl())
},
DisableFlagsInUseLine: true,
}

func init() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/tarmak/cmd/cluster_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

var clusterSshCmd = &cobra.Command{
Use: "ssh [instance alias]",
Use: "ssh [instance alias] [optional ssh command]",
Short: "Log into an instance with SSH",
Run: func(cmd *cobra.Command, args []string) {
t := tarmak.New(globalFlags)
defer t.Cleanup()
t.SSHPassThrough(args)
},
DisableFlagsInUseLine: true,
}

func init() {
Expand Down
43 changes: 23 additions & 20 deletions cmd/tarmak/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"

tarmakv1alpha1 "github.com/jetstack/tarmak/pkg/apis/tarmak/v1alpha1"
"github.com/jetstack/tarmak/pkg/tarmak/utils"
"github.com/jetstack/tarmak/pkg/tarmak/utils/consts"
"github.com/jetstack/tarmak/pkg/terraform"
)
Expand Down Expand Up @@ -39,20 +40,31 @@ func Execute(args []string) {
}
}

RootCmd.SetArgs(args)
cmd, _, err := RootCmd.Traverse(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// evalutate command that is gonna be run
command, commandArgs, err := RootCmd.Traverse(args)
for _, c := range []struct {
use string
name string
}{
{use: clusterKubectlCmd.Use, name: "kubectl"},
{use: clusterSshCmd.Use, name: "ssh"},
} {
if cmd.Use != c.use {
continue
}

// escape pass through commands (kubectl, ssh) if necessary
if err == nil && (command.Use == clusterKubectlCmd.Use || command.Use == clusterSshCmd.Use) {
// if no escape exists already add one
if !stringSliceContains(commandArgs, "--") {
pos := len(args) - len(commandArgs)
newArgs := append(args[:pos], append([]string{"--"}, args[pos:]...)...)
RootCmd.SetArgs(newArgs)
// this line helps debugging fmt.Printf("rewriting args\noriginal args=%v\ncommand args=%v\nnew args=%v\n", args, commandArgs, newArgs)
i := utils.IndexOfString(args, c.name)
if i == -1 {
break
}

RootCmd.SetArgs(
append(args[:i+1], append([]string{"--"}, args[i+1:]...)...))
break
}

if err := RootCmd.Execute(); err != nil {
Expand All @@ -61,15 +73,6 @@ func Execute(args []string) {
}
}

func stringSliceContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func init() {
// set default tarmak config folder
tarmakConfigPath := "~/.tarmak"
Expand Down
106 changes: 106 additions & 0 deletions cmd/tarmak/cmd/tarmak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright Jetstack Ltd. See LICENSE for details.
package cmd

import (
"sync"
"testing"

"github.com/spf13/cobra"
)

const (
globalFlag = "--current-cluster=xxx"
)

type cmdTest struct {
*testing.T
cmd *cobra.Command
argsCh chan string
args []string
}

func newCmdTest(t *testing.T, cmd *cobra.Command) *cmdTest {
return &cmdTest{
T: t,
cmd: cmd,
}
}

func (c *cmdTest) testRunFunc() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
for _, a := range args {
c.argsCh <- a
}
close(c.argsCh)
}
}

func (c *cmdTest) flagIgnored(exp bool) {
c.argsCh = make(chan string)
c.cmd.Run = c.testRunFunc()

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

found := false
for a := range c.argsCh {
if a == globalFlag {
found = true
if !exp {
c.Errorf("got global flag as argument but expected not to: %s [%s]", c.args, globalFlag)
}
}
}

if !found && exp {
c.Errorf("did not receive flag in arguments but was expecting to: %s [%s]", c.args, globalFlag)
}
}()

Execute(c.args[1:])

wg.Wait()
}

func Test_KubectlParsing(t *testing.T) {
c := newCmdTest(t, clusterKubectlCmd)
for _, a := range [][]string{
{"tarmak", "kubectl", globalFlag, "arg", "arg"},
{"tarmak", "kubectl", "arg", globalFlag, "arg"},
{"tarmak", "cluster", "kubectl", globalFlag, "arg", "arg"},
{"tarmak", "cluster", "kubectl", "arg", globalFlag, "arg"},
} {
c.args = a
c.flagIgnored(true)
}

for _, a := range [][]string{
{"tarmak", globalFlag, "kubectl", "arg", "arg"},
{"tarmak", globalFlag, "cluster", "kubectl", "arg", "arg"},
{"tarmak", "cluster", globalFlag, "kubectl", "arg", "arg"},
} {
c.args = a
c.flagIgnored(false)
}
}

func Test_SSHParsing(t *testing.T) {
c := newCmdTest(t, clusterSshCmd)
for _, a := range [][]string{
{"tarmak", "cluster", "ssh", "arg", globalFlag, "arg"},
{"tarmak", "cluster", "ssh", globalFlag, "arg", "arg"},
} {
c.args = a
c.flagIgnored(true)
}

for _, a := range [][]string{
{"tarmak", globalFlag, "cluster", "ssh", "arg", "arg"},
{"tarmak", "cluster", globalFlag, "ssh", "arg", "arg"},
} {
c.args = a
c.flagIgnored(false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Log into an instance with SSH

::

tarmak clusters instances ssh [instance alias] [flags]
tarmak clusters instances ssh [instance alias] [optional ssh command]

Options
~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/cmd/tarmak/tarmak_clusters_ssh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Log into an instance with SSH

::

tarmak clusters ssh [instance alias] [flags]
tarmak clusters ssh [instance alias] [optional ssh command]

Options
~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/cmd/tarmak/tarmak_kubectl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Run kubectl on the current cluster

::

tarmak kubectl [kubectl command arguments] [flags]
tarmak kubectl [kubectl command arguments]

Options
~~~~~~~
Expand Down
10 changes: 10 additions & 0 deletions pkg/tarmak/utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ func SliceContains(slice []string, str string) bool {

return false
}

func IndexOfString(slice []string, str string) int {
for i, s := range slice {
if s == str {
return i
}
}

return -1
}