-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add several --tube-* flags, look for kubectl in $PATH
- Loading branch information
Showing
7 changed files
with
331 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/reconquest/karma-go" | ||
) | ||
|
||
func matchResources(resources []Resource, params *ParamsMatch) ([]Resource, error) { | ||
exp, err := regexp.Compile(params.Query) | ||
if err != nil { | ||
return nil, karma.Format( | ||
err, | ||
"unable to compile regexp: %s", params.Query, | ||
) | ||
} | ||
|
||
matched := []Resource{} | ||
for _, resource := range resources { | ||
if exp.MatchString(resource.Name) { | ||
matched = append(matched, resource) | ||
} | ||
} | ||
|
||
return matched, nil | ||
} | ||
|
||
func completeParams(client string, params *Params) (*Params, error) { | ||
if params.CompleteContext { | ||
contexts, err := parseKubernetesContexts() | ||
if err != nil { | ||
return params, err | ||
} | ||
|
||
completed := complete(contexts, params.Context) | ||
if completed == "" && params.Context != "" { | ||
return params, fmt.Errorf( | ||
"unable to find such context: %s", | ||
params.Context, | ||
) | ||
} | ||
|
||
params.Context = completed | ||
} | ||
|
||
if params.CompleteNamespace { | ||
namespaces, err := requestNamespaces(client, params) | ||
if err != nil { | ||
return params, karma.Format( | ||
err, | ||
"unable to retrieve list of available namespaces", | ||
) | ||
} | ||
|
||
completed := complete(namespaces, params.Namespace) | ||
if completed == "" && params.Context != "" { | ||
return params, fmt.Errorf( | ||
"unable to find such namespace in context %s: %s", | ||
params.Context, | ||
params.Namespace, | ||
) | ||
} | ||
|
||
params.Namespace = completed | ||
} | ||
|
||
return params, nil | ||
} | ||
|
||
func complete(items []string, query string) string { | ||
var partial string | ||
for _, item := range items { | ||
if item == query { | ||
return item | ||
} | ||
|
||
if strings.Contains(item, query) { | ||
partial = item | ||
} | ||
} | ||
|
||
return partial | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
argPrefix = "--tube-" | ||
) | ||
|
||
type Flags struct { | ||
Help bool | ||
Debug bool | ||
Version bool | ||
} | ||
|
||
func parseFlags(raw []string) ([]string, *Flags, error) { | ||
args := make([]string, len(raw)) | ||
copy(args, raw) | ||
|
||
flags := &Flags{} | ||
for i := 0; i < len(args); i++ { | ||
if i == 0 { | ||
continue | ||
} | ||
|
||
arg := args[i] | ||
|
||
if arg == "--" { | ||
break | ||
} | ||
|
||
if strings.HasPrefix(arg, argPrefix) { | ||
suffix := strings.TrimPrefix(arg, argPrefix) | ||
|
||
switch suffix { | ||
case "help": | ||
flags.Help = true | ||
|
||
case "debug": | ||
flags.Debug = true | ||
|
||
case "version": | ||
flags.Version = true | ||
|
||
default: | ||
return raw, nil, fmt.Errorf( | ||
"unexpected flag %q specified, see %shelp", | ||
arg, argPrefix, | ||
) | ||
} | ||
|
||
args = append(args[:i], args[i+1:]...) | ||
i-- | ||
} | ||
} | ||
|
||
return args, flags, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFlags(t *testing.T) { | ||
test := assert.New(t) | ||
|
||
testcases := []struct { | ||
raw []string | ||
after []string | ||
flags *Flags | ||
}{ | ||
{ | ||
[]string{"x", "--version"}, | ||
[]string{"x", "--version"}, | ||
&Flags{}, | ||
}, | ||
{ | ||
[]string{"x", "a", "b", "c%%", "@d", "--", "e", "", "v"}, | ||
[]string{"x", "a", "b", "c%%", "@d", "--", "e", "", "v"}, | ||
&Flags{}, | ||
}, | ||
{ | ||
[]string{"x", "--version", "--tube-version", "@after"}, | ||
[]string{"x", "--version", "@after"}, | ||
&Flags{ | ||
Version: true, | ||
}, | ||
}, | ||
{ | ||
[]string{"x", "--version", "--tube-debug", "@after"}, | ||
[]string{"x", "--version", "@after"}, | ||
&Flags{ | ||
Debug: true, | ||
}, | ||
}, | ||
{ | ||
[]string{"x", "--version", "--tube-help", "@after"}, | ||
[]string{"x", "--version", "@after"}, | ||
&Flags{ | ||
Help: true, | ||
}, | ||
}, | ||
{ | ||
[]string{"x", "--version", "--tube-help"}, | ||
[]string{"x", "--version"}, | ||
&Flags{ | ||
Help: true, | ||
}, | ||
}, | ||
{ | ||
[]string{"x", "--version", "--tube-help", "--tube-version", "--tube-debug"}, | ||
[]string{"x", "--version"}, | ||
&Flags{ | ||
Help: true, | ||
Version: true, | ||
Debug: true, | ||
}, | ||
}, | ||
{ | ||
[]string{"x", "--version", "--", "--tube-help"}, | ||
[]string{"x", "--version", "--", "--tube-help"}, | ||
&Flags{}, | ||
}, | ||
{ | ||
[]string{"x", "--tube-unexpected"}, | ||
[]string{"x", "--tube-unexpected"}, | ||
nil, | ||
}, | ||
} | ||
|
||
for _, testcase := range testcases { | ||
args, flags, err := parseFlags(testcase.raw) | ||
if !test.EqualValues(testcase.flags, flags, "%q", testcase.raw) { | ||
t.FailNow() | ||
} | ||
|
||
if !test.EqualValues(testcase.after, args, "%q", testcase.raw) { | ||
t.FailNow() | ||
} | ||
|
||
if testcase.flags == nil { | ||
if !test.Error(err) { | ||
t.FailNow() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.