Skip to content

Commit 88d7db3

Browse files
authored
cmd/tailscale: use tailnet display name on cli (tailscale#17079)
Updates cli to use tailnet display name Updates tailscale/corp#32108 Signed-off-by: nikiUppal-TS <nikita@tailscale.com>
1 parent 77250a3 commit 88d7db3

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

cmd/tailscale/cli/switch.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var switchCmd = &ffcli.Command{
2424
LongHelp: `"tailscale switch" switches between logged in accounts. You can
2525
use the ID that's returned from 'tailnet switch -list'
2626
to pick which profile you want to switch to. Alternatively, you
27-
can use the Tailnet or the account names to switch as well.
27+
can use the Tailnet, account names, or display names to switch as well.
2828
2929
This command is currently in alpha and may change in the future.`,
3030

@@ -46,7 +46,7 @@ func init() {
4646
seen := make(map[string]bool, 3*len(all))
4747
wordfns := []func(prof ipn.LoginProfile) string{
4848
func(prof ipn.LoginProfile) string { return string(prof.ID) },
49-
func(prof ipn.LoginProfile) string { return prof.NetworkProfile.DomainName },
49+
func(prof ipn.LoginProfile) string { return prof.NetworkProfile.DisplayNameOrDefault() },
5050
func(prof ipn.LoginProfile) string { return prof.Name },
5151
}
5252

@@ -57,7 +57,7 @@ func init() {
5757
continue
5858
}
5959
seen[word] = true
60-
words = append(words, fmt.Sprintf("%s\tid: %s, tailnet: %s, account: %s", word, prof.ID, prof.NetworkProfile.DomainName, prof.Name))
60+
words = append(words, fmt.Sprintf("%s\tid: %s, tailnet: %s, account: %s", word, prof.ID, prof.NetworkProfile.DisplayNameOrDefault(), prof.Name))
6161
}
6262
}
6363
return words, ffcomplete.ShellCompDirectiveNoFileComp, nil
@@ -86,7 +86,7 @@ func listProfiles(ctx context.Context) error {
8686
}
8787
printRow(
8888
string(prof.ID),
89-
prof.NetworkProfile.DomainName,
89+
prof.NetworkProfile.DisplayNameOrDefault(),
9090
name,
9191
)
9292
}
@@ -107,7 +107,7 @@ func switchProfile(ctx context.Context, args []string) error {
107107
os.Exit(1)
108108
}
109109
var profID ipn.ProfileID
110-
// Allow matching by ID, Tailnet, or Account
110+
// Allow matching by ID, Tailnet, Account, or Display Name
111111
// in that order.
112112
for _, p := range all {
113113
if p.ID == ipn.ProfileID(args[0]) {
@@ -131,6 +131,14 @@ func switchProfile(ctx context.Context, args []string) error {
131131
}
132132
}
133133
}
134+
if profID == "" {
135+
for _, p := range all {
136+
if p.NetworkProfile.DisplayName == args[0] {
137+
profID = p.ID
138+
break
139+
}
140+
}
141+
}
134142
if profID == "" {
135143
errf("No profile named %q\n", args[0])
136144
os.Exit(1)

ipn/prefs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package ipn
55

66
import (
77
"bytes"
8+
"cmp"
89
"encoding/json"
910
"errors"
1011
"fmt"
@@ -1001,6 +1002,13 @@ func (n NetworkProfile) RequiresBackfill() bool {
10011002
return n == NetworkProfile{}
10021003
}
10031004

1005+
// DisplayNameOrDefault will always return a non-empty string.
1006+
// If there is a defined display name, it will return that.
1007+
// If they did not it will default to their domain name.
1008+
func (n NetworkProfile) DisplayNameOrDefault() string {
1009+
return cmp.Or(n.DisplayName, n.DomainName)
1010+
}
1011+
10041012
// LoginProfile represents a single login profile as managed
10051013
// by the ProfileManager.
10061014
type LoginProfile struct {

0 commit comments

Comments
 (0)