/
save.go
69 lines (53 loc) · 1.74 KB
/
save.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cmd
import (
"fmt"
"os"
"path/filepath"
cli "github.com/spf13/cobra"
"github.com/4thel00z/templatectl/pkg/templatectl"
"github.com/4thel00z/templatectl/pkg/util/exec"
"github.com/4thel00z/templatectl/pkg/util/exit"
"github.com/4thel00z/templatectl/pkg/util/osutil"
"github.com/4thel00z/templatectl/pkg/util/validate"
)
// Save contains the cli-command for saving templates to template registry.
var Save = &cli.Command{
Use: "save <template-path> <template-tag>",
Short: "Save a local project template to template registry",
Run: func(c *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"template-path", validate.UnixPath},
{"template-tag", validate.AlphanumericExt},
})
MustValidateTemplateDir()
tmplDir, templateName := args[0], args[1]
MustValidateTemplate(tmplDir)
targetDir := filepath.Join(templatectl.Configuration.TemplateDirPath, templateName)
switch exists, err := osutil.DirExists(targetDir); {
case err != nil:
exit.Error(fmt.Errorf("save: %s", err))
case exists:
shouldOverwrite := GetBoolFlag(c, "force")
if err != nil {
exit.Error(fmt.Errorf("save: %v", err))
}
if !shouldOverwrite {
exit.OK("Template %v already exists use -f to overwrite", templateName)
}
if err := os.RemoveAll(targetDir); err != nil {
exit.Error(fmt.Errorf("save: %v", err))
}
}
if _, err := exec.Cmd("cp", "-r", tmplDir, targetDir); err != nil {
exit.Error(err)
}
absTemplateDir, err := filepath.Abs(tmplDir)
if err != nil {
exit.Error(err)
}
if err := serializeMetadata(templateName, "local:"+absTemplateDir, targetDir); err != nil {
exit.Error(fmt.Errorf("save: %s", err))
}
exit.OK("Successfully saved the template %v", templateName)
},
}