mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
feat(cli): allow direct tar upload in template update/create (#5720)
This commit is contained in:
@ -2,12 +2,14 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/briandowns/spinner"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/cli/cliui"
|
||||
@ -16,14 +18,81 @@ import (
|
||||
"github.com/coder/coder/provisionersdk"
|
||||
)
|
||||
|
||||
// templateUploadFlags is shared by `templates create` and `templates push`.
|
||||
type templateUploadFlags struct {
|
||||
directory string
|
||||
}
|
||||
|
||||
func (pf *templateUploadFlags) register(f *pflag.FlagSet) {
|
||||
currentDirectory, _ := os.Getwd()
|
||||
f.StringVarP(&pf.directory, "directory", "d", currentDirectory, "Specify the directory to create from, use '-' to read tar from stdin")
|
||||
}
|
||||
|
||||
func (pf *templateUploadFlags) stdin() bool {
|
||||
return pf.directory == "-"
|
||||
}
|
||||
|
||||
func (pf *templateUploadFlags) upload(cmd *cobra.Command, client *codersdk.Client) (*codersdk.UploadResponse, error) {
|
||||
var (
|
||||
content []byte
|
||||
err error
|
||||
)
|
||||
if pf.stdin() {
|
||||
content, err = io.ReadAll(cmd.InOrStdin())
|
||||
} else {
|
||||
prettyDir := prettyDirectoryPath(pf.directory)
|
||||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf("Upload %q?", prettyDir),
|
||||
IsConfirm: true,
|
||||
Default: cliui.ConfirmYes,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
content, err = provisionersdk.Tar(pf.directory, provisionersdk.TemplateArchiveLimit)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("read tar: %w", err)
|
||||
}
|
||||
|
||||
spin := spinner.New(spinner.CharSets[5], 100*time.Millisecond)
|
||||
spin.Writer = cmd.OutOrStdout()
|
||||
spin.Suffix = cliui.Styles.Keyword.Render(" Uploading directory...")
|
||||
spin.Start()
|
||||
defer spin.Stop()
|
||||
|
||||
resp, err := client.Upload(cmd.Context(), codersdk.ContentTypeTar, content)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("upload: %w", err)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (pf *templateUploadFlags) templateName(args []string) (string, error) {
|
||||
if pf.stdin() {
|
||||
// Can't infer name from directory if none provided.
|
||||
if len(args) == 0 {
|
||||
return "", xerrors.New("template name argument must be provided")
|
||||
}
|
||||
return args[0], nil
|
||||
}
|
||||
|
||||
name := filepath.Base(pf.directory)
|
||||
if len(args) > 0 {
|
||||
name = args[0]
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func templatePush() *cobra.Command {
|
||||
var (
|
||||
directory string
|
||||
versionName string
|
||||
provisioner string
|
||||
parameterFile string
|
||||
alwaysPrompt bool
|
||||
provisionerTags []string
|
||||
uploadFlags templateUploadFlags
|
||||
)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
@ -40,9 +109,9 @@ func templatePush() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
name := filepath.Base(directory)
|
||||
if len(args) > 0 {
|
||||
name = args[0]
|
||||
name, err := uploadFlags.templateName(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
template, err := client.TemplateByName(cmd.Context(), organization.ID, name)
|
||||
@ -50,32 +119,11 @@ func templatePush() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
// Confirm upload of the directory.
|
||||
prettyDir := prettyDirectoryPath(directory)
|
||||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf("Upload %q?", prettyDir),
|
||||
IsConfirm: true,
|
||||
Default: cliui.ConfirmYes,
|
||||
})
|
||||
resp, err := uploadFlags.upload(cmd, client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
spin := spinner.New(spinner.CharSets[5], 100*time.Millisecond)
|
||||
spin.Writer = cmd.OutOrStdout()
|
||||
spin.Suffix = cliui.Styles.Keyword.Render(" Uploading directory...")
|
||||
spin.Start()
|
||||
defer spin.Stop()
|
||||
content, err := provisionersdk.Tar(directory, provisionersdk.TemplateArchiveLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := client.Upload(cmd.Context(), codersdk.ContentTypeTar, content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
spin.Stop()
|
||||
|
||||
tags, err := ParseProvisionerTags(provisionerTags)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -112,13 +160,12 @@ func templatePush() *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
currentDirectory, _ := os.Getwd()
|
||||
cmd.Flags().StringVarP(&directory, "directory", "d", currentDirectory, "Specify the directory to create from")
|
||||
cmd.Flags().StringVarP(&provisioner, "test.provisioner", "", "terraform", "Customize the provisioner backend")
|
||||
cmd.Flags().StringVarP(¶meterFile, "parameter-file", "", "", "Specify a file path with parameter values.")
|
||||
cmd.Flags().StringVarP(&versionName, "name", "", "", "Specify a name for the new template version. It will be automatically generated if not provided.")
|
||||
cmd.Flags().StringArrayVarP(&provisionerTags, "provisioner-tag", "", []string{}, "Specify a set of tags to target provisioner daemons.")
|
||||
cmd.Flags().BoolVar(&alwaysPrompt, "always-prompt", false, "Always prompt all parameters. Does not pull parameter values from active template version")
|
||||
uploadFlags.register(cmd.Flags())
|
||||
cliui.AllowSkipPrompt(cmd)
|
||||
// This is for testing!
|
||||
err := cmd.Flags().MarkHidden("test.provisioner")
|
||||
|
Reference in New Issue
Block a user