Skip to content

Commit 9acee5d

Browse files
authored
Merge pull request #75 from daniel-hutao/dev-2
refactor: do some refactor with argocdapp plugin
2 parents cb39ac1 + bdc4ac5 commit 9acee5d

File tree

4 files changed

+57
-53
lines changed

4 files changed

+57
-53
lines changed

internal/pkg/argocdapp/argocdapp.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,34 @@ import (
99
"text/template"
1010
)
1111

12-
func writeContentToTmpFile(file string, content string, param *Param) {
12+
const defaultYamlPath = "./app.yaml"
13+
14+
const (
15+
ActionApply Action = "apply"
16+
ActionDelete Action = "delete"
17+
)
18+
19+
type Action string
20+
21+
func kubectlAction(action Action, filename string) error {
22+
cmd := exec.Command("kubectl", string(action), "-f", filename)
23+
stdout, err := cmd.Output()
24+
if err != nil {
25+
return err
26+
}
27+
log.Println(strings.TrimSuffix(string(stdout), "\n"))
28+
return nil
29+
}
30+
31+
func writeContentToTmpFile(file string, content string, param *Param) error {
1332
t, err := template.New("app").Option("missingkey=error").Parse(content)
1433
if err != nil {
15-
log.Fatalf("Parse template: %s", err.Error())
34+
return err
1635
}
1736

1837
output, err := os.Create(file)
1938
if err != nil {
20-
log.Fatalf("Create outputFile: %s", err)
39+
return err
2140
}
2241

2342
err = t.Execute(output, param)
@@ -26,31 +45,10 @@ func writeContentToTmpFile(file string, content string, param *Param) {
2645
msg := err.Error()
2746
start := strings.Index(msg, "<")
2847
end := strings.Index(msg, ">")
29-
log.Fatalf("plugin argocdapp needs options%s but it's missing from the config file", msg[start+1:end])
48+
return fmt.Errorf("plugin argocdapp needs options%s but it's missing from the config file", msg[start+1:end])
3049
} else {
31-
log.Fatalf("Executing tpl: %s", err)
50+
return fmt.Errorf("executing tpl: %s", err)
3251
}
3352
}
34-
}
35-
36-
func kubectlApply(file string) (bool, error) {
37-
cmd := exec.Command("kubectl", "apply", "-f", file)
38-
stdout, err := cmd.Output()
39-
if err != nil {
40-
fmt.Println(err.Error())
41-
return false, err
42-
}
43-
log.Println(strings.TrimSuffix(string(stdout), "\n"))
44-
return true, nil
45-
}
46-
47-
func kubectlDelete(file string) (bool, error) {
48-
cmd := exec.Command("kubectl", "delete", "-f", file)
49-
stdout, err := cmd.Output()
50-
if err != nil {
51-
fmt.Println(err.Error())
52-
return false, err
53-
}
54-
log.Println(strings.TrimSuffix(string(stdout), "\n"))
55-
return true, nil
53+
return nil
5654
}

internal/pkg/argocdapp/install.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package argocdapp
22

33
import (
4-
"log"
5-
64
"github.com/mitchellh/mapstructure"
75
)
86

@@ -11,14 +9,18 @@ func Install(options *map[string]interface{}) (bool, error) {
119
var param Param
1210
err := mapstructure.Decode(*options, &param)
1311
if err != nil {
14-
log.Fatal(err)
12+
return false, err
1513
}
1614

17-
file := "./app.yaml"
18-
writeContentToTmpFile(file, appTemplate, &param)
19-
_, errApply := kubectlApply(file)
20-
if errApply != nil {
21-
return false, errApply
15+
file := defaultYamlPath
16+
err = writeContentToTmpFile(file, appTemplate, &param)
17+
if err != nil {
18+
return false, err
19+
}
20+
21+
err = kubectlAction(ActionDelete, file)
22+
if err != nil {
23+
return false, err
2224
}
2325

2426
return true, nil

internal/pkg/argocdapp/reinstall.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,36 @@ import (
66
"github.com/mitchellh/mapstructure"
77
)
88

9-
// Reinstall an ArgoCD app .
9+
// Reinstall an ArgoCD app
1010
func Reinstall(options *map[string]interface{}) (bool, error) {
1111
var param Param
1212
err := mapstructure.Decode(*options, &param)
1313
if err != nil {
1414
return false, err
1515
}
1616

17-
file := "./app.yaml"
17+
file := defaultYamlPath
1818

1919
//delete resource
20-
_, errDel := kubectlDelete(file)
21-
if errDel != nil {
22-
return false, errDel
20+
err = kubectlAction(ActionDelete, file)
21+
if err != nil {
22+
return false, err
2323
}
2424

2525
//remove app.yaml file
26-
errRemove := os.Remove(file)
27-
if errRemove != nil {
26+
if err = os.Remove(file); err != nil {
2827
return false, err
2928
}
3029

3130
//recreate app.yaml file
32-
writeContentToTmpFile(file, appTemplate, &param)
33-
_, errApply := kubectlApply(file)
34-
if errApply != nil {
35-
return false, errApply
31+
err = writeContentToTmpFile(file, appTemplate, &param)
32+
if err != nil {
33+
return false, err
34+
}
35+
36+
err = kubectlAction(ActionApply, file)
37+
if err != nil {
38+
return false, err
3639
}
3740

3841
return true, nil

internal/pkg/argocdapp/uninstall.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ func Uninstall(options *map[string]interface{}) (bool, error) {
1414
return false, err
1515
}
1616

17-
file := "./app.yaml"
18-
_, errDel := kubectlDelete(file)
19-
if errDel != nil {
20-
return false, errDel
17+
file := defaultYamlPath
18+
err = kubectlAction(ActionDelete, file)
19+
if err != nil {
20+
return false, err
2121
}
22-
errRemove := os.Remove(file)
23-
if errRemove != nil {
24-
return false, errRemove
22+
23+
if err = os.Remove(file); err != nil {
24+
return false, err
2525
}
26+
2627
return true, nil
2728
}

0 commit comments

Comments
 (0)