From 4c867ba6b46215a0d0d092bf177d81916d891b0f Mon Sep 17 00:00:00 2001 From: Pawel Kuc Date: Fri, 13 Dec 2024 18:28:19 +0100 Subject: [PATCH] feat: nctl auth login in non-interactive env and without set token should not block If you execute `nctl auth login` in a non-interactive environment (e.g. CI/CD) and forget to set `--api-token`, the browser should not try to open - you should see an error message instead. --- auth/login.go | 4 ++++ internal/format/print.go | 14 +++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/auth/login.go b/auth/login.go index 65e156b..56d51a9 100644 --- a/auth/login.go +++ b/auth/login.go @@ -58,6 +58,10 @@ func (l *LoginCmd) Run(ctx context.Context, command string, tk api.TokenGetter) return login(ctx, cfg, loadingRules.GetDefaultFilename(), userInfo.User, "", project(l.Organization)) } + if !format.IsInteractiveEnvironment(os.Stdout) { + return fmt.Errorf("a static API token is required in non-interactive envirtonments") + } + usePKCE := true token, err := tk.GetTokenString(ctx, l.IssuerURL, l.ClientID, usePKCE) diff --git a/internal/format/print.go b/internal/format/print.go index 93b8721..af6befc 100644 --- a/internal/format/print.go +++ b/internal/format/print.go @@ -193,11 +193,7 @@ func getPrinter(out io.Writer) (printer.Printer, error) { p := printer.Printer{ LineNumber: false, } - f, isFile := out.(*os.File) - if !isFile { - return p, nil - } - if isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd()) { + if IsInteractiveEnvironment(out) { p.Bool = printerProperty(&printer.Property{ Prefix: format(color.FgHiMagenta), Suffix: format(color.Reset), @@ -218,6 +214,14 @@ func getPrinter(out io.Writer) (printer.Printer, error) { return p, nil } +func IsInteractiveEnvironment(out io.Writer) bool { + f, isFile := out.(*os.File) + if !isFile { + return false + } + return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd()) +} + // stripObj removes some fields which simply add clutter to the yaml output. // The object should still be applyable afterwards as just defaults and // computed fields get removed.