-
Notifications
You must be signed in to change notification settings - Fork 937
feat: Add template pull cmd #2329
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"sort" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func templatePull() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "pull <name> [destination]", | ||
Short: "Download the latest version of a template to a path.", | ||
Args: cobra.MaximumNArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var ( | ||
ctx = cmd.Context() | ||
templateName = args[0] | ||
dest string | ||
) | ||
|
||
if len(args) > 1 { | ||
dest = args[1] | ||
} | ||
|
||
client, err := createClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create client: %w", err) | ||
} | ||
|
||
// TODO(JonA): Do we need to add a flag for organization? | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return xerrors.Errorf("current organization: %w", err) | ||
} | ||
|
||
template, err := client.TemplateByName(ctx, organization.ID, templateName) | ||
if err != nil { | ||
return xerrors.Errorf("template by name: %w", err) | ||
} | ||
|
||
// Pull the versions for the template. We'll find the latest | ||
// one and download the source. | ||
versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{ | ||
TemplateID: template.ID, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("template versions by template: %w", err) | ||
} | ||
|
||
if len(versions) == 0 { | ||
return xerrors.Errorf("no template versions for template %q", templateName) | ||
} | ||
|
||
// Sort the slice from newest to oldest template. | ||
sort.SliceStable(versions, func(i, j int) bool { | ||
return versions[i].CreatedAt.After(versions[j].CreatedAt) | ||
}) | ||
|
||
latest := versions[0] | ||
|
||
// Download the tar archive. | ||
raw, ctype, err := client.Download(ctx, latest.Job.StorageSource) | ||
if err != nil { | ||
return xerrors.Errorf("download template: %w", err) | ||
} | ||
|
||
if ctype != codersdk.ContentTypeTar { | ||
return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar) | ||
} | ||
|
||
// If the destination is empty then we write to stdout | ||
// and bail early. | ||
if dest == "" { | ||
_, err = cmd.OutOrStdout().Write(raw) | ||
if err != nil { | ||
return xerrors.Errorf("write stdout: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// Stat the destination to ensure nothing exists already. | ||
fi, err := os.Stat(dest) | ||
if err != nil && !xerrors.Is(err, fs.ErrNotExist) { | ||
return xerrors.Errorf("stat destination: %w", err) | ||
} | ||
|
||
if fi != nil && fi.IsDir() { | ||
// If the destination is a directory we just bail. | ||
return xerrors.Errorf("%q already exists.", dest) | ||
} | ||
|
||
// If a file exists at the destination prompt the user | ||
// to ensure we don't overwrite something valuable. | ||
if fi != nil { | ||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{ | ||
Text: fmt.Sprintf("%q already exists, do you want to overwrite it?", dest), | ||
IsConfirm: true, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("parse prompt: %w", err) | ||
} | ||
} | ||
|
||
err = os.WriteFile(dest, raw, 0600) | ||
if err != nil { | ||
return xerrors.Errorf("write to path: %w", err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cliui.AllowSkipPrompt(cmd) | ||
|
||
return cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/provisioner/echo" | ||
"github.com/coder/coder/provisionersdk/proto" | ||
"github.com/coder/coder/pty/ptytest" | ||
) | ||
|
||
func TestTemplatePull(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Stdout tests that 'templates pull' pulls down the latest template | ||
// and writes it to stdout. | ||
t.Run("Stdout", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
|
||
// Create an initial template bundle. | ||
source1 := genTemplateVersionSource() | ||
// Create an updated template bundle. This will be used to ensure | ||
// that templates are correctly returned in order from latest to oldest. | ||
source2 := genTemplateVersionSource() | ||
|
||
expected, err := echo.Tar(source2) | ||
require.NoError(t, err) | ||
|
||
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, source1) | ||
_ = coderdtest.AwaitTemplateVersionJob(t, client, version1.ID) | ||
|
||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID) | ||
|
||
// Update the template version so that we can assert that templates | ||
// are being sorted correctly. | ||
_ = coderdtest.UpdateTemplateVersion(t, client, user.OrganizationID, source2, template.ID) | ||
|
||
cmd, root := clitest.New(t, "templates", "pull", template.Name) | ||
clitest.SetupConfig(t, client, root) | ||
|
||
var buf bytes.Buffer | ||
cmd.SetOut(&buf) | ||
|
||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
require.True(t, bytes.Equal(expected, buf.Bytes()), "tar files differ") | ||
}) | ||
|
||
// ToFile tests that 'templates pull' pulls down the latest template | ||
// and writes it to the correct directory. | ||
t.Run("ToFile", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
|
||
// Create an initial template bundle. | ||
source1 := genTemplateVersionSource() | ||
// Create an updated template bundle. This will be used to ensure | ||
// that templates are correctly returned in order from latest to oldest. | ||
source2 := genTemplateVersionSource() | ||
|
||
expected, err := echo.Tar(source2) | ||
require.NoError(t, err) | ||
|
||
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, source1) | ||
_ = coderdtest.AwaitTemplateVersionJob(t, client, version1.ID) | ||
|
||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID) | ||
|
||
// Update the template version so that we can assert that templates | ||
// are being sorted correctly. | ||
_ = coderdtest.UpdateTemplateVersion(t, client, user.OrganizationID, source2, template.ID) | ||
|
||
dir := t.TempDir() | ||
|
||
dest := filepath.Join(dir, "actual.tar") | ||
|
||
// Create the file so that we can test that the command | ||
// warns the user before overwriting a preexisting file. | ||
fi, err := os.OpenFile(dest, os.O_CREATE|os.O_RDONLY, 0600) | ||
require.NoError(t, err) | ||
_ = fi.Close() | ||
|
||
cmd, root := clitest.New(t, "templates", "pull", template.Name, dest) | ||
clitest.SetupConfig(t, client, root) | ||
|
||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetOut(pty.Output()) | ||
|
||
errChan := make(chan error) | ||
go func() { | ||
defer close(errChan) | ||
errChan <- cmd.Execute() | ||
}() | ||
|
||
// We expect to be prompted that a file already exists. | ||
pty.ExpectMatch("already exists") | ||
pty.WriteLine("yes") | ||
|
||
require.NoError(t, <-errChan) | ||
|
||
actual, err := os.ReadFile(dest) | ||
require.NoError(t, err) | ||
|
||
require.True(t, bytes.Equal(actual, expected), "tar files differ") | ||
}) | ||
} | ||
|
||
// genTemplateVersionSource returns a unique bundle that can be used to create | ||
// a template version source. | ||
func genTemplateVersionSource() *echo.Responses { | ||
return &echo.Responses{ | ||
Parse: []*proto.Parse_Response{ | ||
{ | ||
Type: &proto.Parse_Response_Log{ | ||
Log: &proto.Log{ | ||
Output: uuid.NewString(), | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
Type: &proto.Parse_Response_Complete{ | ||
Complete: &proto.Parse_Complete{}, | ||
}, | ||
}, | ||
}, | ||
Provision: echo.ProvisionComplete, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.