-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Create a generic Config struct (#9)
Signed-off-by: pokom <[email protected]>
- Loading branch information
Showing
5 changed files
with
148 additions
and
24 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,31 @@ | ||
package config | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Config struct { | ||
Provider string | ||
ProjectID string | ||
Providers struct { | ||
AWS struct { | ||
Profiles StringSliceFlag | ||
Region string | ||
Services StringSliceFlag | ||
} | ||
GCP struct { | ||
DefaultGCSDiscount int | ||
Projects StringSliceFlag | ||
Region string | ||
Services StringSliceFlag | ||
} | ||
} | ||
Collector struct { | ||
ScrapeInterval time.Duration | ||
} | ||
|
||
Server struct { | ||
Address string | ||
Path string | ||
} | ||
} |
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,14 @@ | ||
package config | ||
|
||
import "strings" | ||
|
||
type StringSliceFlag []string | ||
|
||
func (f *StringSliceFlag) String() string { | ||
return strings.Join(*f, ",") | ||
} | ||
|
||
func (f *StringSliceFlag) Set(value string) error { | ||
*f = append(*f, value) | ||
return 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,71 @@ | ||
package config | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
) | ||
|
||
func TestStringSliceFlag_Set(t *testing.T) { | ||
tests := map[string]struct { | ||
values []string | ||
exp int | ||
}{ | ||
"empty": { | ||
values: []string{}, | ||
exp: 0, | ||
}, | ||
"single": { | ||
values: []string{"-test", "test1"}, | ||
exp: 1, | ||
}, | ||
"multiple": { | ||
values: []string{"-test", "test1", "-test", "test2"}, | ||
exp: 2, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
var ssf StringSliceFlag | ||
fs := flag.NewFlagSet("test", flag.ContinueOnError) | ||
fs.Var(&ssf, "test", "test") | ||
if err := fs.Parse(test.values); err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if exp, got := test.exp, len(ssf); exp != got { | ||
t.Fatalf("expected %d, got %d", exp, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestStringSliceFlag_String(t *testing.T) { | ||
tests := map[string]struct { | ||
values []string | ||
exp string | ||
}{ | ||
"empty": { | ||
values: []string{}, | ||
exp: "", | ||
}, "single": { | ||
values: []string{"test1"}, | ||
exp: "test1", | ||
}, "multiple": { | ||
values: []string{"test1", "test2"}, | ||
exp: "test1,test2", | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
var ssf StringSliceFlag | ||
for _, v := range test.values { | ||
if err := ssf.Set(v); err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
} | ||
if exp, got := test.exp, ssf.String(); exp != got { | ||
t.Fatalf("expected %q, got %q", exp, got) | ||
} | ||
}) | ||
} | ||
} |
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