Skip to content

Commit f867a9d

Browse files
fix(cli/templatepush): only implicitly read from stdin if the directory flag is unset (#19681)
In trying to address confusion with the `-` (for stdin) directory flag last year, I had `template push` read from stdin if stdin was not a TTY. However, I made the mistake of checking if the directory flag was set or not by comparing it to the default value. This meant in something like GitHub Actions, where you don't have a TTY for stdin, it was impossible to read from the current working directory. The fix is just to check if the flag was explicitly set, using pflags. If users encounter this bug, and this fix is unavailable in their version of Coder, they can workaround it by setting `-d "$(pwd)"`
1 parent 50704a5 commit f867a9d

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

cli/templatepush.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ func (pf *templateUploadFlags) stdin(inv *serpent.Invocation) (out bool) {
309309
inv.Logger.Info(inv.Context(), "uploading tar read from stdin")
310310
}
311311
}()
312-
// We let the directory override our isTTY check
313-
return pf.directory == "-" || (!isTTYIn(inv) && pf.directory == ".")
312+
// We read a tar from stdin if the directory is "-" or if we're not in a
313+
// TTY and the directory flag is unset.
314+
return pf.directory == "-" || (!isTTYIn(inv) && !inv.ParsedFlags().Lookup("directory").Changed)
314315
}
315316

316317
func (pf *templateUploadFlags) upload(inv *serpent.Invocation, client *codersdk.Client) (*codersdk.UploadResponse, error) {

cli/templatepush_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ func TestTemplatePush(t *testing.T) {
339339
inv, root := clitest.New(t, "templates", "push",
340340
"--test.provisioner", string(database.ProvisionerTypeEcho),
341341
"--test.workdir", source,
342+
"--force-tty",
342343
)
343344
clitest.SetupConfig(t, templateAdmin, root)
344345
pty := ptytest.New(t).Attach(inv)
@@ -1075,6 +1076,45 @@ func TestTemplatePush(t *testing.T) {
10751076
require.Equal(t, templateName, template.Name)
10761077
require.NotEqual(t, uuid.Nil, template.ActiveVersionID)
10771078
})
1079+
1080+
t.Run("NoStdinWithCurrentDirectory", func(t *testing.T) {
1081+
t.Parallel()
1082+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
1083+
owner := coderdtest.CreateFirstUser(t, client)
1084+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
1085+
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
1086+
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
1087+
1088+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
1089+
1090+
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
1091+
Parse: echo.ParseComplete,
1092+
ProvisionApply: echo.ApplyComplete,
1093+
})
1094+
1095+
inv, root := clitest.New(t, "templates", "push", template.Name,
1096+
"--directory", ".",
1097+
"--test.provisioner", string(database.ProvisionerTypeEcho),
1098+
"--test.workdir", source,
1099+
"--name", "example",
1100+
"--yes")
1101+
clitest.SetupConfig(t, templateAdmin, root)
1102+
1103+
inv.Stdin = strings.NewReader("invalid tar content that would cause failure")
1104+
1105+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
1106+
defer cancel()
1107+
1108+
err := inv.WithContext(ctx).Run()
1109+
require.NoError(t, err, "Should succeed without reading from stdin")
1110+
1111+
templateVersions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
1112+
TemplateID: template.ID,
1113+
})
1114+
require.NoError(t, err)
1115+
require.Len(t, templateVersions, 2)
1116+
require.Equal(t, "example", templateVersions[1].Name)
1117+
})
10781118
})
10791119
}
10801120

0 commit comments

Comments
 (0)