snap: support for snap tasks --last=... #3217

Merged
merged 13 commits into from Apr 28, 2017

Conversation

Projects
None yet
4 participants
Contributor

stolowski commented Apr 21, 2017

Support for snap tasks --last=<type>
Type can be any of the change types, e.g. "install-snap"; for convienience I made it also understand a set of predefined verbs without "-snap" prefix.
Discussed here: https://forum.snapcraft.io/t/easy-way-to-get-last-change/246

stolowski added some commits Apr 21, 2017

mvo5 approved these changes Apr 21, 2017

Looks good, one question/suggestion. Also wondering if we should integrate that into one of our spread tests.

cmd/snap/cmd_changes.go
}
func (c *cmdChange) Execute([]string) error {
cli := Client()
return showChange(cli, c.Positional.ID)
}
+func findChangeByKind(changes []*client.Change, kind string) *client.Change {
@mvo5

mvo5 Apr 21, 2017

Collaborator

maybe findFirstChangeByKind or something?

@stolowski

stolowski Apr 21, 2017

Contributor

Done.

stolowski added some commits Apr 21, 2017

Contributor

stolowski commented Apr 21, 2017

Added a few checks to existing spread tests.

stolowski added some commits Apr 24, 2017

cmd/snap/cmd_changes.go
+ if c.LastChangeType != "" {
+ kind := c.LastChangeType
+ // our internal change types use "-snap" postfix but let user skip it and use short form.
+ if kind == "refresh" || kind == "install" || kind == "remove" || kind == "connect" || kind == "disconnect" || kind == "configure" {
@pedronis

pedronis Apr 25, 2017

Contributor

there is also "try-snap" for "snap try"

@stolowski

stolowski Apr 26, 2017

Contributor

Done

cmd/snap/cmd_changes.go
} `positional-args:"yes"`
}
func init() {
addCommand("changes", shortChangesHelp, longChangesHelp, func() flags.Commander { return &cmdChanges{} }, nil, nil)
addCommand("change", shortChangeHelp, longChangeHelp, func() flags.Commander { return &cmdChange{} }, nil, nil).hidden = true
- addCommand("tasks", shortChangeHelp, longChangeHelp, func() flags.Commander { return &cmdTasks{} }, nil, nil)
+ addCommand("tasks", shortChangeHelp, longChangeHelp, func() flags.Commander { return &cmdTasks{} }, map[string]string{
+ "last": "Show last change of given type (install, refresh, remove etc.)",
@pedronis

pedronis Apr 25, 2017

Contributor

we should list "auto-refresh" here as well, it's an interesting one and not obviously a command

@stolowski

stolowski Apr 26, 2017

Contributor

Done.

stolowski added some commits Apr 26, 2017

@pedronis pedronis requested a review from niemeyer Apr 26, 2017

lgtm

cmd/snap/cmd_changes.go
+ }
+ // sort by date, descending so that we will pick the most recent change of given kind.
+ sort.Sort(sort.Reverse(changesByTime(changes)))
+ chg := findFirstChangeByKind(changes, kind)
@pedronis

pedronis Apr 27, 2017

Contributor

given that we need to iterate to filter by kind anyway, we could just find the max spawnTime of a kind in one go, just a consideration

@stolowski

stolowski Apr 28, 2017

Contributor

Good idea. I was in a "functional programming" mindset doing that change ;). Done.

zyga approved these changes Apr 28, 2017

Looks good, a few nitpicks for your consideration.

cmd/snap/cmd_changes.go
} `positional-args:"yes"`
}
func init() {
addCommand("changes", shortChangesHelp, longChangesHelp, func() flags.Commander { return &cmdChanges{} }, nil, nil)
addCommand("change", shortChangeHelp, longChangeHelp, func() flags.Commander { return &cmdChange{} }, nil, nil).hidden = true
- addCommand("tasks", shortChangeHelp, longChangeHelp, func() flags.Commander { return &cmdTasks{} }, nil, nil)
+ addCommand("tasks", shortChangeHelp, longChangeHelp, func() flags.Commander { return &cmdTasks{} }, map[string]string{
+ "last": "Show last change of given type (install, refresh, remove, try, auto-refresh etc.)",
@zyga

zyga Apr 28, 2017

Contributor

nitpick, i18n

cmd/snap/cmd_changes.go
@@ -122,14 +125,55 @@ func (c *cmdChanges) Execute(args []string) error {
func (c *cmdTasks) Execute([]string) error {
cli := Client()
- return showChange(cli, c.Positional.ID)
+ var id changeID
+ if c.Positional.ID == "" && c.LastChangeType == "" {
@zyga

zyga Apr 28, 2017

Contributor

This is a personal preference that may not be shared by the rest of the team but I prefer switch case style instead of the if-then-else tree:

switch {
  case c.Positional.ID == "" && c.LastChangeType == "":
  ...
  case c.Positional.ID != "" && c.LastChangeType == "":
  ...
  case c.Positional.ID != "":
  ...
  case c.LastChangeType != "":
  ...
}

You could also combine this with my suggestion below and merge the first two cases.

@stolowski

stolowski Apr 28, 2017

Contributor

Thanks, good suggestion, I'm not used to this go-specific statements yet, but I like it. Done. I haven't however merged the two error case together, I think they should have separate error messages.

cmd/snap/cmd_changes.go
+ return fmt.Errorf(i18n.G("please provide change ID or type with --last=<type>"))
+ }
+ if c.Positional.ID != "" && c.LastChangeType != "" {
+ return fmt.Errorf(i18n.G("change use ID and type together"))
@zyga

zyga Apr 28, 2017

Contributor

the error message is somewhat confusing for me, how about reusing the exact same error message above? (perhaps emphasise the either by saying please provide either change ID or change type with --last=<type>

@pedronis

pedronis Apr 28, 2017

Contributor

yes, "please provide..." or "please specify..." seems to be the style for this kind of errors we use

@stolowski

stolowski Apr 28, 2017

Contributor

Thanks for spotting, indeed this comment is totally off, I'm not sure what I was thinking... I've changed it to "cannot use change ID and type together", this is in-line with other messages concerning mutually-exclusive options.

+ // our internal change types use "-snap" postfix but let user skip it and use short form.
+ if kind == "refresh" || kind == "install" || kind == "remove" || kind == "connect" || kind == "disconnect" || kind == "configure" || kind == "try" {
+ kind += "-snap"
+ }
@zyga

zyga Apr 28, 2017

Contributor

Should we have an else here that says the kind is incorrect?

@pedronis

pedronis Apr 28, 2017

Contributor

this is not an exhaustive list of all kinds, just the ones for which kind == command+"-snap"

for example auto-refresh is an interesting one not of that form

@stolowski

stolowski Apr 28, 2017

Contributor

Yes, this list acts as a "shortuct" or set of "aliases" for the user to help with common tasks. But you can ask for any other task if you know the internal type name.

stolowski added some commits Apr 28, 2017

zyga approved these changes Apr 28, 2017

+1

@stolowski stolowski merged commit cfb1e94 into snapcore:master Apr 28, 2017

4 of 7 checks passed

artful-amd64 autopkgtest finished (failure)
Details
xenial-i386 autopkgtest finished (failure)
Details
yakkety-amd64 autopkgtest finished (failure)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
xenial-amd64 autopkgtest finished (success)
Details
xenial-ppc64el autopkgtest finished (success)
Details
zesty-amd64 autopkgtest finished (success)
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment