-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (96 loc) · 2.92 KB
/
main.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/dgsb/configlite"
"github.com/alecthomas/kong"
)
type CommonConfig struct {
Database string `long:"db" default:"${default_config_file}" description:"the configuration database file to use"`
}
func (cfg *CommonConfig) GetRepo() *configlite.Repository {
repo, err := configlite.New(cfg.Database)
if err != nil {
log.Fatal("cannot open configuration database", err)
}
return repo
}
type ListAppCmd struct {
CommonConfig `embed:""`
}
func (cmd *ListAppCmd) Run() error {
repo, err := configlite.New(cmd.Database)
if err != nil {
return fmt.Errorf("cannot open configuration database: %w", err)
}
apps, err := repo.GetApps()
if err != nil {
return fmt.Errorf("cannot list registered applications: %w", err)
}
for _, a := range apps {
fmt.Println(a)
}
return nil
}
type ListConfigsCmd struct {
CommonConfig `embed:""`
Format string `short:"f" default:"json" enum:"json,text" description:"the format to display the configuration in"`
Application string `long:"app" arg:"" description:"the application whose configuration has to be displayed"`
}
func (cmd *ListConfigsCmd) Run() error {
repo, err := configlite.New(cmd.Database)
if err != nil {
return fmt.Errorf("cannot open configuration database: %w", err)
}
configs, err := repo.GetConfigs(cmd.Application)
if err != nil {
return fmt.Errorf("cannot get configuration for application %s: %w", cmd.Application, err)
}
switch cmd.Format {
case "json":
jsonConfig, err := json.MarshalIndent(configs, "", " ")
if err != nil {
return fmt.Errorf("json formatting configs: %w", err)
}
fmt.Println(string(jsonConfig))
case "text":
for k, v := range configs {
fmt.Println(k, v)
}
default:
return fmt.Errorf("cannot list configs for application: unknow format %s", cmd.Format)
}
return nil
}
type UpsertConfigCmd struct {
CommonConfig `embed:""`
Application string `arg:""`
Configuration string `arg:""`
Value string `arg:""`
}
func (cmd *UpsertConfigCmd) Run() error {
repo := cmd.GetRepo()
return repo.UpsertConfig(cmd.Application, cmd.Configuration, cmd.Value)
}
type DeleteConfigCmd struct {
CommonConfig `embed:""`
LikePattern bool `long:"like" short:"l" default:"false" help:"the configuration name is going to be used in an sql like clause"`
Application string `arg:""`
Configuration string `arg:""`
}
func (cmd *DeleteConfigCmd) Run() error {
return cmd.GetRepo().DeleteConfig(cmd.Application, cmd.Configuration, cmd.LikePattern)
}
func main() {
var cli struct {
ListApp ListAppCmd `cmd:"" aliases:"la"`
ListConfigs ListConfigsCmd `cmd:"" aliases:"lc"`
UpsertConfig UpsertConfigCmd `cmd:"" aliases:"uc"`
DeleteConfig DeleteConfigCmd `cmd:"" aliases:"dc"`
}
ctx := kong.Parse(&cli, kong.Vars{"default_config_file": configlite.DefaultConfigurationFile()})
if err := ctx.Run(); err != nil {
log.Fatal("cannot run command", err)
}
}