@@ -13,66 +13,70 @@ import (
13
13
"github.com/coder/coder/codersdk"
14
14
)
15
15
16
- func templatePull () * cobra.Command {
17
- cmd := & cobra.Command {
18
- Use : "pull <name> [destination]" ,
19
- Short : "Download the latest version of a template to a path." ,
20
- Args : cobra .RangeArgs (1 , 2 ),
21
- RunE : func (cmd * cobra.Command , args []string ) error {
22
- var (
23
- ctx = cmd .Context ()
24
- templateName = args [0 ]
25
- dest string
26
- )
16
+ func fetchTemplateArchiveBytes (cmd * cobra.Command , templateName string ) ([]byte , error ) {
17
+ ctx := cmd .Context ()
18
+ client , err := createClient (cmd )
19
+ if err != nil {
20
+ return nil , xerrors .Errorf ("create client: %w" , err )
21
+ }
27
22
28
- if len (args ) > 1 {
29
- dest = args [1 ]
30
- }
23
+ // TODO(JonA): Do we need to add a flag for organization?
24
+ organization , err := currentOrganization (cmd , client )
25
+ if err != nil {
26
+ return nil , xerrors .Errorf ("current organization: %w" , err )
27
+ }
31
28
32
- client , err := createClient ( cmd )
33
- if err != nil {
34
- return xerrors .Errorf ("create client : %w" , err )
35
- }
29
+ template , err := client . TemplateByName ( ctx , organization . ID , templateName )
30
+ if err != nil {
31
+ return nil , xerrors .Errorf ("template by name : %w" , err )
32
+ }
36
33
37
- // TODO(JonA): Do we need to add a flag for organization?
38
- organization , err := currentOrganization (cmd , client )
39
- if err != nil {
40
- return xerrors .Errorf ("current organization: %w" , err )
41
- }
34
+ // Pull the versions for the template. We'll find the latest
35
+ // one and download the source.
36
+ versions , err := client .TemplateVersionsByTemplate (ctx , codersdk.TemplateVersionsByTemplateRequest {
37
+ TemplateID : template .ID ,
38
+ })
39
+ if err != nil {
40
+ return nil , xerrors .Errorf ("template versions by template: %w" , err )
41
+ }
42
42
43
- template , err := client .TemplateByName (ctx , organization .ID , templateName )
44
- if err != nil {
45
- return xerrors .Errorf ("template by name: %w" , err )
46
- }
43
+ if len (versions ) == 0 {
44
+ return nil , xerrors .Errorf ("no template versions for template %q" , templateName )
45
+ }
47
46
48
- // Pull the versions for the template. We'll find the latest
49
- // one and download the source.
50
- versions , err := client .TemplateVersionsByTemplate (ctx , codersdk.TemplateVersionsByTemplateRequest {
51
- TemplateID : template .ID ,
52
- })
53
- if err != nil {
54
- return xerrors .Errorf ("template versions by template: %w" , err )
55
- }
47
+ // Sort the slice from newest to oldest template.
48
+ sort .SliceStable (versions , func (i , j int ) bool {
49
+ return versions [i ].CreatedAt .After (versions [j ].CreatedAt )
50
+ })
56
51
57
- if len (versions ) == 0 {
58
- return xerrors .Errorf ("no template versions for template %q" , templateName )
59
- }
52
+ latest := versions [0 ]
60
53
61
- // Sort the slice from newest to oldest template.
62
- sort .SliceStable (versions , func (i , j int ) bool {
63
- return versions [i ].CreatedAt .After (versions [j ].CreatedAt )
64
- })
54
+ // Download the tar archive.
55
+ raw , ctype , err := client .Download (ctx , latest .Job .StorageSource )
56
+ if err != nil {
57
+ return nil , xerrors .Errorf ("download template: %w" , err )
58
+ }
65
59
66
- latest := versions [0 ]
60
+ if ctype != codersdk .ContentTypeTar {
61
+ return nil , xerrors .Errorf ("unexpected Content-Type %q, expecting %q" , ctype , codersdk .ContentTypeTar )
62
+ }
63
+ return raw , nil
64
+ }
67
65
68
- // Download the tar archive.
69
- raw , ctype , err := client .Download (ctx , latest .Job .StorageSource )
66
+ func templatePull () * cobra.Command {
67
+ cmd := & cobra.Command {
68
+ Use : "pull <name> [destination]" ,
69
+ Short : "Download the latest version of a template to a path." ,
70
+ Args : cobra .RangeArgs (1 , 2 ),
71
+ RunE : func (cmd * cobra.Command , args []string ) error {
72
+ raw , err := fetchTemplateArchiveBytes (cmd , args [0 ])
70
73
if err != nil {
71
- return xerrors . Errorf ( "download template: %w" , err )
74
+ return err
72
75
}
73
76
74
- if ctype != codersdk .ContentTypeTar {
75
- return xerrors .Errorf ("unexpected Content-Type %q, expecting %q" , ctype , codersdk .ContentTypeTar )
77
+ var dest string
78
+ if len (args ) > 1 {
79
+ dest = args [1 ]
76
80
}
77
81
78
82
// If the destination is empty then we write to stdout
0 commit comments