-
Notifications
You must be signed in to change notification settings - Fork 583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
many: support 'system' nickname in interfaces #5080
Changes from 4 commits
1b63975
0802d53
c0a8e0e
4fd101b
1f09441
591c777
977d5f0
e9cc71e
c87eafb
70b0589
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"fmt" | ||
|
||
"github.com/snapcore/snapd/i18n" | ||
"github.com/snapcore/snapd/snap" | ||
|
||
"github.com/jessevdk/go-flags" | ||
) | ||
|
@@ -67,6 +68,13 @@ func init() { | |
}}) | ||
} | ||
|
||
func maybeAsNickname(name string, useNick bool) string { | ||
if useNick { | ||
return snap.Nickname(name) | ||
} | ||
return name | ||
} | ||
|
||
func (x *cmdInterfaces) Execute(args []string) error { | ||
if len(args) > 0 { | ||
return ErrExtraArgs | ||
|
@@ -82,11 +90,24 @@ func (x *cmdInterfaces) Execute(args []string) error { | |
w := tabWriter() | ||
defer w.Flush() | ||
fmt.Fprintln(w, i18n.G("Slot\tPlug")) | ||
|
||
wantedSnap := x.Positionals.Query.Snap | ||
|
||
for _, slot := range ifaces.Slots { | ||
if wanted := x.Positionals.Query.Snap; wanted != "" { | ||
ok := wanted == slot.Snap | ||
askedWithNick := false | ||
if wantedSnap != "" { | ||
ok := wantedSnap == slot.Snap | ||
if !ok && wantedSnap == snap.Nickname(slot.Snap) { | ||
askedWithNick = true | ||
ok = true | ||
} | ||
|
||
for i := 0; i < len(slot.Connections) && !ok; i++ { | ||
ok = wanted == slot.Connections[i].Snap | ||
ok = wantedSnap == slot.Connections[i].Snap | ||
if !ok && wantedSnap == snap.Nickname(slot.Connections[i].Snap) { | ||
askedWithNick = true | ||
ok = true | ||
} | ||
} | ||
if !ok { | ||
continue | ||
|
@@ -100,19 +121,20 @@ func (x *cmdInterfaces) Execute(args []string) error { | |
} | ||
// The OS snap is special and enable abbreviated | ||
// display syntax on the slot-side of the connection. | ||
if slot.Snap == "core" || slot.Snap == "ubuntu-core" { | ||
if slot.Snap == "core" || slot.Snap == "ubuntu-core" || slot.Snap == snap.Nickname("core") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a growing number of places that do check like this, I wonder if it's time for a helper. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT this is the only place where all 3 options appear. |
||
fmt.Fprintf(w, ":%s\t", slot.Name) | ||
} else { | ||
fmt.Fprintf(w, "%s:%s\t", slot.Snap, slot.Name) | ||
fmt.Fprintf(w, "%s:%s\t", maybeAsNickname(slot.Snap, askedWithNick), slot.Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't feel like we gain much from varying here based on the snap. Just feels more magic, which isn't necessarily good. In fact, the case for core, which is the system snap and the only nickname we care about right now, is already special case here in a custom branch right above. So the change seems to introduce extra complexity without much of a win. Plus, this whole command is a bit unfortunate.. we really need that "connections" command we discussed a few times. So perhaps we should keep this simpler and more straightforward for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting I should drop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed this on IRC |
||
} | ||
for i := 0; i < len(slot.Connections); i++ { | ||
if i > 0 { | ||
fmt.Fprint(w, ",") | ||
} | ||
if slot.Connections[i].Name != slot.Name { | ||
fmt.Fprintf(w, "%s:%s", slot.Connections[i].Snap, slot.Connections[i].Name) | ||
fmt.Fprintf(w, "%s:%s", maybeAsNickname(slot.Connections[i].Snap, askedWithNick), | ||
slot.Connections[i].Name) | ||
} else { | ||
fmt.Fprintf(w, "%s", slot.Connections[i].Snap) | ||
fmt.Fprintf(w, "%s", maybeAsNickname(slot.Connections[i].Snap, askedWithNick)) | ||
} | ||
} | ||
// Display visual indicator for disconnected slots | ||
|
@@ -124,8 +146,17 @@ func (x *cmdInterfaces) Execute(args []string) error { | |
// Plugs are treated differently. Since the loop above already printed each connected | ||
// plug, the loop below focuses on printing just the disconnected plugs. | ||
for _, plug := range ifaces.Plugs { | ||
if x.Positionals.Query.Snap != "" && x.Positionals.Query.Snap != plug.Snap { | ||
continue | ||
maybeNick := plug.Snap | ||
if wantedSnap != "" { | ||
ok := wantedSnap == plug.Snap | ||
nick := snap.Nickname(plug.Snap) | ||
if !ok && wantedSnap == nick { | ||
maybeNick = nick | ||
ok = true | ||
} | ||
if !ok { | ||
continue | ||
} | ||
} | ||
if x.Positionals.Query.Name != "" && x.Positionals.Query.Name != plug.Name { | ||
continue | ||
|
@@ -135,7 +166,7 @@ func (x *cmdInterfaces) Execute(args []string) error { | |
} | ||
// Display visual indicator for disconnected plugs. | ||
if len(plug.Connections) == 0 { | ||
fmt.Fprintf(w, "-\t%s:%s\n", plug.Snap, plug.Name) | ||
fmt.Fprintf(w, "-\t%s:%s\n", maybeNick, plug.Name) | ||
} | ||
} | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1852,6 +1852,13 @@ func changeInterfaces(c *Command, r *http.Request, user *auth.UserState) Respons | |
st.Lock() | ||
defer st.Unlock() | ||
|
||
for i := range a.Plugs { | ||
a.Plugs[i].Snap = systemCoreSnapUnalias(a.Plugs[i].Snap) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "alias" means something else in the snap world. Perhaps;
|
||
} | ||
for i := range a.Slots { | ||
a.Slots[i].Snap = systemCoreSnapUnalias(a.Slots[i].Snap) | ||
} | ||
|
||
switch a.Action { | ||
case "connect": | ||
var connRef interfaces.ConnRef | ||
|
@@ -2823,3 +2830,10 @@ func systemCoreSnapUnalias(name string) string { | |
} | ||
return name | ||
} | ||
|
||
func systemCoreSnapAlias(name string) string { | ||
if name == "core" { | ||
return "system" | ||
} | ||
return name | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -929,3 +929,12 @@ func JoinSnapApp(snap, app string) string { | |
} | ||
return fmt.Sprintf("%s.%s", snap, app) | ||
} | ||
|
||
// Nickname returns the nickname for given snap name. If there is none, returns | ||
// the original name. | ||
func Nickname(snapName string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this the same function we have above? Can we use the same without cycles? Also, perhaps similar to the above point:
|
||
if snapName == "core" { | ||
return "system" | ||
} | ||
return snapName | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: core-support | ||
summary: special permissions for the core snap | ||
plugs: | ||
- core:core-support-plug | ||
- system:core-support-plug | ||
slots: | ||
- core | ||
- system |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the above checks could use a helper:
matches, askedWithNick := isWantedSnap(snapname)
and the helper would internaly check nickaname as well.