Skip to content

Commit

Permalink
feat: get fiber quota command
Browse files Browse the repository at this point in the history
  • Loading branch information
mempirate committed May 5, 2023
0 parents commit ace5184
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.toml
54 changes: 54 additions & 0 deletions api/fiber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package api

import (
"encoding/json"
"io"
"net/http"
)

type FiberAPI struct {
url string
apiKey string
}

func NewFiberAPI(url string, apiKey string) *FiberAPI {
url = url + "/api/fiber"
return &FiberAPI{url, apiKey}
}

type Quota struct {
EgressMB int `json:"egress_mb"`
MaxEgressMB int `json:"max_egress_mb"`
TransactionCount int `json:"transaction_count"`
ActiveStreams int `json:"active_streams"`
MaxActiveStreams int `json:"max_active_streams"`
}

func (f *FiberAPI) GetQuota() (*Quota, error) {
req, err := http.NewRequest("GET", f.url+"/quota", nil)
if err != nil {
return nil, err
}

req.Header.Set("x-api-key", f.apiKey)
req.Header.Set("Content-Type", "application/json")

res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

quota := new(Quota)
if err := json.Unmarshal(body, quota); err != nil {
return nil, err
}

return quota, nil
}
5 changes: 5 additions & 0 deletions api/fiber_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package api

func testQuota() {

}
109 changes: 109 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"fmt"
"os"

"github.com/chainbound/cbctl/api"

"github.com/urfave/cli/v2"
)

func NewApp() *cli.App {
app := &cli.App{
Name: "cbctl",
Usage: "A command line tool for interacting with the Chainbound API",
Commands: []*cli.Command{
{
Name: "init",
Usage: "Initialize cbctl",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Usage: "The URL of the Chainbound API",
Value: "http://backend.fiberapi.io:8000",
},
&cli.StringFlag{
Name: "key",
Usage: "Your API key",
Required: true,
},
},
Action: func(c *cli.Context) error {
key := c.String("key")
if key == "" {
return fmt.Errorf("API key not provided")
}

url := c.String("url")
if url == "" {
return fmt.Errorf("URL not provided")
}

path, err := inititalize(url, key)
if err != nil {
return err
}

fmt.Printf("Initialized cbctl configuration at %s\n", path)

return nil
},
},
{
Name: "fiber",
Usage: "Interact with the Fiber API",
Subcommands: []*cli.Command{
{
Name: "quota",
Usage: "Get your current usage quota",
Action: func(c *cli.Context) error {
cfg, err := ReadConfig()
if err != nil {
return fmt.Errorf("Error reading config, did you run cbctl init?: %w", err)
}

api := api.NewFiberAPI(cfg.Url, cfg.ApiKey)
quota, err := api.GetQuota()
if err != nil {
return fmt.Errorf("Error getting quota: %w", err)
}

println("Current billing period's quota:")
println(fmt.Sprintf(" EgressMB: %d", quota.EgressMB))
println(fmt.Sprintf(" MaxEgressMB: %d", quota.MaxEgressMB))
println(fmt.Sprintf(" TransactionCount: %d", quota.TransactionCount))
println(fmt.Sprintf(" ActiveStreams: %d", quota.ActiveStreams))
println(fmt.Sprintf(" MaxActiveStreams: %d", quota.MaxActiveStreams))

return nil
},
},
},
},
},
}

return app
}

func inititalize(url, apiKey string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}

if err := os.MkdirAll(home+"/.config/cbctl", 0755); err != nil {
return "", err
}

configBytes := []byte(fmt.Sprintf("url = \"%s\"\napi_key = \"%s\"\n", url, apiKey))

path := home + "/.config/cbctl/config.toml"

if err := os.WriteFile(path, configBytes, 0644); err != nil {
return "", err
}

return path, nil
}
27 changes: 27 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"os"

"github.com/pelletier/go-toml/v2"
)

type Config struct {
Url string
ApiKey string
}

func ReadConfig() (*Config, error) {
path := os.Getenv("HOME") + "/.config/cbctl/config.toml"
f, err := os.ReadFile(path)
if err != nil {
return nil, err
}

cfg := new(Config)
if err := toml.Unmarshal(f, cfg); err != nil {
return nil, err
}

return cfg, nil
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/chainbound/cbctl

go 1.20

require (
github.com/pelletier/go-toml/v2 v2.0.7
github.com/urfave/cli/v2 v2.25.3
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9CjgDb8Us=
github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/urfave/cli/v2 v2.25.3 h1:VJkt6wvEBOoSjPFQvOkv6iWIrsJyCrKGtCtxXWwmGeY=
github.com/urfave/cli/v2 v2.25.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"os"
)

func main() {
app := NewApp()

if err := app.Run(os.Args); err != nil {
panic(err)
}
}

0 comments on commit ace5184

Please sign in to comment.