Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rufio options #6741

Merged
merged 15 commits into from
Oct 13, 2023
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
66 changes: 66 additions & 0 deletions cmd/eksctl-anywhere/cmd/aflag/aflag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package aflag is the eks anywhere flag handling package.
package aflag

import (
"github.com/spf13/pflag"
)

// Flag defines a CLI flag.
type Flag[T any] struct {
Name string
Short string
Usage string
Default T
}

// String applies f to fs and writes the value to dst.
func String(f Flag[string], dst *string, fs *pflag.FlagSet) {
switch {
// With short form
case f.Short != "":
fs.StringVarP(dst, f.Name, f.Short, f.Default, f.Usage)
// Without short form
default:
fs.StringVar(dst, f.Name, f.Default, f.Usage)
}
}

// Bool applies f to fs and writes the value to dst.
func Bool(f Flag[bool], dst *bool, fs *pflag.FlagSet) {
switch {
case f.Short != "":
fs.BoolVarP(dst, f.Name, f.Short, f.Default, f.Usage)
default:
fs.BoolVar(dst, f.Name, f.Default, f.Usage)
}
}

// StringSlice applies f to fs and writes the value to dst.
func StringSlice(f Flag[[]string], dst *[]string, fs *pflag.FlagSet) {
switch {
case f.Short != "":
fs.StringSliceVarP(dst, f.Name, f.Short, f.Default, f.Usage)
default:
fs.StringSliceVar(dst, f.Name, f.Default, f.Usage)
}
}

// StringString applies f to fs and writes the value to dst.
func StringString(f Flag[map[string]string], dst *map[string]string, fs *pflag.FlagSet) {
switch {
case f.Short != "":
fs.StringToStringVarP(dst, f.Name, f.Short, f.Default, f.Usage)
default:
fs.StringToStringVar(dst, f.Name, f.Default, f.Usage)
}
}

// HTTPHeader applies f to fs and writes the value to dst.
func HTTPHeader(f Flag[Header], dst *Header, fs *pflag.FlagSet) {
switch {
case f.Short != "":
fs.VarP(dst, f.Name, f.Short, f.Usage)
default:
fs.Var(dst, f.Name, f.Usage)
}
}
212 changes: 212 additions & 0 deletions cmd/eksctl-anywhere/cmd/aflag/aflag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package aflag

import (
"testing"

"github.com/spf13/pflag"
)

