cmd/tailscale: add ip -1 flag

This limits the output to a single IP address.

RELNOTE=tailscale ip now has a -1 flag (TODO: update docs to use it)

Fixes #1921

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
pull/3392/head
Josh Bleecher Snyder 3 years ago committed by Josh Bleecher Snyder
parent 73beaf59fb
commit b0d543f7a1

@ -18,12 +18,13 @@ import (
var ipCmd = &ffcli.Command{ var ipCmd = &ffcli.Command{
Name: "ip", Name: "ip",
ShortUsage: "ip [-4] [-6] [peer hostname or ip address]", ShortUsage: "ip [-1] [-4] [-6] [peer hostname or ip address]",
ShortHelp: "Show Tailscale IP addresses", ShortHelp: "Show Tailscale IP addresses",
LongHelp: "Show Tailscale IP addresses for peer. Peer defaults to the current machine.", LongHelp: "Show Tailscale IP addresses for peer. Peer defaults to the current machine.",
Exec: runIP, Exec: runIP,
FlagSet: (func() *flag.FlagSet { FlagSet: (func() *flag.FlagSet {
fs := newFlagSet("ip") fs := newFlagSet("ip")
fs.BoolVar(&ipArgs.want1, "1", false, "only print one IP address")
fs.BoolVar(&ipArgs.want4, "4", false, "only print IPv4 address") fs.BoolVar(&ipArgs.want4, "4", false, "only print IPv4 address")
fs.BoolVar(&ipArgs.want6, "6", false, "only print IPv6 address") fs.BoolVar(&ipArgs.want6, "6", false, "only print IPv6 address")
return fs return fs
@ -31,6 +32,7 @@ var ipCmd = &ffcli.Command{
} }
var ipArgs struct { var ipArgs struct {
want1 bool
want4 bool want4 bool
want6 bool want6 bool
} }
@ -45,8 +47,14 @@ func runIP(ctx context.Context, args []string) error {
} }
v4, v6 := ipArgs.want4, ipArgs.want6 v4, v6 := ipArgs.want4, ipArgs.want6
if v4 && v6 { nflags := 0
return errors.New("tailscale ip -4 and -6 are mutually exclusive") for _, b := range []bool{ipArgs.want1, v4, v6} {
if b {
nflags++
}
}
if nflags > 1 {
return errors.New("tailscale ip -1, -4, and -6 are mutually exclusive")
} }
if !v4 && !v6 { if !v4 && !v6 {
v4, v6 = true, true v4, v6 = true, true
@ -71,6 +79,9 @@ func runIP(ctx context.Context, args []string) error {
return fmt.Errorf("no current Tailscale IPs; state: %v", st.BackendState) return fmt.Errorf("no current Tailscale IPs; state: %v", st.BackendState)
} }
if ipArgs.want1 {
ips = ips[:1]
}
match := false match := false
for _, ip := range ips { for _, ip := range ips {
if ip.Is4() && v4 || ip.Is6() && v6 { if ip.Is4() && v4 || ip.Is6() && v6 {

Loading…
Cancel
Save