kd: rework credential / stack commands - #9666
Conversation
|
|
||
| // TabPrinter is used to print any slice of values | ||
| // encoded with a tabwriter. | ||
| var TabPrinter = &Printer{ |
| owner = CurrentUser | ||
| } | ||
|
|
||
| // Don't change owner |
| stringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() | ||
| ) | ||
|
|
||
| func (p *Printer) printTab(v interface{}) error { |
There was a problem hiding this comment.
I believe we can implement the same functionality with
- encode to json
- decode the result to map[string]interface{}
- get the keys and output them.
we can also add embedded field support easily with a recursive function.
Lets not use reflection whenever possible in our app logic.
ie: getting recursive field names
func getKeys(prefix string, data map[string]interface{}) {
for key, val := range data {
fieldKey := prefix + key
// do your thingy with fieldKey
next, ok := val.(map[string]interface{})
if ok {
getKeys(fieldKey+".", next)
}
}
}
There was a problem hiding this comment.
Will take a try at reworking this.
There was a problem hiding this comment.
@cihangir this may not work for empty arrays since they will be decoded to [ ] and we need at least 1 element to know the keys [ {"key1": "val1", "key2": "val2" } ].
There was a problem hiding this comment.
this may not work for empty arrays since they will be decoded to [ ]
Yes, this plus the columns would always be ordered alphabetically, so it won't be possible order them per-struct. I can make some hack, if column == "id" then display it first etc. However it would not work the same.
There was a problem hiding this comment.
@ppknap could you please show me an example where would you need to show the Empty values of an array?
@rjeczalik you will have the fieldKey you can order them however you like.
There was a problem hiding this comment.
I'm going to just remove this generic tab / json printer and use tabwriter package directly in cli.
There was a problem hiding this comment.
Completely diff issue: if we also use json there, we would allow users to pass their own template - ie: http://kubernetes.io/docs/user-guide/jsonpath/ ( not saying we should implement this right now )
There was a problem hiding this comment.
@cihangir Agreed, JSONPath and text/template support (kubectl -t '{{.Something}}') are really cool features for scripting with tool. For now they can be achieved with jq since our backend API does not support patching / server-side filtering - so there's no difference whether we do:
kd stack list -json | jq .Name
or
kd stack list -jsonpath .Name
There was a problem hiding this comment.
With the map[string]interface, you have the keys, same with a struct.
yes, but objects passed to this function are meant to be arrays so we will not have map[string]interface{} but rather []map[string]interface{}/[]interface{} and when len(passed_argument) == 0 we will have no keys.
| continue | ||
| } | ||
|
|
||
| merge: |
There was a problem hiding this comment.
instead of label breaks we can do smth like this.
semi-pseudo
for _, cached := range c.cached[provider] {
if !has(cached, cred) {
c.cached[provider] = append(c.cached[provider], cred)
}
}
func has(cached, cred) bool {
for _, cred := range creds {
if cached.Identifier == cred.Identifier {
return true
}
}
return false
}```
| // | ||
| // BUG(rjeczalik): This is going to break templates, which have legit | ||
| // 1-element []map[string]interface{} values. | ||
| func fixHCL(v interface{}) { |
| } | ||
|
|
||
| // Create | ||
| func (c *Client) Create(opts *CreateOptions) (*stack.ImportResponse, error) { |
There was a problem hiding this comment.
could you write test for this function?
There was a problem hiding this comment.
What kind of test are you thinking of?
| stringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() | ||
| ) | ||
|
|
||
| func (p *Printer) printTab(v interface{}) error { |
There was a problem hiding this comment.
@ppknap could you please show me an example where would you need to show the Empty values of an array?
@rjeczalik you will have the fieldKey you can order them however you like.
|
@rjeczalik this is for you https://koding.slack.com/archives/engineering/p1479810522000885 ;) |
dd719e8 to
89b49c3
Compare
4e31525 to
8ec4d20
Compare
8ec4d20 to
c9e8a73
Compare
Prerequisite for #9640 and #9641.