Skip to content

chore: simplify error handling in template push #6065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ func (pf *templateUploadFlags) stdin() bool {

func (pf *templateUploadFlags) upload(cmd *cobra.Command, client *codersdk.Client) (*codersdk.UploadResponse, error) {
var (
content io.Reader
pipeErrCh = make(chan error, 1)
content io.Reader
)
if pf.stdin() {
content = cmd.InOrStdin()
// No piping if reading from stdin.
pipeErrCh <- nil
close(pipeErrCh)
} else {
prettyDir := prettyDirectoryPath(pf.directory)
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
Expand All @@ -56,11 +52,8 @@ func (pf *templateUploadFlags) upload(cmd *cobra.Command, client *codersdk.Clien

pipeReader, pipeWriter := io.Pipe()
go func() {
defer pipeWriter.Close()
defer close(pipeErrCh)
bufWr := bufio.NewWriter(pipeWriter)
defer bufWr.Flush()
pipeErrCh <- provisionersdk.Tar(bufWr, pf.directory, provisionersdk.TemplateArchiveLimit)
err := provisionersdk.Tar(pipeWriter, pf.directory, provisionersdk.TemplateArchiveLimit)
_ = pipeWriter.CloseWithError(err)
}()
defer pipeReader.Close()
content = pipeReader
Expand All @@ -72,13 +65,10 @@ func (pf *templateUploadFlags) upload(cmd *cobra.Command, client *codersdk.Clien
spin.Start()
defer spin.Stop()

resp, err := client.Upload(cmd.Context(), codersdk.ContentTypeTar, content)
resp, err := client.Upload(cmd.Context(), codersdk.ContentTypeTar, bufio.NewReader(content))
if err != nil {
return nil, xerrors.Errorf("upload: %w", err)
}
if err = <-pipeErrCh; err != nil {
return nil, xerrors.Errorf("pipe: %w", err)
}
return &resp, nil
}

Expand Down