Skip to content

Commit

Permalink
chore(action): extra delete & set logic (#33)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nonzzz authored Aug 11, 2022
1 parent 8233495 commit c596dd0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
12 changes: 5 additions & 7 deletions cmd/grm/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Options:
Commands:
ls List all the registries
current Show current registry name
use <registry> Change registry to registry
use <name> Change registry to registry
test <name> Test response time for specific or all registries
add <name> <registry> [home] Add one custom registry
del <name> Delete one custom registry by alias
add <name> <registry> [home] Add one custom registry
del <name> Delete one custom registry by alias
help Print this help
`

Expand Down Expand Up @@ -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":
Expand Down
95 changes: 54 additions & 41 deletions internal/action/handle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action

import (
"errors"
"fmt"
"math"
"os"
Expand All @@ -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

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
10 changes: 10 additions & 0 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

0 comments on commit c596dd0

Please sign in to comment.