/
delete.go
45 lines (36 loc) · 1.19 KB
/
delete.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
package cmd
import (
"fmt"
"os"
"path/filepath"
cli "github.com/spf13/cobra"
"github.com/4thel00z/templatectl/pkg/templatectl"
"github.com/4thel00z/templatectl/pkg/util/osutil"
"github.com/4thel00z/templatectl/pkg/util/tlog"
"github.com/4thel00z/templatectl/pkg/util/validate"
)
// Delete contains the cli-command for deleting templates.
var Delete = &cli.Command{
Use: "delete <template-tag>",
Short: "Delete a project template from the template registry",
Run: func(c *cli.Command, args []string) {
MustValidateVarArgs(args, validate.Argument{"template-path", validate.AlphanumericExt})
MustValidateTemplateDir()
for _, templateName := range args {
targetDir := filepath.Join(templatectl.Configuration.TemplateDirPath, templateName)
switch exists, err := osutil.DirExists(targetDir); {
case err != nil:
tlog.Error(fmt.Sprintf("delete: %s", err))
continue
case !exists:
tlog.Error(fmt.Sprintf("Template %v doesn't exist", templateName))
continue
}
if err := os.RemoveAll(targetDir); err != nil {
tlog.Error(fmt.Sprintf("delete: %v", err))
continue
}
tlog.Success(fmt.Sprintf("Successfully deleted the template %v", templateName))
}
},
}