diff --git a/config/configuration.go b/config/configuration.go index 340192d72..7ad7cf893 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -16,7 +16,6 @@ package config import ( "encoding/json" "fmt" - "io/ioutil" "os" "runtime" @@ -162,7 +161,7 @@ func LoadConfiguration(path string) (conf *Configuration, err error) { return } - bytes, err := ioutil.ReadFile(path) + bytes, err := os.ReadFile(path) if err != nil { err = fmt.Errorf("reading config from '%s' failed %v", path, err) return @@ -179,7 +178,7 @@ func SaveConfiguration(config *Configuration) (err error) { return } path := GetConfigPath() + "/" + configFile - err = ioutil.WriteFile(path, bytes, 0600) + err = os.WriteFile(path, bytes, 0600) return } diff --git a/config/configure.go b/config/configure.go index 76e99c6ac..50f4e28f5 100644 --- a/config/configure.go +++ b/config/configure.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -17,7 +17,6 @@ import ( "bufio" "fmt" "io" - "io/ioutil" "os" "strconv" "strings" @@ -246,7 +245,7 @@ func configureChainableRamRoleArn(w io.Writer, cp *Profile) error { func configureRsaKeyPair(w io.Writer, cp *Profile) error { cli.Printf(w, "Rsa Private Key File: ") keyFile := ReadInput("") - buf, err := ioutil.ReadFile(keyFile) + buf, err := os.ReadFile(keyFile) if err != nil { return fmt.Errorf("read key file %s failed %v", keyFile, err) } diff --git a/newmeta/meta.go b/newmeta/meta.go new file mode 100644 index 000000000..df4069f67 --- /dev/null +++ b/newmeta/meta.go @@ -0,0 +1,81 @@ +package newmeta + +import ( + "encoding/json" + "strings" + + aliyunopenapimeta "github.com/aliyun/aliyun-cli/aliyun-openapi-meta" +) + +// { +// "products": [ +// { +// "code": "ARMS", +// "name": "Application Real-Time Monitoring Service", +// "version": "2019-08-08", +// "endpointType": "regional", +// "endpoints": { +// "us-west-1": { +// "regionId": "us-west-1", +// "regionName": "US (Silicon Valley)", +// "areaId": "europeAmerica", +// "areaName": "Europe & Americas", +// "public": "arms.us-west-1.aliyuncs.com", +// "vpc": "arms-vpc.us-east-1.aliyuncs.com" +// } +// } +// } +// ] +// } + +type ProductSet struct { + Products []Product `json:"products"` +} + +type Product struct { + Code string `json:"code"` + Name string `json:"name"` + Version string `json:"version"` + EndpointType string `json:"endpointType"` + Endpoints map[string]Endpoint `json:"endpoints"` +} + +type Endpoint struct { + RegionId string `json:"regionId"` + Name string `json:"regionName"` + Version string `json:"areaId"` + EndpointType string `json:"areaName"` + Public string `json:"public"` + VPC string `json:"vpc"` +} + +func GetProductName(language, code string) (name string, err error) { + content, err := GetMetadata(language, "/products.json") + if err != nil { + return + } + + products := new(ProductSet) + err = json.Unmarshal(content, &products) + + for _, p := range products.Products { + if strings.EqualFold(p.Code, code) { + name = strings.TrimSpace(p.Name) + break + } + } + + return +} + +func GetMetadataPrefix(language string) string { + if language == "en" { + return "en-US" + } + return "zh-CN" +} + +func GetMetadata(language string, path string) (content []byte, err error) { + content, err = aliyunopenapimeta.Metadatas.ReadFile(GetMetadataPrefix(language) + path) + return +} diff --git a/newmeta/meta_test.go b/newmeta/meta_test.go new file mode 100644 index 000000000..035a2be44 --- /dev/null +++ b/newmeta/meta_test.go @@ -0,0 +1,19 @@ +package newmeta + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetMetadataPrefix(t *testing.T) { + assert.Equal(t, "zh-CN", GetMetadataPrefix("")) + assert.Equal(t, "en-US", GetMetadataPrefix("en")) + assert.Equal(t, "zh-CN", GetMetadataPrefix("zh")) +} + +func TestGetMetadata(t *testing.T) { + content, err := GetMetadata("en", "/products.json") + assert.Nil(t, err) + assert.Greater(t, len(content), 100) +} diff --git a/openapi/library.go b/openapi/library.go index 9771da1b9..349c3b1d2 100644 --- a/openapi/library.go +++ b/openapi/library.go @@ -23,6 +23,7 @@ import ( "github.com/aliyun/aliyun-cli/cli" "github.com/aliyun/aliyun-cli/i18n" "github.com/aliyun/aliyun-cli/meta" + "github.com/aliyun/aliyun-cli/newmeta" ) type Library struct { @@ -65,7 +66,8 @@ func (a *Library) PrintProducts() { }) for _, product := range a.builtinRepo.Products { - cli.PrintfWithColor(w, cli.Cyan, " %s\t%s\n", strings.ToLower(product.Code), product.Name[i18n.GetLanguage()]) + var productName, _ = newmeta.GetProductName(i18n.GetLanguage(), product.Code) + cli.PrintfWithColor(w, cli.Cyan, " %-20s\t%s\n", strings.ToLower(product.Code), productName) } w.Flush() }