From 04415852b515ddd6a65ddbfc22658450a2ed45ac Mon Sep 17 00:00:00 2001 From: kanno <812137533@qq.com> Date: Tue, 3 May 2022 23:47:13 +0800 Subject: [PATCH] feat: add uri validate (#17) * feat: add validate for addRegistry * fix: validate source err * feat: add uri validate --- cmd/grm/helper.go | 14 ++++++---- internal/action/handle.go | 51 +++++++++++++++++++++++----------- internal/registry/source.go | 5 ++-- internal/registry/write_ini.go | 36 ++++++++++++------------ internal/utils.go | 12 +++++++- 5 files changed, 76 insertions(+), 42 deletions(-) diff --git a/cmd/grm/helper.go b/cmd/grm/helper.go index e239498..65ce7c9 100644 --- a/cmd/grm/helper.go +++ b/cmd/grm/helper.go @@ -32,8 +32,9 @@ func Run() int { func newRegistrySourceData() registry.RegistryDataSource { return registry.RegistryDataSource{ - Registry: make(map[string]string), - Keys: make([]string, 0), + Registry: make(map[string]string), + Keys: make([]string, 0), + UserRegistry: make(map[string]string), } } @@ -69,16 +70,17 @@ func parserSourceForRun(args []string, source *registry.RegistryDataSource) int source.Keys = append(source.Keys, registry.GetPresetRegistryNames()...) - nrmMaps, nrmKyes := registry.GetUserRegistryInfo() + nrmMaps, nrmKeys := registry.GetUserRegistryInfo() - source.Keys = append(source.Keys, nrmKyes...) + source.Keys = append(source.Keys, nrmKeys...) for _, key := range registry.GetPresetRegistryNames() { source.Registry[key] = registry.GetPresetRegistryInfo(key) } - for _, key := range nrmKyes { + for _, key := range nrmKeys { source.Registry[key] = nrmMaps[key].Uri + source.UserRegistry[key] = nrmMaps[key].Uri } for _, arg := range args { @@ -92,7 +94,7 @@ func parserSourceForRun(args []string, source *registry.RegistryDataSource) int case "use": return action.SetCurrent(source, args[1:]) case "add": - return action.AddRegistry(args[1:]) + return action.AddRegistry(source,args[1:]) case "del": return action.DelRegistry(source, args[1:]) case "test": diff --git a/internal/action/handle.go b/internal/action/handle.go index 663ccab..044b0ce 100644 --- a/internal/action/handle.go +++ b/internal/action/handle.go @@ -15,15 +15,15 @@ func getCurrent() string { return registry.ReadNpm() } -func ShowSources(sources *registry.RegistryDataSource) { +func ShowSources(source *registry.RegistryDataSource) { - outLen := len(sources.Keys) + 3 + outLen := len(source.Keys) + 3 cur := getCurrent() - for _, key := range sources.Keys { + for _, key := range source.Keys { prefix := "" - uri := sources.Registry[key] + uri := source.Registry[key] if cur == uri { prefix = "* " } @@ -47,35 +47,39 @@ func ShowCurrent() { logger.Info(internal.StringJoin("[Grm]: you are using", cur)) } -func SetCurrent(sources *registry.RegistryDataSource, args []string) int { +func SetCurrent(source *registry.RegistryDataSource, args []string) int { name := "npm" if len(args) >= 1 { name = args[0] } - uri, ok := sources.Registry[name] + uri, ok := source.Registry[name] if !ok { logger.Error(internal.StringJoin("[Grm]: Can't found alias", name, "in your .nrmrc file. Please check it exist.", registry.Eol())) return 1 } - registry.WriteNpm(registry.RegsitryInfo{ + flag, err := registry.WriteNpm(registry.RegsitryInfo{ Uri: uri, }) - logger.Success(internal.StringJoin("[Grm]: use", name, "success~", registry.Eol())) - return 0 + if flag { + logger.Success(internal.StringJoin("[Grm]: use", name, "success~", registry.Eol())) + return 0 + } + logger.Error(internal.StringJoin("[Grm]: error with", err.Error(), registry.Eol())) + return 1 } // del .nrm file registry alias -func DelRegistry(sources *registry.RegistryDataSource, args []string) int { +func DelRegistry(source *registry.RegistryDataSource, args []string) int { if len(args) == 0 { return 0 } name := args[0] - _, ok := sources.Registry[name] + _, ok := source.UserRegistry[name] if !ok { logger.Error(internal.StringJoin("[Grm]: Can't found alias", name, "in your .nrmrc file. Please check it exist.", registry.Eol())) @@ -91,7 +95,7 @@ func DelRegistry(sources *registry.RegistryDataSource, args []string) int { } -func AddRegistry(args []string) int { +func AddRegistry(source *registry.RegistryDataSource, args []string) int { name := "" home := "" @@ -102,6 +106,16 @@ func AddRegistry(args []string) int { return 1 } name = args[0] + + /** + * Check if the name is same as preset source name + */ + _, ok := source.UserRegistry[name] + if ok { + logger.Error("[Grm]: can't be the same as the default source name!") + return 1 + } + uri = args[1] if len(args) == 2 { home = uri @@ -110,6 +124,11 @@ func AddRegistry(args []string) int { home = args[2] } + if !internal.IsUri(uri) && !internal.IsUri(home) { + logger.Error("[Grm]: please verify the uri address you entered.") + return 1 + } + flag, err := addRegistryImpl(name, uri, home) if flag { @@ -120,23 +139,23 @@ func AddRegistry(args []string) int { return 1 } -func FetchRegistry(sources *registry.RegistryDataSource, args []string) int { +func FetchRegistry(source *registry.RegistryDataSource, args []string) int { keys := make([]string, 0) if len(args) == 0 { - keys = append(keys, sources.Keys...) + keys = append(keys, source.Keys...) } else { keys = append(keys, args[0]) } if len(keys) == 1 { - if _, ok := sources.Registry[keys[0]]; !ok { + if _, ok := source.Registry[keys[0]]; !ok { logger.Warn(internal.StringJoin("[Grm]: warning! can't found alias", keys[0], "will fetch npm source", registry.Eol())) keys[0] = "npm" } } for _, key := range keys { - fetchRegistryImpl(sources.Registry[key], key) + fetchRegistryImpl(source.Registry[key], key) } return 0 } diff --git a/internal/registry/source.go b/internal/registry/source.go index e5b524f..da33ea7 100644 --- a/internal/registry/source.go +++ b/internal/registry/source.go @@ -91,6 +91,7 @@ var ( ) type RegistryDataSource struct { - Registry map[string]string - Keys []string + Registry map[string]string + Keys []string + UserRegistry map[string]string } diff --git a/internal/registry/write_ini.go b/internal/registry/write_ini.go index cd78bce..cdddb47 100644 --- a/internal/registry/write_ini.go +++ b/internal/registry/write_ini.go @@ -7,8 +7,6 @@ import ( "strings" "github.com/go-ini/ini" - "github.com/modern-magic/grm/internal" - "github.com/modern-magic/grm/internal/logger" ) func isWin() bool { @@ -84,25 +82,29 @@ func ReadNpm() string { return cfg.Section("").Key(Registry).Value() } -func WriteNpm(info RegsitryInfo) { +func WriteNpm(info RegsitryInfo) (bool, error) { buf, err := ioutil.ReadFile(Npmrc) if err != nil { - return + return false, err } - slice := strings.Split(string(buf), eol()) + + return writeNpmImpl(buf, info.Uri) +} + +func writeNpmImpl(buf []byte, uri string) (bool, error) { + sections := strings.Split(string(buf), eol()) next := make([]string, 0) - for _, k := range slice { - if strings.Index(k, "registry=") == 0 || strings.Index(k, "home=") == 0 { - tmp := strings.Split(k, "=") - tmp[1] = info.Uri - k = strings.Join(tmp, "=") + for _, key := range sections { + if strings.Index(key, "registry=") == 0 || strings.Index(key, "home=") == 0 { + temp := strings.Split(key, "=") + temp[1] = uri + line := strings.Join(temp, "=") + next = append(next, line) + } else { + next = append(next, key) } - next = append(next, k) - } - str := strings.Join(next, eol()) - err = ioutil.WriteFile(Npmrc, []byte(str), 0644) - if err != nil { - logger.Error(internal.StringJoin("[Grm]: Error with", err.Error())) - return } + after := strings.Join(next, eol()) + err := ioutil.WriteFile(Npmrc, []byte(after), 0644) + return err == nil, err } diff --git a/internal/utils.go b/internal/utils.go index 279ac64..143dd93 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -1,7 +1,17 @@ package internal -import "strings" +import ( + "regexp" + "strings" +) func StringJoin(s ...string) string { return strings.Join(s, " ") } + +// https://regex101.com/ +var uriReg = regexp.MustCompile(`(^https?:\/\/(www\.))?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)$`) + +func IsUri(uri string) bool { + return uriReg.MatchString(uri) +}