Skip to content

Commit 4ca8064

Browse files
authored
Merge pull request #6 from mgrzybek/catalog
Call /public/catalog
2 parents 66d1505 + 0ed0fc7 commit 4ca8064

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

cmd/catalog.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"encoding/json"
20+
21+
"github.com/spf13/cobra"
22+
"onyxiactl/utils"
23+
)
24+
25+
type Catalog struct {
26+
ID string `json:"id"`
27+
Description string `json:"description"`
28+
LastUpdateTime uint `json:"lastUpdateTime"`
29+
Location string `json:"location"`
30+
Maintainer string `json:"maintainer"`
31+
Name string `json:"name"`
32+
Status string `json:"status"`
33+
Type string `json:"type"`
34+
}
35+
36+
type Catalogs struct {
37+
Catalogs []Catalog `json:"catalogs"`
38+
}
39+
40+
func init() {
41+
rootCmd.AddCommand(catalogCmd)
42+
catalogCmd.AddCommand(catalogListCmd)
43+
catalogCmd.PersistentFlags().StringP("id", "i", "", "Catalog’s ID.")
44+
}
45+
46+
var catalogCmd = &cobra.Command{
47+
Use: "catalog",
48+
Short: "Manage the catalogs.",
49+
Long: `Manage the catalogs. By default the command lists the available catalogs.`,
50+
Run: func(cmd *cobra.Command, args []string) { catalogList(cmd) },
51+
}
52+
53+
var catalogListCmd = &cobra.Command{
54+
Use: "list",
55+
Short: "List the available catalogs (default).",
56+
Long: `List the available catalogs.`,
57+
Run: func(cmd *cobra.Command, args []string) { catalogList(cmd) },
58+
}
59+
60+
func catalogList(cmd *cobra.Command) error {
61+
token, _ := cmd.Flags().GetString("token")
62+
onyxiaURL, _ := cmd.Flags().GetString("onyxiaURL")
63+
id, _ := cmd.Flags().GetString("id")
64+
65+
if len(id) == 0 {
66+
return catalogListAll(onyxiaURL+"/public/catalog", token)
67+
}
68+
69+
return catalogListId(onyxiaURL+"/public/catalog", token, id)
70+
}
71+
72+
func catalogListAll(url, token string) error {
73+
catalog := &Catalogs{}
74+
JsonCatalog, e := utils.CallAPIGet(url, token)
75+
if e != nil {
76+
panic(e)
77+
}
78+
79+
e = json.Unmarshal(JsonCatalog, &catalog)
80+
if e != nil {
81+
panic(e)
82+
}
83+
84+
for _, v := range catalog.Catalogs {
85+
utils.PrintStruct(v)
86+
}
87+
88+
return nil
89+
}
90+
91+
func catalogListId(url, token, id string) error {
92+
catalog := &Catalog{}
93+
JsonCatalog, e := utils.CallAPIGet(url+"/"+id, token)
94+
if e != nil {
95+
panic(e)
96+
}
97+
98+
e = json.Unmarshal(JsonCatalog, &catalog)
99+
if e != nil {
100+
panic(e)
101+
}
102+
103+
utils.PrintStruct(catalog)
104+
105+
return nil
106+
}

utils/api.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"errors"
8+
)
9+
10+
func CallAPIGet(url, token string) ([]byte, error) {
11+
req, _ := http.NewRequest("GET", url, nil)
12+
req.Header.Add("Authorization", "Bearer "+token)
13+
req.Header.Set("Content-Type", "application/json")
14+
15+
client := &http.Client{}
16+
res, e := client.Do(req)
17+
if e != nil {
18+
return []byte(""), e
19+
}
20+
if res.StatusCode != 200 {
21+
return []byte(""), errors.New(fmt.Sprintf("API returned code %d", res.StatusCode))
22+
}
23+
body, e := io.ReadAll(res.Body)
24+
25+
return body, e
26+
}

utils/console.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"encoding/json"
6+
)
7+
8+
func PrintStruct(v interface{}) {
9+
j, _ := json.Marshal(v)
10+
fmt.Println(string(j))
11+
}
12+
13+
func PrintStructs(v []interface{}) {
14+
for i := range(v) {
15+
PrintStruct(i)
16+
}
17+
}

0 commit comments

Comments
 (0)