cmd/tailscale/cli: add RunWithContext

Fixes #12778

Signed-off-by: DeedleFake <deedlefake@users.noreply.github.com>
pull/13867/head
DeedleFake 2 weeks ago
parent fd77965f23
commit 67ef00a8bf

@ -82,8 +82,8 @@ var localClient = tailscale.LocalClient{
Socket: paths.DefaultTailscaledSocket(), Socket: paths.DefaultTailscaledSocket(),
} }
// Run runs the CLI. The args do not include the binary name. // RunWithContext runs the CLI. The args do not include the binary name.
func Run(args []string) (err error) { func RunWithContext(ctx context.Context, args []string) (err error) {
if runtime.GOOS == "linux" && os.Getenv("GOKRAZY_FIRST_START") == "1" && distro.Get() == distro.Gokrazy && os.Getppid() == 1 { if runtime.GOOS == "linux" && os.Getenv("GOKRAZY_FIRST_START") == "1" && distro.Get() == distro.Gokrazy && os.Getppid() == 1 {
// We're running on gokrazy and it's the first start. // We're running on gokrazy and it's the first start.
// Don't run the tailscale CLI as a service; just exit. // Don't run the tailscale CLI as a service; just exit.
@ -148,7 +148,7 @@ func Run(args []string) (err error) {
return return
} }
err = rootCmd.Run(context.Background()) err = rootCmd.Run(ctx)
if tailscale.IsAccessDeniedError(err) && os.Getuid() != 0 && runtime.GOOS != "windows" { 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, " ")) return fmt.Errorf("%v\n\nUse 'sudo tailscale %s' or 'tailscale up --operator=$USER' to not require root.", err, strings.Join(args, " "))
} }
@ -158,6 +158,11 @@ func Run(args []string) (err error) {
return err return err
} }
// Run is equivalent to calling [RunWithContext] with the background context.
func Run(args []string) (err error) {
return RunWithContext(context.Background(), args)
}
func newRootCmd() *ffcli.Command { func newRootCmd() *ffcli.Command {
rootfs := newFlagSet("tailscale") rootfs := newFlagSet("tailscale")
rootfs.Func("socket", "path to tailscaled socket", func(s string) error { rootfs.Func("socket", "path to tailscaled socket", func(s string) error {

Loading…
Cancel
Save