-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathcmd_alias.go
118 lines (101 loc) · 3.06 KB
/
cmd_alias.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"io"
"text/tabwriter"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/snap"
)
type cmdAlias struct {
waitMixin
Positionals struct {
SnapApp appName `required:"yes"`
Alias string `required:"yes"`
} `positional-args:"true"`
}
// TODO: implement a completer for snapApp
var shortAliasHelp = i18n.G("Set up a manual alias")
var longAliasHelp = i18n.G(`
The alias command aliases the given snap application to the given alias.
Once this manual alias is setup the respective application command can be
invoked just using the alias.
`)
func init() {
addCommand("alias", shortAliasHelp, longAliasHelp, func() flags.Commander {
return &cmdAlias{}
}, waitDescs, []argDesc{
{name: "<snap.app>"},
// TRANSLATORS: This needs to begin with < and end with >
{name: i18n.G("<alias>")},
})
}
func (x *cmdAlias) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
snapName, appName := snap.SplitSnapApp(string(x.Positionals.SnapApp))
alias := x.Positionals.Alias
id, err := x.client.Alias(snapName, appName, alias)
if err != nil {
return err
}
chg, err := x.wait(id)
if err != nil {
if err == noWait {
return nil
}
return err
}
return showAliasChanges(chg)
}
type changedAlias struct {
Snap string `json:"snap"`
App string `json:"app"`
Alias string `json:"alias"`
}
func showAliasChanges(chg *client.Change) error {
var added, removed []*changedAlias
if err := chg.Get("aliases-added", &added); err != nil && err != client.ErrNoData {
return err
}
if err := chg.Get("aliases-removed", &removed); err != nil && err != client.ErrNoData {
return err
}
w := tabwriter.NewWriter(Stdout, 2, 2, 1, ' ', 0)
if len(added) != 0 {
// TRANSLATORS: this is used to introduce a list of aliases that were added
printChangedAliases(w, i18n.G("Added"), added)
}
if len(removed) != 0 {
// TRANSLATORS: this is used to introduce a list of aliases that were removed
printChangedAliases(w, i18n.G("Removed"), removed)
}
w.Flush()
return nil
}
func printChangedAliases(w io.Writer, label string, changed []*changedAlias) {
fmt.Fprintf(w, "%s:\n", label)
for _, a := range changed {
// TRANSLATORS: the first %s is a snap command (e.g. "hello-world.echo"), the second is the alias
fmt.Fprintf(w, i18n.G("\t- %s as %s\n"), snap.JoinSnapApp(a.Snap, a.App), a.Alias)
}
}