Skip to content

Commit

Permalink
meta: use new metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian authored and yndu13 committed Dec 26, 2023
1 parent a505142 commit 64756ba
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
7 changes: 3 additions & 4 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,7 +16,6 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,7 +17,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
}
Expand Down
81 changes: 81 additions & 0 deletions newmeta/meta.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions newmeta/meta_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 3 additions & 1 deletion openapi/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 64756ba

Please sign in to comment.