|
| 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 | +} |
0 commit comments