Skip to content

Commit 944c288

Browse files
authored
Merge pull request #950 from steinliber/refactor-unify-template-processor
refactor: unify template file processor
2 parents e1d5124 + 67047d4 commit 944c288

File tree

26 files changed

+929
-247
lines changed

26 files changed

+929
-247
lines changed
Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
package argocdapp
22

3-
var argoCDAppTemplate = `---
4-
apiVersion: argoproj.io/v1alpha1
5-
kind: Application
6-
metadata:
7-
name: {{.app.name}}
8-
namespace: {{.app.namespace}}
9-
finalizers:
10-
- resources-finalizer.argocd.argoproj.io
11-
spec:
12-
destination:
13-
namespace: {{.destination.namespace}}
14-
server: {{.destination.server}}
15-
project: default
16-
source:
17-
helm:
18-
valueFiles:
19-
- {{.source.valuefile}}
20-
path: {{.source.path}}
21-
repoURL: {{.source.repoURL}}
22-
targetRevision: HEAD
23-
syncPolicy:
24-
automated:
25-
prune: true
26-
selfHeal: true
27-
`
3+
import (
4+
_ "embed"
5+
)
6+
7+
//go:embed tpl/argocd.tpl.yaml
8+
var templateFileLoc string

internal/pkg/plugin/argocdapp/create.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argocdapp
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
55
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
6+
"github.com/devstream-io/devstream/pkg/util/file"
67
"github.com/devstream-io/devstream/pkg/util/log"
78
)
89

@@ -14,7 +15,9 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
1415
validate,
1516
},
1617
ExecuteOperations: []plugininstaller.BaseOperation{
17-
kubectl.ProcessByContent("create", "", argoCDAppTemplate),
18+
kubectl.ProcessByContent(
19+
"create", file.NewTemplate().FromContent(templateFileLoc),
20+
),
1821
},
1922
GetStatusOperation: getStaticState,
2023
}

internal/pkg/plugin/argocdapp/delete.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argocdapp
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
55
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
6+
"github.com/devstream-io/devstream/pkg/util/file"
67
)
78

89
func Delete(options map[string]interface{}) (bool, error) {
@@ -12,7 +13,9 @@ func Delete(options map[string]interface{}) (bool, error) {
1213
validate,
1314
},
1415
ExecuteOperations: []plugininstaller.BaseOperation{
15-
kubectl.ProcessByContent("delete", "", argoCDAppTemplate),
16+
kubectl.ProcessByContent(
17+
"delete", file.NewTemplate().FromContent(templateFileLoc),
18+
),
1619
},
1720
}
1821

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: argoproj.io/v1alpha1
2+
kind: Application
3+
metadata:
4+
name: "[[ .app.name ]]"
5+
namespace: "[[ .app.namespace ]]"
6+
finalizers:
7+
- resources-finalizer.argocd.argoproj.io
8+
spec:
9+
destination:
10+
namespace: "[[ .destination.namespace ]]"
11+
server: "[[ .destination.server ]]"
12+
project: default
13+
source:
14+
helm:
15+
valueFiles:
16+
- "[[ .source.valuefile ]]"
17+
path: "[[ .source.path ]]"
18+
repoURL: "[[ .source.repoURL ]]"
19+
targetRevision: HEAD
20+
syncPolicy:
21+
automated:
22+
prune: true
23+
selfHeal: true
24+

internal/pkg/plugin/devlake/create.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package devlake
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
55
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
6+
"github.com/devstream-io/devstream/pkg/util/file"
67
"github.com/devstream-io/devstream/pkg/util/log"
78
)
89

910
func Create(options map[string]interface{}) (map[string]interface{}, error) {
1011
// 1. config install operations
1112
runner := &plugininstaller.Runner{
1213
ExecuteOperations: []plugininstaller.BaseOperation{
13-
kubectl.ProcessByContent("create", devLakeInstallYAMLDownloadURL, ""),
14+
kubectl.ProcessByContent(
15+
"create", file.NewTemplate().FromRemote(devLakeInstallYAMLDownloadURL),
16+
),
1417
},
1518
GetStatusOperation: getStaticState,
1619
}

internal/pkg/plugin/devlake/delete.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package devlake
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
55
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
6+
"github.com/devstream-io/devstream/pkg/util/file"
67
)
78

89
func Delete(options map[string]interface{}) (bool, error) {
910
// 1. config delete operations
1011
runner := &plugininstaller.Runner{
1112
ExecuteOperations: []plugininstaller.BaseOperation{
12-
kubectl.ProcessByContent("delete", devLakeInstallYAMLDownloadURL, ""),
13+
kubectl.ProcessByContent(
14+
"delete", file.NewTemplate().FromRemote(devLakeInstallYAMLDownloadURL),
15+
),
1316
},
1417
}
1518

internal/pkg/plugininstaller/kubectl/installer.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,20 @@ package kubectl
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
7+
"github.com/devstream-io/devstream/pkg/util/file"
88
"github.com/devstream-io/devstream/pkg/util/kubectl"
9-
"github.com/devstream-io/devstream/pkg/util/log"
109
)
1110