func TestString(t *testing.T) {
type args struct {
f Flag[string]
dst *string
fs *pflag.FlagSet
}
tests := map[string]struct {
name string
args args
}{
"success long form": {
args: args{
f: Flag[string]{
Name: "test",
Usage: "test usage",
Default: "test default",
},
dst: new(string),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
"success short form": {
args: args{
f: Flag[string]{
Name: "test",
Short: "t",
Usage: "test usage",
Default: "test default",
},
dst: new(string),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
String(tt.args.f, tt.args.dst, tt.args.fs)
})
}
}

func TestBool(t *testing.T) {
type args struct {
f Flag[bool]
dst *bool
fs *pflag.FlagSet
}
tests := map[string]struct {
name string
args args
}{
"success long form": {
args: args{
f: Flag[bool]{
Name: "test",
Usage: "test usage",
Default: true,
},
dst: new(bool),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
"success short form": {
args: args{
f: Flag[bool]{
Name: "test",
Short: "t",
Usage: "test usage",
Default: true,
},
dst: new(bool),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Bool(tt.args.f, tt.args.dst, tt.args.fs)
})
}
}

func TestStringSlice(t *testing.T) {
type args struct {
f Flag[[]string]
dst *[]string
fs *pflag.FlagSet
}
tests := map[string]struct {
name string
args args
}{
"success long form": {
args: args{
f: Flag[[]string]{
Name: "test",
Usage: "test usage",
Default: []string{"test"},
},
dst: new([]string),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
"success short form": {
args: args{
f: Flag[[]string]{
Name: "test",
Short: "t",
Usage: "test usage",
Default: []string{"test"},
},
dst: new([]string),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
StringSlice(tt.args.f, tt.args.dst, tt.args.fs)
})
}
}

func TestStringString(t *testing.T) {
type args struct {
f Flag[map[string]string]
dst *map[string]string
fs *pflag.FlagSet
}
tests := map[string]struct {
name string
args args
}{
"success long form": {
args: args{
f: Flag[map[string]string]{
Name: "test",
Usage: "test usage",
Default: map[string]string{"test": "test"},
},
dst: new(map[string]string),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
"success short form": {
args: args{
f: Flag[map[string]string]{
Name: "test",
Short: "t",
Usage: "test usage",
Default: map[string]string{"test": "test"},
},
dst: new(map[string]string),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
StringString(tt.args.f, tt.args.dst, tt.args.fs)
})
}
}

func TestHTTPHeader(t *testing.T) {
type args struct {
f Flag[Header]
dst *Header
fs *pflag.FlagSet
}
tests := map[string]struct {
name string
args args
}{
"success long form": {
args: args{
f: Flag[Header]{
Name: "test",
Usage: "test usage",
Default: Header{},
},
dst: new(Header),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
"success short form": {
args: args{
f: Flag[Header]{
Name: "test",
Short: "t",
Usage: "test usage",
Default: Header{},
},
dst: new(Header),
fs: pflag.NewFlagSet("test", pflag.ContinueOnError),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
HTTPHeader(tt.args.f, tt.args.dst, tt.args.fs)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flags
package aflag

// ClusterConfig is the path to a cluster specification YAML.
var ClusterConfig = Flag[string]{
Expand Down
63 changes: 63 additions & 0 deletions cmd/eksctl-anywhere/cmd/aflag/header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package aflag

import (
"encoding/csv"
"encoding/json"
"fmt"
"net/http"
"strings"
)

// Header Value.
type Header http.Header

// NewHeader returns a new Header pointer.
func NewHeader(h *http.Header) *Header {
return (*Header)(h)
}

// String returns the string representation of the Header.
func (h *Header) String() string {
if b, err := json.Marshal(h); err == nil {
return string(b)
}

return ""

Check warning on line 25 in cmd/eksctl-anywhere/cmd/aflag/header.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/aflag/header.go#L25

Added line #L25 was not covered by tests
}

// Set sets the value of the Header.
// Format: "a=1;2,b=2;4;5".
func (h *Header) Set(val string) error {
var ss []string
n := strings.Count(val, "=")
switch n {
case 0:
return fmt.Errorf("%s must be formatted as key=value;value", val)
case 1:
ss = append(ss, strings.Trim(val, `"`))

Check warning on line 37 in cmd/eksctl-anywhere/cmd/aflag/header.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/aflag/header.go#L36-L37

Added lines #L36 - L37 were not covered by tests
default:
r := csv.NewReader(strings.NewReader(val))
var err error
ss, err = r.Read()
if err != nil {
return err
}

Check warning on line 44 in cmd/eksctl-anywhere/cmd/aflag/header.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/aflag/header.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}

out := make(map[string][]string, len(ss))
for _, pair := range ss {
kv := strings.SplitN(pair, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("%s must be formatted as key=value;value", pair)
}
out[kv[0]] = strings.Split(kv[1], ";")
}
*h = out

return nil
}

// Type returns the flag type.
func (h *Header) Type() string {
return "header"

Check warning on line 62 in cmd/eksctl-anywhere/cmd/aflag/header.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/aflag/header.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}
35 changes: 35 additions & 0 deletions cmd/eksctl-anywhere/cmd/aflag/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package aflag

import (
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestHeaderM(t *testing.T) {
tests := map[string]struct {
shouldErr bool
want http.Header
input string
}{
"success": {want: map[string][]string{"A": {"1", "2", "3"}, "B": {"2"}, "C": {"4"}}, input: `A=1;2;3,B=2,C=4`},
"bad input format 1": {shouldErr: true, input: `abc`, want: http.Header{}},
"bad input format 2": {shouldErr: true, input: `abc=123;abd=345,`, want: http.Header{}},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
h := http.Header{}
got := NewHeader(&h)
if err := got.Set(tc.input); err != nil {
if !tc.shouldErr {
t.Fatal(err)
}
}
if diff := cmp.Diff(tc.want, h); diff != "" {
t.Fatalf("diff: %s", diff)
}
})
}
}
Loading