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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 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 (
"encoding/json"
"errors"
"fmt"
"io"
"sort"
"text/tabwriter"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/i18n"
)
type cmdInterface struct {
clientMixin
ShowAttrs bool `long:"attrs"`
ShowAll bool `long:"all"`
Positionals struct {
Interface interfaceName `skip-help:"true"`
} `positional-args:"true"`
}
var shortInterfaceHelp = i18n.G("Show details of snap interfaces")
var longInterfaceHelp = i18n.G(`
The interface command shows details of snap interfaces.
If no interface name is provided, a list of interface names with at least
one connection is shown, or a list of all interfaces if --all is provided.
`)
func init() {
addCommand("interface", shortInterfaceHelp, longInterfaceHelp, func() flags.Commander {
return &cmdInterface{}
}, map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"attrs": i18n.G("Show interface attributes"),
// TRANSLATORS: This should not start with a lowercase letter.
"all": i18n.G("Include unused interfaces"),
}, []argDesc{{
// TRANSLATORS: This needs to begin with < and end with >
name: i18n.G("<interface>"),
// TRANSLATORS: This should not start with a lowercase letter.
desc: i18n.G("Show details of a specific interface"),
}})
}
func (x *cmdInterface) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if x.Positionals.Interface != "" {
// Show one interface in detail.
name := string(x.Positionals.Interface)
ifaces, err := x.client.Interfaces(&client.InterfaceOptions{
Names: []string{name},
Doc: true,
Plugs: true,
Slots: true,
})
if err != nil {
return err
}
if len(ifaces) == 0 {
return errors.New(i18n.G("no such interface"))
}
x.showOneInterface(ifaces[0])
} else {
// Show an overview of available interfaces.
ifaces, err := x.client.Interfaces(&client.InterfaceOptions{
Connected: !x.ShowAll,
})
if err != nil {
return err
}
if len(ifaces) == 0 {
if x.ShowAll {
return errors.New(i18n.G("no interfaces found"))
}
return errors.New(i18n.G("no interfaces currently connected"))
}
x.showManyInterfaces(ifaces)
}
return nil
}
func (x *cmdInterface) showOneInterface(iface *client.Interface) {
w := tabwriter.NewWriter(Stdout, 2, 2, 1, ' ', 0)
defer w.Flush()
fmt.Fprintf(w, "name:\t%s\n", iface.Name)
if iface.Summary != "" {
fmt.Fprintf(w, "summary:\t%s\n", iface.Summary)
}
if iface.DocURL != "" {
fmt.Fprintf(w, "documentation:\t%s\n", iface.DocURL)
}
if len(iface.Plugs) > 0 {
fmt.Fprintf(w, "plugs:\n")
for _, plug := range iface.Plugs {
var labelPart string
if plug.Label != "" {
labelPart = fmt.Sprintf(" (%s)", plug.Label)
}
if plug.Name == iface.Name {
fmt.Fprintf(w, " - %s%s", plug.Snap, labelPart)
} else {
fmt.Fprintf(w, ` - %s:%s%s`, plug.Snap, plug.Name, labelPart)
}
// Print a colon which will make the snap:plug element a key-value
// yaml object so that we can write the attributes.
if len(plug.Attrs) > 0 && x.ShowAttrs {
fmt.Fprintf(w, ":\n")
x.showAttrs(w, plug.Attrs, " ")
} else {
fmt.Fprintf(w, "\n")
}
}
}
if len(iface.Slots) > 0 {
fmt.Fprintf(w, "slots:\n")
for _, slot := range iface.Slots {
var labelPart string
if slot.Label != "" {
labelPart = fmt.Sprintf(" (%s)", slot.Label)
}
if slot.Name == iface.Name {
fmt.Fprintf(w, " - %s%s", slot.Snap, labelPart)
} else {
fmt.Fprintf(w, ` - %s:%s%s`, slot.Snap, slot.Name, labelPart)
}
// Print a colon which will make the snap:slot element a key-value
// yaml object so that we can write the attributes.
if len(slot.Attrs) > 0 && x.ShowAttrs {
fmt.Fprintf(w, ":\n")
x.showAttrs(w, slot.Attrs, " ")
} else {
fmt.Fprintf(w, "\n")
}
}
}
}
func (x *cmdInterface) showManyInterfaces(infos []*client.Interface) {
w := tabWriter()
defer w.Flush()
fmt.Fprintln(w, i18n.G("Name\tSummary"))
for _, iface := range infos {
fmt.Fprintf(w, "%s\t%s\n", iface.Name, iface.Summary)
}
}
func (x *cmdInterface) showAttrs(w io.Writer, attrs map[string]any, indent string) {
if len(attrs) == 0 {
return
}
names := make([]string, 0, len(attrs))
for name := range attrs {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
value := attrs[name]
switch value.(type) {
case string, bool, json.Number:
fmt.Fprintf(w, "%s %s:\t%v\n", indent, name, value)
}
}
}
|