Skip to content

Commit 949b635

Browse files
committed
Improve setup utility
1 parent 1a04e57 commit 949b635

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

cmd/mytoken-server/mytoken-migratedb/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ import (
66
"strings"
77
"time"
88

9+
log "github.com/sirupsen/logrus"
910
"github.com/zachmann/cli/v2"
1011
"golang.org/x/term"
1112

1213
"github.com/oidc-mytoken/server/internal/config"
1314
"github.com/oidc-mytoken/server/internal/db"
14-
"github.com/oidc-mytoken/server/shared/utils/fileutil"
15-
16-
log "github.com/sirupsen/logrus"
17-
1815
"github.com/oidc-mytoken/server/internal/model/version"
16+
"github.com/oidc-mytoken/server/shared/utils/fileutil"
1917
)
2018

2119
var configFile string

cmd/mytoken-server/mytoken-setup/setup.go

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,63 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7+
"time"
78

89
"github.com/Songmu/prompter"
9-
"github.com/jessevdk/go-flags"
10-
"github.com/pkg/errors"
1110
log "github.com/sirupsen/logrus"
11+
"github.com/zachmann/cli/v2"
1212

1313
"github.com/oidc-mytoken/server/internal/config"
1414
"github.com/oidc-mytoken/server/internal/jws"
15+
"github.com/oidc-mytoken/server/internal/model/version"
1516
loggerUtils "github.com/oidc-mytoken/server/internal/utils/logger"
1617
"github.com/oidc-mytoken/server/internal/utils/zipdownload"
1718
"github.com/oidc-mytoken/server/shared/utils/fileutil"
1819
)
1920

20-
var genSigningKeyComm commandGenSigningKey
21-
var installComm struct {
22-
GeoIP commandInstallGeoIPDB `command:"geoip-db" description:"Installs the ip geolocation database."`
21+
var app = &cli.App{
22+
Name: "mytoken-setup",
23+
Usage: "Command line client for easily setting up a mytoken server",
24+
Version: version.VERSION(),
25+
Compiled: time.Time{},
26+
Authors: []*cli.Author{{
27+
Name: "Gabriel Zachmann",
28+
29+
}},
30+
Copyright: "Karlsruhe Institute of Technology 2020-2021",
31+
UseShortOptionHandling: true,
32+
Commands: cli.Commands{
33+
&cli.Command{
34+
Name: "signing-key",
35+
Aliases: []string{"key"},
36+
Usage: "Generates a new signing key",
37+
Description: "Generates a new signing key according to the properties specified in the config file and stores it.",
38+
Action: createSigningKey,
39+
},
40+
&cli.Command{
41+
Name: "install",
42+
Usage: "Installs needed dependencies",
43+
Subcommands: cli.Commands{
44+
&cli.Command{
45+
Name: "geoip-db",
46+
Aliases: []string{"geo-ip-db"},
47+
Usage: "Installs the ip geolocation database.",
48+
Action: installGEOIPDB,
49+
},
50+
},
51+
},
52+
},
2353
}
2454

2555
func main() {
2656
config.LoadForSetup()
2757
loggerUtils.Init()
28-
29-
parser := flags.NewNamedParser("mytoken", flags.HelpFlag|flags.PassDoubleDash)
30-
if _, err := parser.AddCommand("signing-key", "Generates a new signing key", "Generates a new signing key according to the properties specified in the config file and stores it.", &genSigningKeyComm); err != nil {
31-
log.WithError(err).Fatal()
32-
os.Exit(1)
33-
}
34-
if _, err := parser.AddCommand("install", "Installs needed dependencies", "", &installComm); err != nil {
35-
log.WithError(err).Fatal()
36-
os.Exit(1)
37-
}
38-
if _, err := parser.Parse(); err != nil {
39-
var flagError *flags.Error
40-
if errors.As(err, &flagError) {
41-
if flagError.Type == flags.ErrHelp {
42-
fmt.Println(err)
43-
os.Exit(0)
44-
}
45-
}
46-
log.WithError(err).Fatal()
47-
os.Exit(1)
58+
if err := app.Run(os.Args); err != nil {
59+
log.Fatal(err)
4860
}
49-
5061
}
5162

52-
type commandGenSigningKey struct{}
53-
type commandInstallGeoIPDB struct{}
54-
55-
// Execute implements the flags.Commander interface
56-
func (c *commandInstallGeoIPDB) Execute(args []string) error {
63+
func installGEOIPDB(context *cli.Context) error {
5764
archive, err := zipdownload.DownloadZipped("https://download.ip2location.com/lite/IP2LOCATION-LITE-DB1.IPV6.BIN.ZIP")
5865
if err != nil {
5966
return err
@@ -67,8 +74,7 @@ func (c *commandInstallGeoIPDB) Execute(args []string) error {
6774
return err
6875
}
6976

70-
// Execute implements the flags.Commander interface
71-
func (c *commandGenSigningKey) Execute(args []string) error {
77+
func createSigningKey(context *cli.Context) error {
7278
sk, _, err := jws.GenerateKeyPair()
7379
if err != nil {
7480
return err

internal/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/coreos/go-oidc/v3/oidc"
88
"github.com/pkg/errors"
99
log "github.com/sirupsen/logrus"
10-
yaml "gopkg.in/yaml.v3"
10+
"gopkg.in/yaml.v3"
1111

1212
model2 "github.com/oidc-mytoken/server/internal/model"
1313
"github.com/oidc-mytoken/server/internal/utils/errorfmt"
@@ -368,4 +368,5 @@ func load() {
368368
// not required for setup
369369
func LoadForSetup() {
370370
load()
371+
conf.Logging.Internal.StdErr = true
371372
}

0 commit comments

Comments
 (0)