github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap/last.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package main 21 22 import ( 23 "errors" 24 "fmt" 25 26 "github.com/snapcore/snapd/client" 27 "github.com/snapcore/snapd/i18n" 28 ) 29 30 type changeIDMixin struct { 31 clientMixin 32 LastChangeType string `long:"last"` 33 Positional struct { 34 ID changeID `positional-arg-name:"<id>"` 35 } `positional-args:"yes"` 36 } 37 38 var changeIDMixinOptDesc = mixinDescs{ 39 // TRANSLATORS: This should not start with a lowercase letter. 40 "last": i18n.G("Select last change of given type (install, refresh, remove, try, auto-refresh, etc.). A question mark at the end of the type means to do nothing (instead of returning an error) if no change of the given type is found. Note the question mark could need protecting from the shell."), 41 } 42 43 var changeIDMixinArgDesc = []argDesc{{ 44 // TRANSLATORS: This needs to begin with < and end with > 45 name: i18n.G("<change-id>"), 46 // TRANSLATORS: This should not start with a lowercase letter. 47 desc: i18n.G("Change ID"), 48 }} 49 50 // should not be user-visible, but keep it clear and polite because mistakes happen 51 var noChangeFoundOK = errors.New("no change found but that's ok") 52 53 func (l *changeIDMixin) GetChangeID() (string, error) { 54 if l.Positional.ID == "" && l.LastChangeType == "" { 55 return "", fmt.Errorf(i18n.G("please provide change ID or type with --last=<type>")) 56 } 57 58 if l.Positional.ID != "" { 59 if l.LastChangeType != "" { 60 return "", fmt.Errorf(i18n.G("cannot use change ID and type together")) 61 } 62 63 return string(l.Positional.ID), nil 64 } 65 66 cli := l.client 67 // note that at this point we know l.LastChangeType != "" 68 kind := l.LastChangeType 69 optional := false 70 if l := len(kind) - 1; kind[l] == '?' { 71 optional = true 72 kind = kind[:l] 73 } 74 // our internal change types use "-snap" postfix but let user skip it and use short form. 75 if kind == "refresh" || kind == "install" || kind == "remove" || kind == "connect" || kind == "disconnect" || kind == "configure" || kind == "try" { 76 kind += "-snap" 77 } 78 changes, err := queryChanges(cli, &client.ChangesOptions{Selector: client.ChangesAll}) 79 if err != nil { 80 return "", err 81 } 82 if len(changes) == 0 { 83 if optional { 84 return "", noChangeFoundOK 85 } 86 return "", fmt.Errorf(i18n.G("no changes found")) 87 } 88 chg := findLatestChangeByKind(changes, kind) 89 if chg == nil { 90 if optional { 91 return "", noChangeFoundOK 92 } 93 return "", fmt.Errorf(i18n.G("no changes of type %q found"), l.LastChangeType) 94 } 95 96 return chg.ID, nil 97 } 98 99 func findLatestChangeByKind(changes []*client.Change, kind string) (latest *client.Change) { 100 for _, chg := range changes { 101 if chg.Kind == kind && (latest == nil || latest.SpawnTime.Before(chg.SpawnTime)) { 102 latest = chg 103 } 104 } 105 return latest 106 }