Skip to content

Commit 7a29ba2

Browse files
committed
refactor: reposcaffolding plugin use encode logic
Signed-off-by: Meng JiaFeng <[email protected]>
1 parent 1a16d9c commit 7a29ba2

File tree

18 files changed

+246
-155
lines changed

18 files changed

+246
-155
lines changed

internal/pkg/plugin/installer/reposcaffolding/installer.go

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

internal/pkg/plugin/installer/reposcaffolding/validate.go

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/mitchellh/mapstructure"
8+
9+
"github.com/devstream-io/devstream/internal/pkg/configmanager"
10+
"github.com/devstream-io/devstream/pkg/util/mapz"
11+
"github.com/devstream-io/devstream/pkg/util/scm/git"
12+
)
13+
14+
func DecodePlugin(rawOptions configmanager.RawOptions, pluginData any) error {
15+
// 1. create a new decode with pluginDecoder config
16+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
17+
DecodeHook: pluginDecoder,
18+
Result: pluginData,
19+
})
20+
if err != nil {
21+
return fmt.Errorf("create plugin decoder failed: %w", err)
22+
}
23+
// 2. decode rawOptions to structData
24+
if err := decoder.Decode(rawOptions); err != nil {
25+
return fmt.Errorf("decode plugin option failed: %w", err)
26+
}
27+
return nil
28+
}
29+
30+
func pluginDecoder(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
31+
switch t {
32+
// set git.RepoInfo default value
33+
case reflect.TypeOf(&git.RepoInfo{}):
34+
repoData := new(git.RepoInfo)
35+
if err := mapstructure.Decode(data, repoData); err != nil {
36+
return nil, err
37+
}
38+
if err := repoData.SetDefault(); err != nil {
39+
return nil, err
40+
}
41+
return mapz.DecodeStructToMap(repoData)
42+
}
43+
return data, nil
44+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package util_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
7+
"github.com/devstream-io/devstream/internal/pkg/configmanager"
8+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/util"
9+
"github.com/devstream-io/devstream/pkg/util/scm/git"
10+
)
11+
12+
var _ = Describe("DecodePlugin func", func() {
13+
var (
14+
plugData *mockStruct
15+
rawData configmanager.RawOptions
16+
)
17+
When("decoder is not valid", func() {
18+
BeforeEach(func() {
19+
plugData = nil
20+
})
21+
It("should return error", func() {
22+
err := util.DecodePlugin(rawData, plugData)
23+
Expect(err).Should(HaveOccurred())
24+
Expect(err.Error()).Should(ContainSubstring("create plugin decoder failed"))
25+
})
26+
})
27+
When("options is not valid", func() {
28+
BeforeEach(func() {
29+
plugData = new(mockStruct)
30+
rawData = map[string]any{
31+
"scm": map[string]any{"key": "not_exist"},
32+
}
33+
})
34+
It("should return error", func() {
35+
err := util.DecodePlugin(rawData, plugData)
36+
Expect(err).Should(HaveOccurred())
37+
Expect(err.Error()).Should(ContainSubstring("decode plugin option failed"))
38+
})
39+
})
40+
41+
When("all params are valid", func() {
42+
BeforeEach(func() {
43+
plugData = new(mockStruct)
44+
rawData = map[string]any{
45+
"scm": map[string]any{
46+
"url": "github.com/test/test_repo",
47+
},
48+
}
49+
})
50+
51+
It("should set default value", func() {
52+
err := util.DecodePlugin(rawData, plugData)
53+
Expect(err).ShouldNot(HaveOccurred())
54+
Expect(plugData).Should(Equal(&mockStruct{
55+
Scm: &git.RepoInfo{
56+
Owner: "test",
57+
Repo: "test_repo",
58+
Branch: "main",
59+
RepoType: "github",
60+
CloneURL: "github.com/test/test_repo",
61+
NeedAuth: false,
62+
},
63+
}))
64+
})
65+
})
66+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package util_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
9+
"github.com/devstream-io/devstream/pkg/util/scm/git"
10+
)
11+
12+
func TestCommon(t *testing.T) {
13+
RegisterFailHandler(Fail)
14+
RunSpecs(t, "Plugin Installer Util Suite")
15+
}
16+
17+
type mockStruct struct {
18+
Scm *git.RepoInfo `mapstructure:"scm"`
19+
DeepStruct deepStruct `mapstructure:"deepStruct"`
20+
}
21+
22+
type deepStruct struct {
23+
DeepStr string `mapstructure:"deepStr" validate:"required"`
24+
}

internal/pkg/plugin/reposcaffolding/create.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package reposcaffolding
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/reposcaffolding"
76
"github.com/devstream-io/devstream/internal/pkg/statemanager"
87
"github.com/devstream-io/devstream/pkg/util/log"
98
)
@@ -12,12 +11,12 @@ func Create(options configmanager.RawOptions) (statemanager.ResourceStatus, erro
1211
// Initialize Operator with Operations
1312
operator := &installer.Operator{
1413
PreExecuteOperations: installer.PreExecuteOperations{
15-
reposcaffolding.Validate,
14+
validate,
1615
},
1716
ExecuteOperations: installer.ExecuteOperations{
18-
reposcaffolding.InstallRepo,
17+
installRepo,
1918
},
20-
GetStatusOperation: reposcaffolding.GetDynamicStatus,
19+
GetStatusOperation: getDynamicStatus,
2120
}
2221

2322
// Execute all Operations in Operator

internal/pkg/plugin/reposcaffolding/delete.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package reposcaffolding
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/reposcaffolding"
76
)
87

