-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
delete.go
83 lines (70 loc) · 2.51 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package actions
import (
"fmt"
"github.com/analogj/drawbridge/pkg/config"
"github.com/analogj/drawbridge/pkg/utils"
"github.com/fatih/color"
"path"
"strings"
)
type DeleteAction struct {
Config config.Interface
}
func (e *DeleteAction) All(answerDataList []map[string]interface{}, force bool) error {
for _, v := range answerDataList {
err := e.One(v, force)
if err != nil {
color.Red("ERROR IGNORED: %v", err)
}
}
return nil
}
func (e *DeleteAction) One(answerData map[string]interface{}, force bool) error {
//delete the config file by answerData
renderedConfigFilePath := answerData["config"].(map[string]interface{})["filepath"].(string)
//custom files specified in answerData
renderedCustomFilePaths := []interface{}{}
if customItems, ok := answerData["custom"]; ok && customItems != nil && len(customItems.([]interface{})) > 0 {
renderedCustomFilePaths = customItems.([]interface{})
}
if !force {
questionStr := []string{"Are you sure you would like to delete this config and associated templates? (PEM files will not be deleted)\n"}
for k, v := range answerData {
if utils.SliceIncludes(e.Config.InternalQuestionKeys(), k) {
continue
}
questionStr = append(questionStr, fmt.Sprintf("%v: %v", k, v))
}
questionStr = append(questionStr, "\nPlease confirm [yes/no]:")
val := utils.StdinQueryBoolean(strings.Join(questionStr, "\n"))
if !val {
color.Red("Cancelled delete operation.")
return nil
}
}
fmt.Printf("Deleting config file: %v\n", renderedConfigFilePath)
if utils.FileExists(renderedConfigFilePath) {
utils.FileDelete(renderedConfigFilePath)
} else {
color.Yellow(" - Skipping. Could not find config file at: %v", renderedConfigFilePath)
}
//delete any custom templates.
for _, customTemplateData := range renderedCustomFilePaths {
renderedCustomFilePath := customTemplateData.(map[string]interface{})["filepath"].(string)
fmt.Printf("Deleting custom file: %v\n", renderedCustomFilePath)
if utils.FileExists(renderedCustomFilePath) {
utils.FileDelete(renderedCustomFilePath)
} else {
color.Yellow(" - Skipping. Could not find config file at: %v", renderedCustomFilePath)
}
}
//delete the .answers.yaml
fmt.Println("Deleting answers file")
answersFilePath := path.Join(answerData["config_dir"].(string), fmt.Sprintf(".%v.answers.yaml", path.Base(renderedConfigFilePath)))
if utils.FileExists(answersFilePath) {
utils.FileDelete(answersFilePath)
} else {
color.Yellow(" - Skipping. Could not find answers file at: %v", answersFilePath)
}
return nil
}