From 4c0841001176e1b55a5e6880da07440aa961435a Mon Sep 17 00:00:00 2001 From: Paul Scott Date: Wed, 1 May 2024 16:24:55 +0100 Subject: [PATCH] cmd/tailscale/cli: set localClient.UseSocketOnly during flag parsing This configures localClient correctly during flag parsing, so that the --socket option is effective when generating tab-completion results. For example, the following would not connect to the system Tailscale for tab-completion results: tailscale --socket=/tmp/tailscaled.socket switch Updates #3793 Signed-off-by: Paul Scott --- cmd/tailscale/cli/cli.go | 22 +++++++++------------- cmd/tailscale/cli/ssh.go | 4 ++-- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/cmd/tailscale/cli/cli.go b/cmd/tailscale/cli/cli.go index 2c952a1c0..929b63958 100644 --- a/cmd/tailscale/cli/cli.go +++ b/cmd/tailscale/cli/cli.go @@ -78,7 +78,9 @@ func CleanUpArgs(args []string) []string { return out } -var localClient tailscale.LocalClient +var localClient = tailscale.LocalClient{ + Socket: paths.DefaultTailscaledSocket(), +} // Run runs the CLI. The args do not include the binary name. func Run(args []string) (err error) { @@ -139,13 +141,6 @@ func Run(args []string) (err error) { return } - localClient.Socket = rootArgs.socket - rootCmd.FlagSet.Visit(func(f *flag.Flag) { - if f.Name == "socket" { - localClient.UseSocketOnly = true - } - }) - err = rootCmd.Run(context.Background()) if tailscale.IsAccessDeniedError(err) && os.Getuid() != 0 && runtime.GOOS != "windows" { return fmt.Errorf("%v\n\nUse 'sudo tailscale %s' or 'tailscale up --operator=$USER' to not require root.", err, strings.Join(args, " ")) @@ -158,7 +153,12 @@ func Run(args []string) (err error) { func newRootCmd() *ffcli.Command { rootfs := newFlagSet("tailscale") - rootfs.StringVar(&rootArgs.socket, "socket", paths.DefaultTailscaledSocket(), "path to tailscaled socket") + rootfs.Func("socket", "path to tailscaled socket", func(s string) error { + localClient.Socket = s + localClient.UseSocketOnly = true + return nil + }) + rootfs.Lookup("socket").DefValue = localClient.Socket rootCmd := &ffcli.Command{ Name: "tailscale", @@ -236,10 +236,6 @@ func fatalf(format string, a ...any) { // Fatalf, if non-nil, is used instead of log.Fatalf. var Fatalf func(format string, a ...any) -var rootArgs struct { - socket string -} - type cmdWalk struct { *ffcli.Command parents []*ffcli.Command diff --git a/cmd/tailscale/cli/ssh.go b/cmd/tailscale/cli/ssh.go index 1aceb10bb..68a6193af 100644 --- a/cmd/tailscale/cli/ssh.go +++ b/cmd/tailscale/cli/ssh.go @@ -110,8 +110,8 @@ func runSSH(ctx context.Context, args []string) error { // mode, so 'nc' isn't very useful. if runtime.GOOS != "darwin" { socketArg := "" - if rootArgs.socket != "" && rootArgs.socket != paths.DefaultTailscaledSocket() { - socketArg = fmt.Sprintf("--socket=%q", rootArgs.socket) + if localClient.Socket != "" && localClient.Socket != paths.DefaultTailscaledSocket() { + socketArg = fmt.Sprintf("--socket=%q", localClient.Socket) } argv = append(argv,