-
Notifications
You must be signed in to change notification settings - Fork 29
feat(cdn): add cdn client, config, list command #1100
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
base: main
Are you sure you want to change the base?
feat(cdn): add cdn client, config, list command #1100
Conversation
| return cmd | ||
| } | ||
|
|
||
| var sortByFlagOptions = []string{"id", "created", "updated", "origin-url", "status"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please directly use directly the API values: "id" "updatedAt" "createdAt" "originUrl" "status" "originUrlRelated"
This was wrong in the Jira story.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there's also a enum generated in the SDK which holds these values. Then this would be the way to go because it will be kept up-to-date in the future without any efforts on our side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Searched for sortBy and originUrl etc. could not find an enum for this. The spec looks like it should generate an enum: https://github.com/stackitcloud/stackit-api-specifications/blob/83214868e6dbdc53d053cc07654542d8f34b5491/services/cdn/v1beta2/cdn.json#L1486
How could I start to investigate this further?
|
|
||
| func outputResult(p *print.Printer, outputFormat string, distributions []cdn.Distribution) error { | ||
| if distributions == nil { | ||
| distributions = make([]cdn.Distribution, 0) // otherwise prints null in json output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good catch indeed! Any chance we can solve this in a central place for all commands?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea, I'll investigate this a bit, don't know much about go reflection yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reflection is considered a no-no, see https://principles.schwarz/principles/engineering-principles/go/avoid-using-reflection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With reflection I'd add this in Printer.OutputResult():
val := reflect.ValueOf(output)
if val.IsNil() {
// when passing a nil slice or map, JSON and YAML marshal to "null"
// so we need to create an empty slice or map to have "[]" or "{}" output instead
switch val.Kind() {
case reflect.Slice:
output = make([]any, 0)
case reflect.Map:
output = make(map[any]any)
default:
// do nothing
}
}The linked document suggests type switches, which would not work here.
Another solution without reflection, that comes to mind, would be to introduce specialized OutputResult functions, like OutputResultSlice/Map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Could be an option, please feel free to create a Jira ticket and propose a solution 😊
Co-authored-by: Ruben Hönle <Ruben.Hoenle@stackit.cloud>
Co-authored-by: Ruben Hönle <Ruben.Hoenle@stackit.cloud>
|
Btw, please always add the Jira issue ID to your PR description if possible :) |
Merging this branch changes the coverage (3 decrease, 7 increase)
Coverage by fileChanged files (no unit tests)
Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code. Changed unit test files
|
- test Min - test JoinStringMap - rm superfluous var for constant - rm file committed by accident - add nil checks when dereferencing pointers
Merging this branch changes the coverage (2 decrease, 8 increase)
Coverage by fileChanged files (no unit tests)
Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code. Changed unit test files
|
| } | ||
|
|
||
| // JoinStringMap concatenates the key-value pairs of a string map, key and value separated by kvSep, key value pairs separated by sep. | ||
| func JoinStringMap(m map[string]string, kvSep, sep string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's fine, can stay here in this pkg
| } | ||
|
|
||
| // JoinStringMap concatenates the key-value pairs of a string map, key and value separated by kvSep, key value pairs separated by sep. | ||
| func JoinStringMap(m map[string]string, kvSep, sep string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| func JoinStringMap(m map[string]string, kvSep, sep string) string { | |
| func JoinStringMap(m map[string]string, keyValueSeparator, separator string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's more readable if you write it out, isn't it?
| return JoinStringKeys(m, sep) | ||
| } | ||
|
|
||
| // JoinStringMap concatenates the key-value pairs of a string map, key and value separated by kvSep, key value pairs separated by sep. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // JoinStringMap concatenates the key-value pairs of a string map, key and value separated by kvSep, key value pairs separated by sep. | |
| // JoinStringMap concatenates the key-value pairs of a string map, key and value separated by keyValueSeparator, key value pairs separated by separator. |
| func TestJoinStringMap(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| m map[string]string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| m map[string]string | |
| input map[string]string |
Sorry, I'm nitpicking. But please use proper names.
| ), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() // should this be cancellable? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ctx := context.Background() // should this be cancellable? | |
| ctx := context.Background() |
We have it like this everywhere. And as of now I don't see any reason to change that. Any objections?
|
|
||
| table := tables.NewTable() | ||
| table.SetHeader("ID", "REGIONS", "STATUS") | ||
| for i := range distributions { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| for i := range distributions { | |
| for i, d := range distributions { |
Saves yourself the d := &distributions[i] line below, not?
| table.SetHeader("ID", "REGIONS", "STATUS") | ||
| for i := range distributions { | ||
| d := &distributions[i] | ||
| joinedRegions := strings.Join(sdkUtils.EnumSliceToStringSlice(*d.Config.Regions), ", ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take care of the potential nil pointer reference here please
|
|
||
| // Returns the flag's value as a []string. | ||
| // Returns nil if flag is not set, if its value can not be converted to []string, or if the flag does not exist. | ||
| func FlagToStringArrayValue(p *print.Printer, cmd *cobra.Command, flag string) []string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| func FlagToStringArrayValue(p *print.Printer, cmd *cobra.Command, flag string) []string { | |
| func FlagToStringSliceValue(p *print.Printer, cmd *cobra.Command, flag string) []string { |
won't we call it slice in golang?
Description
Issue: STACKITCLI-70
Checklist
make fmtmake generate-docs(will be checked by CI)make test(will be checked by CI)make lint(will be checked by CI)