From 86758c70fabef14a3e0870452248b5c880001e2a Mon Sep 17 00:00:00 2001 From: kanno <812137533@qq.com> Date: Wed, 31 May 2023 17:39:40 +0800 Subject: [PATCH] chore: remove huawei resouce --- go.mod | 2 +- go.sum | 4 +-- internal/source/conf.go | 14 +++------- internal/source/ini.go | 59 +++++++++++++++++++---------------------- 4 files changed, 34 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 8f7e4e8..a6b2fd0 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.17 require ( github.com/manifoldco/promptui v0.9.0 - github.com/nonzzz/ini v0.0.0-20230414084939-6d92cdc1bd80 + github.com/nonzzz/ini v0.0.0-20230531091757-a15bda4e5a8b ) require github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect diff --git a/go.sum b/go.sum index 09ca539..533c76f 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ= github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= -github.com/nonzzz/ini v0.0.0-20230414084939-6d92cdc1bd80 h1:cehvgmhwj93LSOz29UgoixAPnd2q5rX6u+nuM/73GBw= -github.com/nonzzz/ini v0.0.0-20230414084939-6d92cdc1bd80/go.mod h1:t0R1dkD281akpQ0TfHjEnpST6FaY50g8ix538IeLFhc= +github.com/nonzzz/ini v0.0.0-20230531091757-a15bda4e5a8b h1:8Itse4wlbyEwusdxzU4Y+6SdntyPtUFYHctSMSCGDi8= +github.com/nonzzz/ini v0.0.0-20230531091757-a15bda4e5a8b/go.mod h1:t0R1dkD281akpQ0TfHjEnpST6FaY50g8ix538IeLFhc= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/source/conf.go b/internal/source/conf.go index 52ef528..8a97026 100644 --- a/internal/source/conf.go +++ b/internal/source/conf.go @@ -17,24 +17,21 @@ type S uint8 const ( Npm S = iota Yarn - HuaWei Tencent NpmMirror System ) var DefaultSource = map[string]S{ - "https://registry.npmjs.org/": Npm, - "https://registry.yarnpkg.com/": Yarn, - "https://repo.huaweicloud.com/repository/npm/": HuaWei, - "https://mirrors.cloud.tencent.com/npm/": Tencent, - "https://registry.npmmirror.com/": NpmMirror, + "https://registry.npmjs.org/": Npm, + "https://registry.yarnpkg.com/": Yarn, + "https://mirrors.cloud.tencent.com/npm/": Tencent, + "https://registry.npmmirror.com/": NpmMirror, } var DefaultKey = map[S]string{ Npm: "https://registry.npmjs.org/", Yarn: "https://registry.yarnpkg.com/", - HuaWei: "https://repo.huaweicloud.com/repository/npm/", Tencent: "https://mirrors.cloud.tencent.com/npm/", NpmMirror: "https://registry.npmmirror.com/", } @@ -46,8 +43,6 @@ func EnsureDefaultKey(input string) S { s = Npm case Yarn.String(): s = Yarn - case HuaWei.String(): - s = HuaWei case Tencent.String(): s = Tencent case NpmMirror.String(): @@ -61,7 +56,6 @@ func EnsureDefaultKey(input string) S { var SourceToString = []string{ "npm", "yarn", - "huawei", "tencet", "npmMirror", "system", diff --git a/internal/source/ini.go b/internal/source/ini.go index 9949467..20827ec 100644 --- a/internal/source/ini.go +++ b/internal/source/ini.go @@ -1,56 +1,51 @@ package source import ( + "fmt" + "strings" + "github.com/nonzzz/ini" "github.com/nonzzz/ini/pkg/ast" ) -type GrmIniVisitor struct { - key string - val string - ini.IniVisitor -} - -func (v *GrmIniVisitor) Expression(node *ast.Expression) { - if node.Key == v.key { - if v.val == "" { - v.val = node.Value - return - } - node.Value = v.val - } -} - type GrmIni struct { Path string ini *ini.Ini } func NewGrmIniParse(conf *GrmConfig) *GrmIni { - p := &GrmIni{ - ini: ini.New(), - } - p.ini.LoadFile(conf.ConfPath) + p := &GrmIni{} + i, _ := ini.New().LoadFile(conf.ConfPath) + p.ini = i return p } func (i *GrmIni) Set(k, v string) bool { - i.ini.Accept(&GrmIniVisitor{ - key: k, - val: v, + i.ini.Walk(func(node, _ ast.Node) { + switch t := node.(type) { + case *ast.ExpressionNode: + if t.Key == k { + t.Text = fmt.Sprintf("%s = %s", k, v) + } + } }) - return i.ini.Err() == nil + return true } -func (i *GrmIni) Get(k string) string { - v := &GrmIniVisitor{ - key: k, - } - i.ini.Accept(v) - i.Path = v.val - return v.val +func (i *GrmIni) Get(k string) (val string) { + i.ini.Walk(func(node, _ ast.Node) { + switch t := node.(type) { + case *ast.ExpressionNode: + if t.Key == k { + val = strings.TrimSpace(t.Value) + } + } + }) + i.Path = val + return val } func (i *GrmIni) ToString() string { - return i.ini.String() + s, _ := i.ini.Printer() + return s }