Skip to content

Commit 59d3eba

Browse files
xavier-houdaniel-hutao
authored andcommitted
fix: check existence for 'create-plugin'
1 parent 7f2fc36 commit 59d3eba

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

cmd/devstream/list/list.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,23 @@ func List() {
2121
fmt.Println(pluginName)
2222
}
2323
}
24+
25+
// Get plugins name in slice
26+
func PluginsNameSlice() []string {
27+
listPluginsName := strings.Fields(PluginsName)
28+
sort.Strings(listPluginsName)
29+
return listPluginsName
30+
}
31+
32+
// Get plugins name in map
33+
func PluginNamesMap() map[string]struct{} {
34+
mp := make(map[string]struct{})
35+
36+
listPluginsName := strings.Fields(PluginsName)
37+
38+
for _, pluginName := range listPluginsName {
39+
mp[pluginName] = struct{}{}
40+
}
41+
42+
return mp
43+
}

internal/pkg/develop/plugin/create.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/spf13/viper"
77

8+
"github.com/devstream-io/devstream/cmd/devstream/list"
89
"github.com/devstream-io/devstream/pkg/util/log"
910
)
1011

@@ -19,6 +20,11 @@ func Create() error {
1920
}
2021
log.Debugf("Got the name: %s.", name)
2122

23+
if pluginExists(name) {
24+
return fmt.Errorf("Plugin name: %s is already exists", name)
25+
}
26+
log.Debugf("Got the name: %s.", name)
27+
2228
p := NewPlugin(name)
2329

2430
// 1. Render template files
@@ -40,3 +46,14 @@ func Create() error {
4046

4147
return nil
4248
}
49+
50+
// Check whether the new name exists.
51+
func pluginExists(name string) bool {
52+
pluginMp := list.PluginNamesMap()
53+
54+
if _, ok := (pluginMp)[name]; ok {
55+
return true
56+
}
57+
58+
return false
59+
}

internal/pkg/develop/plugin/validate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package plugin
22

33
import (
44
"fmt"
5-
"sort"
6-
"strings"
75

86
"github.com/spf13/viper"
97

@@ -31,8 +29,7 @@ func Validate() error {
3129
// Validate all plugins
3230
// calling ValidatePlugin() via all plugins name
3331
func ValidatePlugins() error {
34-
listPluginsName := strings.Fields(list.PluginsName)
35-
sort.Strings(listPluginsName)
32+
listPluginsName := list.PluginsNameSlice()
3633

3734
for _, pluginName := range listPluginsName {
3835
log.Infof("===== start validating <%s> =====", pluginName)

0 commit comments

Comments
 (0)