This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
cli.go
102 lines (85 loc) · 2.75 KB
/
cli.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
// Kiebitz - Privacy-Friendly Appointment Scheduling
// Copyright (C) 2021-2021 The Kiebitz Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version. Additional terms
// as defined in section 7 of the license (e.g. regarding attribution)
// are specified at https://kiebitz.eu/en/docs/open-source/additional-terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package helpers
import (
"github.com/impfen/services-inoeg"
"github.com/urfave/cli"
"os"
)
type decorator func(f func(c *cli.Context) error) func(c *cli.Context) error
func decorate(commands []cli.Command, decorator decorator) []cli.Command {
newCommands := make([]cli.Command, len(commands))
for i, command := range commands {
if command.Action != nil {
command.Action = decorator(command.Action.(func(c *cli.Context) error))
}
if command.Subcommands != nil {
command.Subcommands = decorate(command.Subcommands, decorator)
}
newCommands[i] = command
}
return newCommands
}
func CLI(settings *services.Settings) {
var err error
init := func(f func(c *cli.Context) error) func(c *cli.Context) error {
return func(c *cli.Context) error {
level := c.GlobalString("level")
logLevel, err := services.ParseLevel(level)
if err != nil {
return err
}
services.Log.SetLevel(logLevel)
runner := func() error { return f(c) }
profiler := c.GlobalString("profile")
if profiler != "" {
return runWithProfiler(profiler, runner)
}
return f(c)
}
}
app := cli.NewApp()
app.Name = "Kiebitz"
app.Usage = "Run all Kiebitz commands"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "level",
Value: "info",
Usage: "The desired log level",
},
cli.StringFlag{
Name: "profile",
Value: "",
Usage: "enable profiler and store results to given filename",
},
}
bareCommands := []cli.Command{}
// we add commands from the definitions
for _, commandsDefinition := range settings.Definitions.CommandsDefinitions {
if commands, err := commandsDefinition.Maker(settings); err != nil {
services.Log.Fatal(err)
} else {
bareCommands = append(bareCommands, commands...)
}
}
app.Commands = decorate(bareCommands, init)
err = app.Run(os.Args)
if err != nil {
services.Log.Error(err)
}
}