Repository navigation
Expand file tree
/
Copy pathsubscription.go
More file actions
85 lines (76 loc) · 1.88 KB
/
Copy pathsubscription.go
File metadata and controls
85 lines (76 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cmd
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/neosapience/cast/internal/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var subscriptionCmd = &cobra.Command{
Use: "subscription",
Short: "Show current subscription details",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
asJSON, _ := cmd.Flags().GetBool("json")
baseURL := viper.GetString("base_url")
var c *client.Client
if baseURL != "" {
c = client.NewWithBaseURL(viper.GetString("api_key"), baseURL)
} else {
c = client.New(viper.GetString("api_key"))
}
sub, err := c.GetMySubscription()
if err != nil {
return err
}
remaining := sub.Credits.PlanCredits - sub.Credits.UsedCredits
if asJSON {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(struct {
*client.SubscriptionResponse
RemainingCredits int64 `json:"remaining_credits"`
}{
SubscriptionResponse: sub,
RemainingCredits: remaining,
})
}
fmt.Printf("Plan: %s\n", sub.Plan)
fmt.Printf("Credits: %s / %s used\n",
formatInt(sub.Credits.UsedCredits),
formatInt(sub.Credits.PlanCredits))
fmt.Printf("Remaining: %s\n", formatInt(remaining))
fmt.Printf("Concurrency: %d\n", sub.Limits.ConcurrencyLimit)
return nil
},
}
func formatInt(n int64) string {
negative := n < 0
s := strconv.FormatInt(n, 10)
if negative {
s = s[1:] // strip the minus; we re-add it at the end
}
if len(s) <= 3 {
if negative {
return "-" + s
}
return s
}
var result []byte
for i, c := range s {
if i > 0 && (len(s)-i)%3 == 0 {
result = append(result, ',')
}
result = append(result, byte(c))
}
if negative {
return "-" + string(result)
}
return string(result)
}
func init() {
subscriptionCmd.Flags().Bool("json", false, "Output as JSON instead of human-readable text")
rootCmd.AddCommand(subscriptionCmd)
}