1211
// InstallByDownload will download file for apply
13-
func ProcessByContent(action, downloadUrl, content string) plugininstaller.BaseOperation {
12+
func ProcessByContent(action string, templateConfig *file.TemplateConfig) plugininstaller.BaseOperation {
1413
return func(options plugininstaller.RawOptions) error {
1514
// generate k8s config file for apply
16-
configFileName, err := createKubectlFile(downloadUrl, content, options)
15+
configFileName, err := templateConfig.RenderFile("kubectl", options).Run()
1716
if err != nil {
1817
return err
1918
}
20-
21-
defer func() {
22-
err := os.Remove(configFileName)
23-
if err != nil {
24-
log.Debugf("kubectl delete temp file failed: %s", err)
25-
}
26-
}()
2719
// kubectl apply -f
2820
switch action {
2921
case "create":

internal/pkg/plugininstaller/kubectl/utils.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

internal/pkg/plugininstaller/reposcaffolding/installer.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package reposcaffolding
22

33
import (
44
"fmt"
5-
"os"
6-
"path/filepath"
75

86
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
9-
"github.com/devstream-io/devstream/pkg/util/log"
107
)
118

129
// InstallRepo will install repo by opts config
@@ -16,33 +13,20 @@ func InstallRepo(options plugininstaller.RawOptions) error {
1613
return err
1714
}
1815

19-
// 1. Create temp dir
20-
dirName, err := os.MkdirTemp("", "")
16+
// 1. Create and render repo get from given url
17+
dirPath, err := opts.CreateAndRenderLocalRepo()
2118
if err != nil {
2219
return err
2320
}
24-
defer func() {
25-
if err := os.RemoveAll(dirName); err != nil {
26-
log.Errorf("Failed to clear workpath %s: %s.", dirName, err)
27-
}
28-
}()
2921

30-
// 2. Create and render repo get from given url
31-
err = opts.CreateAndRenderLocalRepo(dirName)
32-
if err != nil {
33-
return err
34-
}
35-
36-
dstRepo := opts.DestinationRepo
3722
// 2. Push local repo to remote
38-
repoLoc := filepath.Join(dirName, opts.DestinationRepo.Repo)
39-
switch dstRepo.RepoType {
23+
switch opts.DestinationRepo.RepoType {
4024
case "github":
41-
err = opts.PushToRemoteGithub(repoLoc)
25+
err = opts.PushToRemoteGithub(dirPath)
4226
case "gitlab":
43-
err = opts.PushToRemoteGitlab(repoLoc)
27+
err = opts.PushToRemoteGitlab(dirPath)
4428
default:
45-
err = fmt.Errorf("scaffolding not support repo destination: %s", dstRepo.RepoType)
29+
err = fmt.Errorf("scaffolding not support repo destination: %s", opts.DestinationRepo.RepoType)
4630
}
4731
if err != nil {
4832
return err
@@ -62,7 +46,7 @@ func DeleteRepo(options plugininstaller.RawOptions) error {
6246
switch dstRepo.RepoType {
6347
case "github":
6448
// 1. create ghClient
65-
ghClient, err := opts.DestinationRepo.CreateGithubClient(true)
49+
ghClient, err := dstRepo.CreateGithubClient(true)
6650
if err != nil {
6751
return err
6852
}

internal/pkg/plugininstaller/reposcaffolding/option.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package reposcaffolding
22

33
import (
4+
"path/filepath"
5+
46
"github.com/mitchellh/mapstructure"
57

68
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
79
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/common"
10+
"github.com/devstream-io/devstream/pkg/util/file"
811
"github.com/devstream-io/devstream/pkg/util/log"
912
)
1013

1114
const (
12-
transitBranch = "init-with-devstream"
13-
defaultCommitMsg = "init with devstream"
14-
appNamePlaceHolder = "_app_name_"
15+
transitBranch = "init-with-devstream"
16+
defaultCommitMsg = "init with devstream"
1517
)
1618

1719
type Options struct {
@@ -36,18 +38,25 @@ func (opts *Options) Encode() (map[string]interface{}, error) {
3638
return options, nil
3739
}
3840

39-
func (opts *Options) CreateAndRenderLocalRepo(workpath string) error {
40-
// 1. download template scaffolding repo
41-
err := opts.SourceRepo.DownloadRepo(workpath)
41+
// CreateAndRenderLocalRepo will download repo from source repo and render it locally
42+
func (opts *Options) CreateAndRenderLocalRepo() (string, error) {
43+
// 1. get download url
44+
githubCodeZipDownloadURL, err := opts.SourceRepo.getDownloadURL()
4245
if err != nil {
43-
return err
46+
log.Debugf("reposcaffolding get download url failed: %s", err)
47+
return "", err
4448
}
45-
// 2. walk iter repo files to render template
46-
if err := walkLocalRepoPath(workpath, opts); err != nil {
47-
log.Debugf("create local repo failed walk: %s.", err)
48-
return err
49+
// 2. download zip file and unzip this file then render folders
50+
projectDir, err := file.NewTemplate().FromRemote(githubCodeZipDownloadURL).UnzipFile().RenderRepoDIr(
51+
opts.DestinationRepo.Repo, opts.renderTplConfig(),
52+
).Run()
53+
if err != nil {
54+
log.Debugf("reposcaffolding process file error: %s", err)
55+
return "", err
4956
}
50-
return nil
57+
// 3. join download path and repo name to get repo path
58+
repoDirName := opts.SourceRepo.getRepoName()
59+
return filepath.Join(projectDir, repoDirName), nil
5160
}
5261

5362
// PushToRemoteGitLab push local repo to remote gitlab repo

0 commit comments

Comments
 (0)