From c596dd0716e2ab9b59ff9d1f1683f21a353218bc Mon Sep 17 00:00:00 2001 From: kanno <812137533@qq.com> Date: Thu, 11 Aug 2022 15:22:28 +0800 Subject: [PATCH] chore(action): extra delete & set logic (#33) * chore(action): extra del & set logic * fix(action): add action should verify all source * test: update unit test case * feat: rmmove process code from helper --- cmd/grm/helper.go | 12 +++-- internal/action/handle.go | 95 ++++++++++++++++++++++----------------- internal/utils.go | 7 +++ internal/utils_test.go | 10 +++++ 4 files changed, 76 insertions(+), 48 deletions(-) diff --git a/cmd/grm/helper.go b/cmd/grm/helper.go index 09d299f..e234276 100644 --- a/cmd/grm/helper.go +++ b/cmd/grm/helper.go @@ -19,10 +19,10 @@ Options: Commands: ls List all the registries current Show current registry name - use Change registry to registry + use Change registry to registry test Test response time for specific or all registries - add [home] Add one custom registry - del Delete one custom registry by alias + add [home] Add one custom registry + del Delete one custom registry by alias help Print this help ` @@ -85,11 +85,9 @@ func parserSourceForRun(args []string, source *registry.RegistryDataSource) int for _, arg := range args { switch arg { case "ls": - action.ShowSources(source) - return 0 + return action.ShowSources(source) case "current": - action.ShowCurrent() - return 0 + return action.ShowCurrent() case "use": return action.SetCurrent(source, args[1:]) case "add": diff --git a/internal/action/handle.go b/internal/action/handle.go index 3d0f4cc..1d9c8ca 100644 --- a/internal/action/handle.go +++ b/internal/action/handle.go @@ -1,6 +1,7 @@ package action import ( + "errors" "fmt" "math" "os" @@ -16,7 +17,7 @@ func getCurrent() string { return registry.ReadNpm() } -func ShowSources(source *registry.RegistryDataSource) { +func ShowSources(source *registry.RegistryDataSource) int { outLen := len(source.Keys) + 3 @@ -38,101 +39,113 @@ func ShowSources(source *registry.RegistryDataSource) { } } - + return 0 } // show current registry uri and alias -func ShowCurrent() { +func ShowCurrent() int { cur := getCurrent() logger.Info(internal.StringJoin("[Grm]: you are using", cur)) + return 0 } func SetCurrent(source *registry.RegistryDataSource, args []string) int { - - name := "npm" - - if len(args) >= 1 { - name = args[0] - } - 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())) + defer func() { + if err := recover(); err != nil { + logger.Warn(internal.StringJoin("[Grm]: Plese pass an alias.", registry.Eol())) + return + } + }() + name := internal.PickArgs(args, 0) + uri, err := getRegistryMeta(name, source.Registry, func(n string) (string, error) { + return "", errors.New(internal.StringJoin("[Grm]: Can't found alias", name, "in your .nrmrc file. Please check it exist.")) + }) + if err != nil { + logger.Error(internal.StringJoin(err.Error(), registry.Eol())) return 1 } - err := registry.WriteNpm(uri) + err = registry.WriteNpm(uri) if err != nil { logger.Error(internal.StringJoin("[Grm]: error with", err.Error(), registry.Eol())) return 1 } logger.Success(internal.StringJoin("[Grm]: use", name, "success~", registry.Eol())) return 0 + } // del .nrm file registry alias func DelRegistry(source *registry.RegistryDataSource, args []string) int { - - if len(args) == 0 { - return 0 - } - name := args[0] - - _, 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())) + defer func() { + if err := recover(); err != nil { + logger.Warn(internal.StringJoin("[Grm]: Plese pass an alias.", registry.Eol())) + return + } + }() + name := internal.PickArgs(args, 0) + _, err := getRegistryMeta(name, source.UserRegistry, func(n string) (string, error) { + return "", errors.New(internal.StringJoin("[Grm]: Can't found alias", name, "in your .nrmrc file. Please check it exist.")) + }) + if err != nil { + logger.Error(internal.StringJoin(err.Error(), registry.Eol())) return 1 } - err := registry.DelNrm(name) + err = registry.DelNrm(name) if err != nil { logger.Error(internal.StringJoin("[Grm]: del registry fail", err.Error(), registry.Eol())) return 1 - } logger.Success(internal.StringJoin("[Grm]: del registry", name, "success!", registry.Eol())) return 0 +} +func getRegistryMeta(name string, source map[string]string, callback func(name string) (string, error)) (string, error) { + meta, ok := source[name] + if !ok { + return callback(name) + } + return meta, nil } func AddRegistry(source *registry.RegistryDataSource, args []string) int { - name := "" - home := "" - uri := "" + defer func() { + if err := recover(); err != nil { + logger.Warn(internal.StringJoin("[Grm]: Plese pass an alias.", registry.Eol())) + return + } + }() - if len(args) <= 1 { - logger.Error(internal.StringJoin("[Grm]: name and registry url is must be entry", registry.Eol())) - return 1 - } - name = args[0] + name := internal.PickArgs(args, 0) + uri := internal.PickArgs(args, 1) + home := "" - if _, ok := source.UserRegistry[name]; ok { - logger.Error("[Grm]: can't be the same as the default source name!") + if _, ok := source.Registry[name]; ok { + logger.Error(internal.StringJoin("[Grm]: alias already exist.", registry.Eol())) return 1 } - uri = args[1] if len(args) == 2 { home = uri } if len(args) >= 3 { - home = args[2] + home = internal.PickArgs(args, 2) } if !internal.IsUri(uri) && !internal.IsUri(home) { logger.Error("[Grm]: please verify the uri address you entered.") return 1 } - - err := addRegistryImpl(name, uri, home) - - if err != nil { + if err := addRegistryImpl(name, uri, home); err != nil { logger.Error(internal.StringJoin("[Grm]: add registry fail", err.Error(), registry.Eol())) return 1 } + logger.Success(internal.StringJoin("[Grm]: add registry success!", registry.Eol())) return 0 + } type FetchState uint8 diff --git a/internal/utils.go b/internal/utils.go index 143dd93..87edcbf 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -15,3 +15,10 @@ var uriReg = regexp.MustCompile(`(^https?:\/\/(www\.))?[-a-zA-Z0-9@:%._\+~#=]{2, func IsUri(uri string) bool { return uriReg.MatchString(uri) } + +func PickArgs(args []string, ptr int) string { + if len(args) >= ptr { + return args[ptr] + } + panic("Invalid ptr") +} diff --git a/internal/utils_test.go b/internal/utils_test.go index 4d8dc7d..1d12098 100644 --- a/internal/utils_test.go +++ b/internal/utils_test.go @@ -32,3 +32,13 @@ func TestIsUri(t *testing.T) { } } + +func TestPickArgs(t *testing.T) { + args := []string{"a", "b", "c"} + cursor := 1 + v := internal.PickArgs(args, cursor) + if v != "b" { + t.Errorf("Expected: %s, Actual: %s", "b", v) + } + +}