Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

kd: rework credential / stack commands - #9666

Merged
cihangir merged 22 commits into
masterfrom
kd-improvements
Nov 24, 2016
Merged

kd: rework credential / stack commands#9666
cihangir merged 22 commits into
masterfrom
kd-improvements

Conversation

@rjeczalik

Copy link
Copy Markdown
Contributor

Prerequisite for #9640 and #9641.


// TabPrinter is used to print any slice of values
// encoded with a tabwriter.
var TabPrinter = &Printer{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @ppknap

owner = CurrentUser
}

// Don't change owner

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

stringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
)

func (p *Printer) printTab(v interface{}) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
		}
	}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will take a try at reworking this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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" } ].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to just remove this generic tab / json printer and use tabwriter package directly in cli.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 )

@rjeczalik rjeczalik Nov 23, 2016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

continue
}

merge:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
	}```

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

//
// BUG(rjeczalik): This is going to break templates, which have legit
// 1-element []map[string]interface{} values.
func fixHCL(v interface{}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

// Create
func (c *Client) Create(opts *CreateOptions) (*stack.ImportResponse, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you write test for this function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of test are you thinking of?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

stringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
)

func (p *Printer) printTab(v interface{}) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@cihangir

Copy link
Copy Markdown
Contributor

@rjeczalik this is for you https://koding.slack.com/archives/engineering/p1479810522000885 ;)

@cihangir
cihangir merged commit d2b65c8 into master Nov 24, 2016
@cihangir
cihangir deleted the kd-improvements branch November 24, 2016 17:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants