Commit 33c3b9b6 authored by Sietse Ringers's avatar Sietse Ringers

refactor: unify irma server package with main irma package

parent 286c1655
......@@ -5,7 +5,9 @@ import (
"os"
"runtime"
"github.com/go-errors/errors"
irma "github.com/privacybydesign/irmago"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
......@@ -16,6 +18,17 @@ var RootCmd = &cobra.Command{
Long: "IRMA toolkit v" + irma.Version + "\nDocumentation: https://irma.app/docs",
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print irma version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(RootCmd.Long)
fmt.Println()
fmt.Println("Version: ", irma.Version)
fmt.Println("OS/Arg: ", runtime.GOOS+"/"+runtime.GOARCH)
},
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
os.Exit(-1)
......@@ -23,16 +36,7 @@ func Execute() {
}
func init() {
RootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Print irma version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(RootCmd.Long)
fmt.Println()
fmt.Println("Version: ", irma.Version)
fmt.Println("OS/Arg: ", runtime.GOOS+"/"+runtime.GOARCH)
},
})
RootCmd.AddCommand(versionCmd)
}
func die(message string, err error) {
......@@ -41,8 +45,13 @@ func die(message string, err error) {
m = message + ": "
}
if err != nil {
m = m + err.Error()
if e, ok := err.(*errors.Error); ok && logger.IsLevelEnabled(logrus.DebugLevel) {
m += e.ErrorStack()
} else {
m = m + err.Error()
}
}
fmt.Println(m)
logger.Error(m)
os.Exit(1)
}
......@@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"
)
var CheckCommand = &cobra.Command{
var serverCheckCmd = &cobra.Command{
Use: "check",
Short: "Check server configuration correctness",
Long: `check reads the server configuration like the main command does, from a
......@@ -17,8 +17,8 @@ that the configuration is valid.
Specify -v to see the configuration.`,
Run: func(command *cobra.Command, args []string) {
if err := configure(command); err != nil {
die(errors.WrapPrefix(err, "Failed to read configuration from file, args, or env vars", 0))
if err := configureServer(command); err != nil {
die("", errors.WrapPrefix(err, "Failed to read configuration from file, args, or env vars", 0))
}
// Hack: temporarily disable scheme updating to prevent verifyConfiguration() from immediately updating schemes
......@@ -26,7 +26,7 @@ Specify -v to see the configuration.`,
conf.DisableSchemesUpdate = true
if _, err := requestorserver.New(conf); err != nil {
die(errors.WrapPrefix(err, "Invalid configuration", 0))
die("", errors.WrapPrefix(err, "Invalid configuration", 0))
}
conf.DisableSchemesUpdate = enabled // restore previous value before printing configuration
......@@ -36,9 +36,9 @@ Specify -v to see the configuration.`,
}
func init() {
RootCommand.AddCommand(CheckCommand)
serverCmd.AddCommand(serverCheckCmd)
if err := setFlags(CheckCommand, productionMode()); err != nil {
die(errors.WrapPrefix(err, "Failed to attach flags to "+CheckCommand.Name()+" command", 0))
if err := setFlags(serverCheckCmd, productionMode()); err != nil {
die("", errors.WrapPrefix(err, "Failed to attach flags to "+serverCheckCmd.Name()+" command", 0))
}
}
package cmd
import (
"github.com/go-errors/errors"
"github.com/spf13/cobra"
)
var RunCommand = &cobra.Command{
var serverRunCmd = &cobra.Command{
Use: "run",
Short: "Run server (same as specifying no command)",
Run: RootCommand.Run,
Run: serverCmd.Run,
}
func init() {
RootCommand.AddCommand(RunCommand)
serverCmd.AddCommand(serverRunCmd)
if err := setFlags(RunCommand, productionMode()); err != nil {
die(errors.WrapPrefix(err, "Failed to attach flags to "+RunCommand.Name()+" command", 0))
if err := setFlags(serverRunCmd, productionMode()); err != nil {
die("Failed to attach flags to "+serverRunCmd.Name()+" command", err)
}
}
This diff is collapsed.
......@@ -43,7 +43,7 @@ irma session --issue irma-demo.MijnOverheid.ageLower=yes,yes,yes,no --disclose i
irma session --request '{"type":"disclosing","content":[{"label":"BSN","attributes":["irma-demo.MijnOverheid.root.BSN"]}]}'
irma session --server http://localhost:8088 --authmethod token --key mytoken --disclose irma-demo.MijnOverheid.root.BSN`,
Run: func(cmd *cobra.Command, args []string) {
request, irmaconfig, err := configure(cmd)
request, irmaconfig, err := configureSession(cmd)
if err != nil {
die("", err)
}
......@@ -94,7 +94,7 @@ func libraryRequest(
noqr bool,
verbosity int,
) (*server.SessionResult, error) {
if err := configureServer(url, port, privatekeysPath, irmaconfig, verbosity); err != nil {
if err := configureSessionServer(url, port, privatekeysPath, irmaconfig, verbosity); err != nil {
return nil, err
}
startServer(port)
......@@ -192,7 +192,7 @@ func postRequest(serverurl string, request irma.RequestorRequest, name, authmeth
// Configuration functions
func configureServer(url string, port int, privatekeysPath string, irmaconfig *irma.Configuration, verbosity int) error {
func configureSessionServer(url string, port int, privatekeysPath string, irmaconfig *irma.Configuration, verbosity int) error {
// Replace "port" in url with actual port
replace := "$1:" + strconv.Itoa(port)
url = string(regexp.MustCompile("(https?://[^/]*):port").ReplaceAll([]byte(url), []byte(replace)))
......@@ -213,7 +213,7 @@ func configureServer(url string, port int, privatekeysPath string, irmaconfig *i
return err
}
func configure(cmd *cobra.Command) (irma.RequestorRequest, *irma.Configuration, error) {
func configureSession(cmd *cobra.Command) (irma.RequestorRequest, *irma.Configuration, error) {
verbosity, _ := cmd.Flags().GetCount("verbose")
logger.Level = server.Verbosity(verbosity)
irma.Logger = logger
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment