Skip to content

Commit

Permalink
feat: add uri validate (#17)
Browse files Browse the repository at this point in the history
* feat: add validate for addRegistry

* fix: validate source err

* feat: add uri validate
  • Loading branch information
nonzzz authored May 3, 2022
1 parent ebc9613 commit 0441585
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 42 deletions.
14 changes: 8 additions & 6 deletions cmd/grm/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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":
Expand Down
51 changes: 35 additions & 16 deletions internal/action/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "* "
}
Expand All @@ -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()))
Expand All @@ -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 := ""
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions internal/registry/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var (
)

type RegistryDataSource struct {
Registry map[string]string
Keys []string
Registry map[string]string
Keys []string
UserRegistry map[string]string
}
36 changes: 19 additions & 17 deletions internal/registry/write_ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion internal/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 0441585

Please sign in to comment.