-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move from `flag` to `kingpin` - Add tests - Add comments
- Loading branch information
1 parent
4993f86
commit 877af50
Showing
5 changed files
with
237 additions
and
56 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,72 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
"text/template" | ||
"time" | ||
|
||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
const helpTemplate = `Purpose: Create a Kubernetes job and watch the logs until it completes. | ||
Examples: | ||
# Read job spec from file: | ||
{{ .app }} -n test job.yaml | ||
# Read job spec from stdin | ||
cat job.yaml | {{ .app }} -n test | ||
` | ||
|
||
type Args struct { | ||
Kubeconfig string | ||
Namespace string | ||
JobFile string | ||
Timeout time.Duration | ||
} | ||
|
||
func Parse(args []string, home, version string, out io.Writer) (*Args, error) { | ||
var help bytes.Buffer | ||
err := template.Must(template.New("help").Parse(helpTemplate)).Execute(&help, map[string]string{"app": args[0]}) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to format help: %v", err) | ||
} | ||
|
||
app := kingpin.New(args[0], "Run Kubernetes jobs and wait for their completion.") | ||
app.Version(version) | ||
app.Help = help.String() | ||
|
||
jobFile := app.Arg("JOBFILE", "Job spec file, - for stdin (default)").Default("-").String() | ||
namespace := app.Flag("namespace", "Kubernetes namespace to use").Short('n').Required().String() | ||
timeout := app.Flag("timeout", "Timeout in time.Duration format (eg. 10s, 1m, 1h, ...)").Short('t').Duration() | ||
|
||
var kubeconfig *string | ||
if home != "" { | ||
kubeconfig = app.Flag("kubeconfig", "(optional) absolute path to the Kubeconfig file").Default(filepath.Join(home, ".kube", "config")).String() | ||
} else { | ||
kubeconfig = app.Flag("kubeconfig", "absolute path to the Kubeconfig file").Required().String() | ||
} | ||
|
||
// do not call os.Exit() on error | ||
app.Terminate(nil) | ||
|
||
// redirect output | ||
app.ErrorWriter(out) | ||
app.UsageWriter(out) | ||
|
||
_, err = app.Parse(args[1:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Args{ | ||
Kubeconfig: *kubeconfig, | ||
Namespace: *namespace, | ||
JobFile: *jobFile, | ||
Timeout: *timeout, | ||
}, 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,78 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
data := []struct { | ||
home string | ||
args []string | ||
expectedRes Args | ||
expectedErr bool | ||
outContains string | ||
}{ | ||
{ | ||
"/foo/bar", | ||
[]string{"kubejob", "-n", "foo"}, | ||
Args{ | ||
Kubeconfig: "/foo/bar/.kube/config", | ||
Namespace: "foo", | ||
JobFile: "-", | ||
}, | ||
false, | ||
"", | ||
}, | ||
{ | ||
"/foo/bar", | ||
[]string{"kubejob", "-n", "foo", "/fizz/buzz"}, | ||
Args{ | ||
Kubeconfig: "/foo/bar/.kube/config", | ||
Namespace: "foo", | ||
JobFile: "/fizz/buzz", | ||
}, | ||
false, | ||
"", | ||
}, | ||
{ | ||
"/foo/bar", | ||
[]string{"kubejob", "--help"}, | ||
Args{}, | ||
true, | ||
"", | ||
}, | ||
{ | ||
"", | ||
[]string{"kubejob", "--version"}, | ||
Args{}, | ||
true, | ||
"42-23-73", | ||
}, | ||
} | ||
|
||
for _, d := range data { | ||
var buf bytes.Buffer | ||
res, err := Parse(d.args, d.home, "42-23-73", &buf) | ||
if d.expectedErr { | ||
if err == nil { | ||
t.Fatalf("%v: missing expected error", d) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Fatalf("%v: unexpected error: %v", d, err) | ||
} | ||
} | ||
if !d.expectedErr && !reflect.DeepEqual(res, &d.expectedRes) { | ||
t.Fatalf("%v: unexpected result: %v", d, res) | ||
} | ||
if d.outContains != "" { | ||
if !strings.Contains(buf.String(), d.outContains) { | ||
t.Log(buf.String()) | ||
t.Fatal("output does not contain expected string: ", d.outContains) | ||
} | ||
} | ||
} | ||
} |
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,30 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestLabelSelector(t *testing.T) { | ||
data := []struct { | ||
in map[string]string | ||
out string | ||
}{ | ||
{ | ||
map[string]string{}, | ||
"", | ||
}, | ||
{ | ||
map[string]string{"foo": "bar"}, | ||
"foo=bar", | ||
}, | ||
{ | ||
map[string]string{"foo": "bar", "fizz": "buzz"}, | ||
"foo=bar,fizz=buzz", | ||
}, | ||
} | ||
|
||
for _, d := range data { | ||
out := labelSelector(d.in) | ||
if out != d.out { | ||
t.Fatalf("%v: unexpected result: %s", d, out) | ||
} | ||
} | ||
} |