(cmd *cobra.Command, args []string)
| 34 | } |
| 35 | |
| 36 | func runPricing(cmd *cobra.Command, args []string) error { |
| 37 | if len(pricingProducts) == 0 { |
| 38 | return fmt.Errorf("at least one --products value is required, e.g. \"AWS EC2\"") |
| 39 | } |
| 40 | |
| 41 | attrMap := map[string]string{} |
| 42 | for _, a := range pricingAttrs { |
| 43 | parts := strings.SplitN(a, "=", 2) |
| 44 | if len(parts) != 2 || strings.TrimSpace(parts[0]) == "" { |
| 45 | return fmt.Errorf("invalid attribute filter %q — expected key=value format", a) |
| 46 | } |
| 47 | attrMap[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1]) |
| 48 | } |
| 49 | |
| 50 | client, err := api.New() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if !client.IsInitialized() { |
| 55 | return fmt.Errorf("not authenticated — run 'cloudcent init' first") |
| 56 | } |
| 57 | |
| 58 | regionStr := "all regions" |
| 59 | if len(pricingRegions) > 0 { |
| 60 | regionStr = strings.Join(pricingRegions, ", ") |
| 61 | } |
| 62 | fmt.Printf("Querying pricing for %s in %s…\n", strings.Join(pricingProducts, ", "), regionStr) |
| 63 | |
| 64 | resp, err := client.FetchPricing(pricingProducts, pricingRegions, attrMap, pricingPrices) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | if len(resp.Data) == 0 { |
| 70 | fmt.Println("No results found.") |
| 71 | return nil |
| 72 | } |
| 73 | |
| 74 | limit := pricingLimit |
| 75 | if limit > len(resp.Data) { |
| 76 | limit = len(resp.Data) |
| 77 | } |
| 78 | items := resp.Data[:limit] |
| 79 | fmt.Printf("Showing %d of %d results\n\n", len(items), resp.Total) |
| 80 | |
| 81 | if pricingOutput == "json" { |
| 82 | out, _ := json.MarshalIndent(resp.Data, "", " ") |
| 83 | fmt.Println(string(out)) |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | for _, item := range items { |
| 88 | product := item.Product |
| 89 | if item.Provider != "" { |
| 90 | product = item.Provider + " " + item.Product |
| 91 | } |
| 92 | fmt.Printf(" %s | %s\n", product, item.Region) |
| 93 |
nothing calls this directly
no test coverage detected