From 423d43b4df5bfd36c4c23086b476c3643343271d Mon Sep 17 00:00:00 2001 From: Ethan Dickson Date: Wed, 11 Sep 2024 12:20:09 +0000 Subject: [PATCH] chore: read template tar from stdin if stdin is not a tty --- cli/login.go | 2 +- cli/root.go | 8 ++++---- cli/templatecreate.go | 6 +++--- cli/templatepush.go | 23 +++++++++++++++-------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/cli/login.go b/cli/login.go index 834ba73ce38a0..a6cf7fc04ce22 100644 --- a/cli/login.go +++ b/cli/login.go @@ -212,7 +212,7 @@ func (r *RootCmd) login() *serpent.Command { _, _ = fmt.Fprintf(inv.Stdout, Caret+"Your Coder deployment hasn't been set up!\n") if username == "" { - if !isTTY(inv) { + if !isTTYIn(inv) { return xerrors.New("the initial user cannot be created in non-interactive mode. use the API") } diff --git a/cli/root.go b/cli/root.go index 4d3b0e5a85fd6..4945ecdd1656c 100644 --- a/cli/root.go +++ b/cli/root.go @@ -692,8 +692,8 @@ func (r *RootCmd) createConfig() config.Root { return config.Root(r.globalConfig) } -// isTTY returns whether the passed reader is a TTY or not. -func isTTY(inv *serpent.Invocation) bool { +// isTTYIn returns whether the passed invocation is having stdin read from a TTY +func isTTYIn(inv *serpent.Invocation) bool { // If the `--force-tty` command is available, and set, // assume we're in a tty. This is primarily for cases on Windows // where we may not be able to reliably detect this automatically (ie, tests) @@ -708,12 +708,12 @@ func isTTY(inv *serpent.Invocation) bool { return isatty.IsTerminal(file.Fd()) } -// isTTYOut returns whether the passed reader is a TTY or not. +// isTTYOut returns whether the passed invocation is having stdout written to a TTY func isTTYOut(inv *serpent.Invocation) bool { return isTTYWriter(inv, inv.Stdout) } -// isTTYErr returns whether the passed reader is a TTY or not. +// isTTYErr returns whether the passed invocation is having stderr written to a TTY func isTTYErr(inv *serpent.Invocation) bool { return isTTYWriter(inv, inv.Stderr) } diff --git a/cli/templatecreate.go b/cli/templatecreate.go index f294e7d06fbf0..beef00650847c 100644 --- a/cli/templatecreate.go +++ b/cli/templatecreate.go @@ -74,7 +74,7 @@ func (r *RootCmd) templateCreate() *serpent.Command { return err } - templateName, err := uploadFlags.templateName(inv.Args) + templateName, err := uploadFlags.templateName(inv) if err != nil { return err } @@ -96,7 +96,7 @@ func (r *RootCmd) templateCreate() *serpent.Command { message := uploadFlags.templateMessage(inv) var varsFiles []string - if !uploadFlags.stdin() { + if !uploadFlags.stdin(inv) { varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory) if err != nil { return err @@ -139,7 +139,7 @@ func (r *RootCmd) templateCreate() *serpent.Command { return err } - if !uploadFlags.stdin() { + if !uploadFlags.stdin(inv) { _, err = cliui.Prompt(inv, cliui.PromptOptions{ Text: "Confirm create?", IsConfirm: true, diff --git a/cli/templatepush.go b/cli/templatepush.go index e8bba31c818cc..f5ff1dcb3cf85 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -51,7 +51,7 @@ func (r *RootCmd) templatePush() *serpent.Command { return err } - name, err := uploadFlags.templateName(inv.Args) + name, err := uploadFlags.templateName(inv) if err != nil { return err } @@ -87,7 +87,7 @@ func (r *RootCmd) templatePush() *serpent.Command { message := uploadFlags.templateMessage(inv) var varsFiles []string - if !uploadFlags.stdin() { + if !uploadFlags.stdin(inv) { varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory) if err != nil { return err @@ -275,13 +275,19 @@ func (pf *templateUploadFlags) setWorkdir(wd string) { } } -func (pf *templateUploadFlags) stdin() bool { - return pf.directory == "-" +func (pf *templateUploadFlags) stdin(inv *serpent.Invocation) (out bool) { + defer func() { + if out { + inv.Logger.Info(inv.Context(), "uploading tar read from stdin") + } + }() + // We let the directory override our isTTY check + return pf.directory == "-" || (!isTTYIn(inv) && pf.directory == "") } func (pf *templateUploadFlags) upload(inv *serpent.Invocation, client *codersdk.Client) (*codersdk.UploadResponse, error) { var content io.Reader - if pf.stdin() { + if pf.stdin(inv) { content = inv.Stdin } else { prettyDir := prettyDirectoryPath(pf.directory) @@ -317,7 +323,7 @@ func (pf *templateUploadFlags) upload(inv *serpent.Invocation, client *codersdk. } func (pf *templateUploadFlags) checkForLockfile(inv *serpent.Invocation) error { - if pf.stdin() || pf.ignoreLockfile { + if pf.stdin(inv) || pf.ignoreLockfile { // Just assume there's a lockfile if reading from stdin. return nil } @@ -350,8 +356,9 @@ func (pf *templateUploadFlags) templateMessage(inv *serpent.Invocation) string { return "Uploaded from the CLI" } -func (pf *templateUploadFlags) templateName(args []string) (string, error) { - if pf.stdin() { +func (pf *templateUploadFlags) templateName(inv *serpent.Invocation) (string, error) { + args := inv.Args + if pf.stdin(inv) { // Can't infer name from directory if none provided. if len(args) == 0 { return "", xerrors.New("template name argument must be provided")