Skip to content

Commit a513d7e

Browse files
committed
Add usage information
Signed-off-by: Flipez <code@brauser.io>
1 parent 9ac7724 commit a513d7e

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

database.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"log"
67

78
"github.com/hetznercloud/hcloud-go/hcloud"
@@ -13,6 +14,7 @@ type Database struct {
1314
Client *hcloud.Client
1415
Context context.Context
1516
Self *hcloud.SSHKey
17+
NoInfo bool
1618
}
1719

1820
func (d *Database) Init() {
@@ -42,6 +44,8 @@ func (d *Database) Fetch() *hcloud.SSHKey {
4244
log.Fatalf("database %s not found", d.Name)
4345
}
4446

47+
checkSize(d)
48+
4549
return d.Self
4650
}
4751

@@ -57,6 +61,9 @@ func (d *Database) Set(key, value string) bool {
5761

5862
d.Store = database.Labels
5963

64+
log.Println("OK")
65+
checkSize(d)
66+
6067
return true
6168
}
6269

@@ -78,3 +85,11 @@ func (d *Database) List() []string {
7885

7986
return keys
8087
}
88+
89+
func checkSize(db *Database) {
90+
if !db.NoInfo {
91+
jsonStr, _ := json.Marshal(db.Store)
92+
log.Printf("[Info] Database usage: %.2f%%", float64(len(jsonStr))/float64(maxDBBytes)*100)
93+
}
94+
}
95+

main.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"github.com/urfave/cli/v2"
1212
)
1313

14+
var (
15+
maxDBBytes = 1946
16+
)
17+
1418
func main() {
1519
log.SetFlags(0)
1620

@@ -23,7 +27,7 @@ func main() {
2327
Aliases: []string{"i"},
2428
Usage: "initializes a new database",
2529
Action: func(cCtx *cli.Context) error {
26-
database := setupDB(cCtx.String("db"))
30+
database := setupDB(cCtx)
2731
database.Init()
2832
return nil
2933
},
@@ -33,7 +37,7 @@ func main() {
3337
Aliases: []string{"s"},
3438
Usage: "sets a key",
3539
Action: func(cCtx *cli.Context) error {
36-
database := setupDB(cCtx.String("db"))
40+
database := setupDB(cCtx)
3741

3842
key := cCtx.Args().First()
3943
val := cCtx.Args().Get(1)
@@ -51,7 +55,7 @@ func main() {
5155
Aliases: []string{"g"},
5256
Usage: "get a value from given key",
5357
Action: func(cCtx *cli.Context) error {
54-
database := setupDB(cCtx.String("db"))
58+
database := setupDB(cCtx)
5559
fmt.Println(database.Get(cCtx.Args().First()))
5660
return nil
5761
},
@@ -61,7 +65,7 @@ func main() {
6165
Aliases: []string{"l"},
6266
Usage: "list all keys",
6367
Action: func(cCtx *cli.Context) error {
64-
database := setupDB(cCtx.String("db"))
68+
database := setupDB(cCtx)
6569
keys := database.List()
6670
fmt.Println(strings.Join(keys, "\n"))
6771
return nil
@@ -74,6 +78,11 @@ func main() {
7478
Value: "0",
7579
Usage: "database to use",
7680
},
81+
&cli.BoolFlag{
82+
Name: "no-info",
83+
Value: false,
84+
Usage: "Do not print db usage information",
85+
},
7786
},
7887
}
7988

@@ -83,12 +92,15 @@ func main() {
8392

8493
}
8594

86-
func setupDB(name string) Database {
95+
func setupDB(ctx *cli.Context) Database {
8796
token := os.Getenv("HCLOUD_TOKEN")
97+
name := ctx.String("db")
98+
noInfo := ctx.Bool("no-info")
8899

89100
return Database{
90101
Client: hcloud.NewClient(hcloud.WithToken(token)),
91102
Context: context.Background(),
92103
Name: fmt.Sprintf("hkv-%s", name),
104+
NoInfo: noInfo,
93105
}
94106
}

0 commit comments

Comments
 (0)