github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_alias.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 "fmt" 24 "io" 25 "text/tabwriter" 26 27 "github.com/jessevdk/go-flags" 28 29 "github.com/snapcore/snapd/client" 30 "github.com/snapcore/snapd/i18n" 31 "github.com/snapcore/snapd/snap" 32 ) 33 34 type cmdAlias struct { 35 waitMixin 36 Positionals struct { 37 SnapApp appName `required:"yes"` 38 Alias string `required:"yes"` 39 } `positional-args:"true"` 40 } 41 42 // TODO: implement a completer for snapApp 43 44 var shortAliasHelp = i18n.G("Set up a manual alias") 45 var longAliasHelp = i18n.G(` 46 The alias command aliases the given snap application to the given alias. 47 48 Once this manual alias is setup the respective application command can be 49 invoked just using the alias. 50 `) 51 52 func init() { 53 addCommand("alias", shortAliasHelp, longAliasHelp, func() flags.Commander { 54 return &cmdAlias{} 55 }, waitDescs, []argDesc{ 56 {name: "<snap.app>"}, 57 // TRANSLATORS: This needs to begin with < and end with > 58 {name: i18n.G("<alias>")}, 59 }) 60 } 61 62 func (x *cmdAlias) Execute(args []string) error { 63 if len(args) > 0 { 64 return ErrExtraArgs 65 } 66 67 snapName, appName := snap.SplitSnapApp(string(x.Positionals.SnapApp)) 68 alias := x.Positionals.Alias 69 70 id, err := x.client.Alias(snapName, appName, alias) 71 if err != nil { 72 return err 73 } 74 chg, err := x.wait(id) 75 if err != nil { 76 if err == noWait { 77 return nil 78 } 79 return err 80 } 81 82 return showAliasChanges(chg) 83 } 84 85 type changedAlias struct { 86 Snap string `json:"snap"` 87 App string `json:"app"` 88 Alias string `json:"alias"` 89 } 90 91 func showAliasChanges(chg *client.Change) error { 92 var added, removed []*changedAlias 93 if err := chg.Get("aliases-added", &added); err != nil && err != client.ErrNoData { 94 return err 95 } 96 if err := chg.Get("aliases-removed", &removed); err != nil && err != client.ErrNoData { 97 return err 98 } 99 w := tabwriter.NewWriter(Stdout, 2, 2, 1, ' ', 0) 100 if len(added) != 0 { 101 // TRANSLATORS: this is used to introduce a list of aliases that were added 102 printChangedAliases(w, i18n.G("Added"), added) 103 } 104 if len(removed) != 0 { 105 // TRANSLATORS: this is used to introduce a list of aliases that were removed 106 printChangedAliases(w, i18n.G("Removed"), removed) 107 } 108 w.Flush() 109 return nil 110 } 111 112 func printChangedAliases(w io.Writer, label string, changed []*changedAlias) { 113 fmt.Fprintf(w, "%s:\n", label) 114 for _, a := range changed { 115 // TRANSLATORS: the first %s is a snap command (e.g. "hello-world.echo"), the second is the alias 116 fmt.Fprintf(w, i18n.G("\t- %s as %s\n"), snap.JoinSnapApp(a.Snap, a.App), a.Alias) 117 } 118 }