Skip to content

Commit 8079188

Browse files
committed
chore: read template tar from stdin if stdin is not a tty
1 parent 1b5f341 commit 8079188

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

cli/login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (r *RootCmd) login() *serpent.Command {
212212
_, _ = fmt.Fprintf(inv.Stdout, Caret+"Your Coder deployment hasn't been set up!\n")
213213

214214
if username == "" {
215-
if !isTTY(inv) {
215+
if !isTTYIn(inv) {
216216
return xerrors.New("the initial user cannot be created in non-interactive mode. use the API")
217217
}
218218

cli/root.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,8 @@ func (r *RootCmd) createConfig() config.Root {
692692
return config.Root(r.globalConfig)
693693
}
694694

695-
// isTTY returns whether the passed reader is a TTY or not.
696-
func isTTY(inv *serpent.Invocation) bool {
695+
// isTTYIn returns whether the passed invocation is having stdin read from a TTY
696+
func isTTYIn(inv *serpent.Invocation) bool {
697697
// If the `--force-tty` command is available, and set,
698698
// assume we're in a tty. This is primarily for cases on Windows
699699
// where we may not be able to reliably detect this automatically (ie, tests)
@@ -708,12 +708,12 @@ func isTTY(inv *serpent.Invocation) bool {
708708
return isatty.IsTerminal(file.Fd())
709709
}
710710

711-
// isTTYOut returns whether the passed reader is a TTY or not.
711+
// isTTYOut returns whether the passed invocation is having stdout written to a TTY
712712
func isTTYOut(inv *serpent.Invocation) bool {
713713
return isTTYWriter(inv, inv.Stdout)
714714
}
715715

716-
// isTTYErr returns whether the passed reader is a TTY or not.
716+
// isTTYErr returns whether the passed invocation is having stderr written to a TTY
717717
func isTTYErr(inv *serpent.Invocation) bool {
718718
return isTTYWriter(inv, inv.Stderr)
719719
}

cli/templatecreate.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
7474
return err
7575
}
7676

77-
templateName, err := uploadFlags.templateName(inv.Args)
77+
templateName, err := uploadFlags.templateName(inv)
7878
if err != nil {
7979
return err
8080
}
@@ -96,7 +96,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
9696
message := uploadFlags.templateMessage(inv)
9797

9898
var varsFiles []string
99-
if !uploadFlags.stdin() {
99+
if !uploadFlags.stdin(inv) {
100100
varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory)
101101
if err != nil {
102102
return err
@@ -139,7 +139,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
139139
return err
140140
}
141141

142-
if !uploadFlags.stdin() {
142+
if !uploadFlags.stdin(inv) {
143143
_, err = cliui.Prompt(inv, cliui.PromptOptions{
144144
Text: "Confirm create?",
145145
IsConfirm: true,

cli/templatepush.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (r *RootCmd) templatePush() *serpent.Command {
5151
return err
5252
}
5353

54-
name, err := uploadFlags.templateName(inv.Args)
54+
name, err := uploadFlags.templateName(inv)
5555
if err != nil {
5656
return err
5757
}
@@ -87,7 +87,7 @@ func (r *RootCmd) templatePush() *serpent.Command {
8787
message := uploadFlags.templateMessage(inv)
8888

8989
var varsFiles []string
90-
if !uploadFlags.stdin() {
90+
if !uploadFlags.stdin(inv) {
9191
varsFiles, err = codersdk.DiscoverVarsFiles(uploadFlags.directory)
9292
if err != nil {
9393
return err
@@ -275,13 +275,13 @@ func (pf *templateUploadFlags) setWorkdir(wd string) {
275275
}
276276
}
277277

278-
func (pf *templateUploadFlags) stdin() bool {
279-
return pf.directory == "-"
278+
func (pf *templateUploadFlags) stdin(inv *serpent.Invocation) bool {
279+
return pf.directory == "-" || (!isTTYIn(inv) && pf.directory == "")
280280
}
281281

282282
func (pf *templateUploadFlags) upload(inv *serpent.Invocation, client *codersdk.Client) (*codersdk.UploadResponse, error) {
283283
var content io.Reader
284-
if pf.stdin() {
284+
if pf.stdin(inv) {
285285
content = inv.Stdin
286286
} else {
287287
prettyDir := prettyDirectoryPath(pf.directory)
@@ -317,7 +317,7 @@ func (pf *templateUploadFlags) upload(inv *serpent.Invocation, client *codersdk.
317317
}
318318

319319
func (pf *templateUploadFlags) checkForLockfile(inv *serpent.Invocation) error {
320-
if pf.stdin() || pf.ignoreLockfile {
320+
if pf.stdin(inv) || pf.ignoreLockfile {
321321
// Just assume there's a lockfile if reading from stdin.
322322
return nil
323323
}
@@ -350,8 +350,9 @@ func (pf *templateUploadFlags) templateMessage(inv *serpent.Invocation) string {
350350
return "Uploaded from the CLI"
351351
}
352352

353-
func (pf *templateUploadFlags) templateName(args []string) (string, error) {
354-
if pf.stdin() {
353+
func (pf *templateUploadFlags) templateName(inv *serpent.Invocation) (string, error) {
354+
args := inv.Args
355+
if pf.stdin(inv) {
355356
// Can't infer name from directory if none provided.
356357
if len(args) == 0 {
357358
return "", xerrors.New("template name argument must be provided")

0 commit comments

Comments
 (0)