@@ -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
2555func 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
0 commit comments