-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (149 loc) · 3.63 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
_ "embed"
"fmt"
"log"
"os"
"github.com/charmbracelet/glamour"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
var (
//go:embed LICENSE.md
license string
// Version is the version string to be set at compile time via command line.
version string
)
//nolint:funlen
func main() {
if isatty.IsTerminal(os.Stdout.Fd()) {
log.SetFlags(0)
}
app := cli.NewApp()
app.Name = "aarsstjerner"
app.Usage = "Lav liste over årsstjerner"
app.EnableBashCompletion = true
app.Authors = []*cli.Author{
{
Name: "Arne Jørgensen",
Email: "[email protected]",
},
}
app.Version = getVersion()
app.Copyright = fmt.Sprintf("MIT License, run `%s license` to view", app.Name)
configPath := ""
userConfigDir, err := os.UserConfigDir()
if err == nil {
configPath = userConfigDir + "/aarsstjerner.yaml"
}
app.Flags = []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "username",
Value: "",
Usage: "The Medlemsservice `username`",
EnvVars: []string{"MS_USERNAME"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "1pass",
Value: "",
Usage: "A 1Password secret `reference` for the Medlemsservice password",
EnvVars: []string{"MS_1PASS"},
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: "slack",
Value: 90, //nolint:gomnd
Usage: "Add `days` of slack in the calculation",
EnvVars: []string{"AARSSTJERNER_SLACK"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "ms-url",
Value: "https://medlem.dds.dk",
Usage: "The Medlemsservice `URL`",
EnvVars: []string{"MS_URL"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "ms-database",
Value: "dds",
Usage: "The Medlemsservice database `name`",
EnvVars: []string{"MS_DATABASE"},
}),
&cli.StringFlag{
Name: "config",
Value: configPath,
Usage: "Read config from `file`",
EnvVars: []string{"AARSSTJERNER_CONFIG_FILE"},
TakesFile: true,
},
&cli.BoolFlag{
Name: "all",
Value: false,
Usage: "Include all not just those who needs new",
EnvVars: []string{"AARSSTJERNER_ALL"},
},
&cli.BoolFlag{
Name: "fake-names",
Value: false,
Usage: "Display fake names (for demo purposes)",
EnvVars: []string{"AARSSTJERNER_FAKE_NAMES"},
Hidden: true,
},
}
app.Commands = []*cli.Command{
{
Name: "browser",
Usage: "Open årsstjerner in browser (default)",
Action: runBrowser,
},
{
Name: "markdown",
Usage: "List årsstjerner as markdown",
Action: list,
},
{
Name: "term",
Usage: "List årsstjerner in terminal",
Action: term,
},
{
Name: "license",
Usage: "View the license",
Action: func(c *cli.Context) error {
result, err := glamour.Render(license, "auto")
if err != nil {
return fmt.Errorf("rendering license: %w", err)
}
fmt.Fprintln(os.Stdout, result)
return nil
},
},
{
Name: "edit-config",
Usage: "Open config file in editor",
Action: editConfig,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "editor",
Value: "vi",
Usage: "Use `EDITOR` to edit config file (create it of it doesn't exist)",
EnvVars: []string{"EDITOR"},
},
},
},
}
app.DefaultCommand = "browser"
app.Before = func(ctx *cli.Context) error {
if _, err := os.Stat(configPath); !os.IsNotExist(err) {
initConfig := altsrc.InitInputSourceWithContext(app.Flags, altsrc.NewYamlSourceFromFlagFunc("config"))
err = initConfig(ctx)
if err != nil {
return fmt.Errorf("reading config file: %w", err)
}
}
return nil
}
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}