Skip to content

Commit 59cf87e

Browse files
authored
Merge pull request #47 from lfbdev/lfb-dev2
add reinstall and unistall actions
2 parents 4a34f1a + a3ca230 commit 59cf87e

File tree

10 files changed

+194
-14
lines changed

10 files changed

+194
-14
lines changed

cmd/argocdapp/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ func (p Plugin) Install(options *map[string]interface{}) (bool, error) {
1919

2020
// Reinstall implements the installation of an ArgoCD app.
2121
func (p Plugin) Reinstall(options *map[string]interface{}) (bool, error) {
22-
return false, fmt.Errorf("mock: %s reinstall finished", NAME)
22+
return argocdapp.Reinstall(options)
2323
}
2424

2525
// Uninstall Uninstall the installation of an ArgoCD app.
2626
func (p Plugin) Uninstall(options *map[string]interface{}) (bool, error) {
27-
return false, fmt.Errorf("mock: %s uninstall finished", NAME)
27+
return argocdapp.Uninstall(options)
2828
}
2929

3030
// DevStreamPlugin is the exported variable used by the DevStream core.

cmd/githubactions/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ func (p Plugin) Install(options *map[string]interface{}) (bool, error) {
1919

2020
// Reinstall implements the installation of some GitHub Actions workflows.
2121
func (p Plugin) Reinstall(options *map[string]interface{}) (bool, error) {
22-
return false, fmt.Errorf("mock: %s reinstall finished", NAME)
22+
return githubactions.Reinstall(options)
2323
}
2424

2525
// Uninstall implements the installation of some GitHub Actions workflows.
2626
func (p Plugin) Uninstall(options *map[string]interface{}) (bool, error) {
27-
return false, fmt.Errorf("mock: %s uninstall finished", NAME)
27+
return githubactions.Uninstall(options)
2828
}
2929

3030
// DevStreamPlugin is the exported variable used by the DevStream core.

internal/pkg/argocdapp/argocdapp.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,24 @@ func writeContentToTmpFile(file string, content string, param *Param) {
3333
}
3434
}
3535

36-
func kubectlApply(file string) {
36+
func kubectlApply(file string) (bool, error) {
3737
cmd := exec.Command("kubectl", "apply", "-f", file)
3838
stdout, err := cmd.Output()
3939
if err != nil {
4040
fmt.Println(err.Error())
41-
return
41+
return false, err
4242
}
43-
log.Println(string(strings.TrimSuffix(string(stdout), "\n")))
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
4456
}

internal/pkg/argocdapp/install.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ func Install(options *map[string]interface{}) (bool, error) {
1616

1717
file := "./app.yaml"
1818
writeContentToTmpFile(file, appTemplate, &param)
19-
kubectlApply(file)
19+
_, errApply := kubectlApply(file)
20+
if errApply != nil {
21+
return false, errApply
22+
}
2023

2124
return true, nil
2225
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package argocdapp
2+
3+
import (
4+
"os"
5+
6+
"github.com/mitchellh/mapstructure"
7+
)
8+
9+
// Reinstall an ArgoCD app .
10+
func Reinstall(options *map[string]interface{}) (bool, error) {
11+
var param Param
12+
err := mapstructure.Decode(*options, &param)
13+
if err != nil {
14+
return false, err
15+
}
16+
17+
file := "./app.yaml"
18+
19+
//delete resource
20+
_, errDel := kubectlDelete(file)
21+
if errDel != nil {
22+
return false, errDel
23+
}
24+
25+
//remove app.yaml file
26+
errRemove := os.Remove(file)
27+
if errRemove != nil {
28+
return false, err
29+
}
30+
31+
//recreate app.yaml file
32+
writeContentToTmpFile(file, appTemplate, &param)
33+
_, errApply := kubectlApply(file)
34+
if errApply != nil {
35+
return false, errApply
36+
}
37+
38+
return true, nil
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package argocdapp
2+
3+
import (
4+
"os"
5+
6+
"github.com/mitchellh/mapstructure"
7+
)
8+
9+
// Uninstall uninstall an ArgoCD app by yaml.
10+
func Uninstall(options *map[string]interface{}) (bool, error) {
11+
var param Param
12+
err := mapstructure.Decode(*options, &param)
13+
if err != nil {
14+
return false, err
15+
}
16+
17+
file := "./app.yaml"
18+
_, errDel := kubectlDelete(file)
19+
if errDel != nil {
20+
return false, errDel
21+
}
22+
errRemove := os.Remove(file)
23+
if errRemove != nil {
24+
return false, errRemove
25+
}
26+
return true, nil
27+
}

internal/pkg/githubactions/githubactions.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func fileExists(params *Param) bool {
6060

6161
}
6262

63-
func createFile(params *Param) {
63+
func createFile(params *Param) (bool, error) {
6464
if fileExists(params) {
6565
log.Printf("github actions workflow %s already exists\n", params.workflow.workflowFileName)
66-
return
66+
return true, nil
6767
}
6868

6969
// Note: the file needs to be absent from the repository as you are not
@@ -83,8 +83,39 @@ func createFile(params *Param) {
8383
opts)
8484

8585
if err != nil {
86-
fmt.Println(err.Error())
87-
return
86+
log.Println(err.Error())
87+
return false, err
8888
}
8989
log.Printf("github actions workflow %s created\n", params.workflow.workflowFileName)
90+
return true, nil
91+
}
92+
93+
func removeFile(params *Param) (bool, error) {
94+
if !fileExists(params) {
95+
log.Printf("github actions workflow %s already removed\n", params.workflow.workflowFileName)
96+
return true, nil
97+
}
98+
99+
// Note: the file needs to be absent from the repository as you are not
100+
// specifying a SHA reference here.
101+
opts := &github.RepositoryContentFileOptions{
102+
Message: github.String(params.workflow.commitMessage),
103+
Content: []byte(params.workflow.workflowContent),
104+
Branch: github.String("master"),
105+
}
106+
107+
log.Printf("deleting github actions workflow %s...\n", params.workflow.workflowFileName)
108+
_, _, err := params.client.Repositories.DeleteFile(
109+
*params.ctx,
110+
params.options.Owner,
111+
params.options.Repo,
112+
generateGitHubWorkflowFileByName(params.workflow.workflowFileName),
113+
opts)
114+
115+
if err != nil {
116+
log.Println(err.Error())
117+
return false, err
118+
}
119+
log.Printf("github actions workflow %s removed\n", params.workflow.workflowFileName)
120+
return true, nil
90121
}

internal/pkg/githubactions/install.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ func Install(options *map[string]interface{}) (bool, error) {
2323
}
2424

2525
for _, pipeline := range workflows {
26-
createFile(&Param{
26+
_, errCreate := createFile(&Param{
2727
&ctx,
2828
getGitHubClient(&ctx),
2929
&opt,
3030
&pipeline,
3131
})
32+
if errCreate != nil {
33+
return false, errCreate
34+
}
3235
}
33-
3436
return true, nil
3537
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package githubactions
2+
3+
import (
4+
"context"
5+
"github.com/mitchellh/mapstructure"
6+
)
7+
8+
// Reinstall remove and set up GitHub Actions workflows.
9+
func Reinstall(options *map[string]interface{}) (bool, error) {
10+
ctx := context.Background()
11+
12+
var opt Options
13+
err := mapstructure.Decode(*options, &opt)
14+
if err != nil {
15+
return false, err
16+
}
17+
18+
for _, pipeline := range workflows {
19+
param := &Param{
20+
&ctx,
21+
getGitHubClient(&ctx),
22+
&opt,
23+
&pipeline,
24+
}
25+
_, errRemove := removeFile(param)
26+
if errRemove != nil {
27+
return false, errRemove
28+
}
29+
30+
_, errCreate := createFile(param)
31+
if errCreate != nil {
32+
return false, errCreate
33+
}
34+
}
35+
return true, nil
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package githubactions
2+
3+
import (
4+
"context"
5+
"github.com/mitchellh/mapstructure"
6+
)
7+
8+
// Uninstall remove GitHub Actions workflows.
9+
func Uninstall(options *map[string]interface{}) (bool, error) {
10+
ctx := context.Background()
11+
12+
var opt Options
13+
err := mapstructure.Decode(*options, &opt)
14+
if err != nil {
15+
return false, err
16+
}
17+
18+
for _, pipeline := range workflows {
19+
_, errRemove := removeFile(&Param{
20+
&ctx,
21+
getGitHubClient(&ctx),
22+
&opt,
23+
&pipeline,
24+
})
25+
if errRemove != nil {
26+
return false, errRemove
27+
}
28+
}
29+
return true, nil
30+
}

0 commit comments

Comments
 (0)