98
func Delete(options configmanager.RawOptions) (bool, error) {
109
// Initialize Operator with Operations
1110
operator := &installer.Operator{
1211
PreExecuteOperations: installer.PreExecuteOperations{
13-
reposcaffolding.Validate,
12+
validate,
1413
},
1514
ExecuteOperations: installer.ExecuteOperations{
16-
reposcaffolding.DeleteRepo,
15+
deleteRepo,
1716
},
1817
}
1918
_, err := operator.Execute(options)

internal/pkg/plugin/installer/reposcaffolding/option.go renamed to internal/pkg/plugin/reposcaffolding/option.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
package reposcaffolding
22

33
import (
4-
"github.com/mitchellh/mapstructure"
5-
6-
"github.com/devstream-io/devstream/internal/pkg/configmanager"
74
"github.com/devstream-io/devstream/pkg/util/scm/git"
85
)
96

10-
type Options struct {
7+
type options struct {
118
SourceRepo *git.RepoInfo `validate:"required" mapstructure:"sourceRepo"`
129
DestinationRepo *git.RepoInfo `validate:"required" mapstructure:"destinationRepo"`
1310
Vars map[string]interface{}
1411
}
1512

16-
func NewOptions(options configmanager.RawOptions) (*Options, error) {
17-
var opts Options
18-
if err := mapstructure.Decode(options, &opts); err != nil {
19-
return nil, err
20-
}
21-
return &opts, nil
22-
}
23-
24-
func (opts *Options) renderTplConfig() map[string]interface{} {
13+
func (opts *options) renderTplConfig() map[string]interface{} {
2514
// default render value from repo
2615
renderConfig := map[string]any{
2716
"AppName": opts.DestinationRepo.GetRepoName(),

internal/pkg/plugin/installer/reposcaffolding/option_test.go renamed to internal/pkg/plugin/reposcaffolding/option_test.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import (
44
. "github.com/onsi/ginkgo/v2"
55
. "github.com/onsi/gomega"
66

7-
"github.com/devstream-io/devstream/internal/pkg/configmanager"
87
"github.com/devstream-io/devstream/pkg/util/scm/git"
98
)
109

1110
var _ = Describe("Options struct", func() {
1211
var (
13-
opts *Options
12+
opts *options
1413
)
1514
BeforeEach(func() {
16-
opts = &Options{
15+
opts = &options{
1716
SourceRepo: &git.RepoInfo{
1817
Repo: "source_repo",
1918
Owner: "source_owner",
@@ -26,33 +25,6 @@ var _ = Describe("Options struct", func() {
2625
},
2726
}
2827
})
29-
Context("NewOptions method", func() {
30-
var (
31-
rawOptions configmanager.RawOptions
32-
)
33-
BeforeEach(func() {
34-
rawOptions = configmanager.RawOptions{
35-
"sourceRepo": map[string]string{
36-
"owner": "test_user",
37-
"name": "test_repo",
38-
"scmType": "github",
39-
},
40-
"destinationRepo": map[string]string{
41-
"owner": "dst_user",
42-
"name": "dst_repo",
43-
"scmType": "github",
44-
},
45-
}
46-
})
47-
It("should work normal", func() {
48-
opts, err := NewOptions(rawOptions)
49-
Expect(err).Error().ShouldNot(HaveOccurred())
50-
Expect(opts.SourceRepo.Owner).Should(Equal("test_user"))
51-
Expect(opts.SourceRepo.Repo).Should(Equal("test_repo"))
52-
Expect(opts.DestinationRepo.Owner).Should(Equal("dst_user"))
53-
Expect(opts.DestinationRepo.Repo).Should(Equal("dst_repo"))
54-
})
55-
})
5628

5729
Context("renderTplConfig method", func() {
5830
var (

internal/pkg/plugin/reposcaffolding/read.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package reposcaffolding
33
import (
44
"github.com/devstream-io/devstream/internal/pkg/configmanager"
55
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
6-
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/reposcaffolding"
76
"github.com/devstream-io/devstream/internal/pkg/statemanager"
87
"github.com/devstream-io/devstream/pkg/util/log"
98
)
109

1110
func Read(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
1211
operator := &installer.Operator{
1312
PreExecuteOperations: installer.PreExecuteOperations{
14-
reposcaffolding.Validate,
13+
validate,
1514
},
16-
GetStatusOperation: reposcaffolding.GetDynamicStatus,
15+
GetStatusOperation: getDynamicStatus,
1716
}
1817

1918
status, err := operator.Execute(options)

0 commit comments

Comments
 (0)