root.go 2.52 KB
Newer Older
1
// Package cmd handles flags and commands management.
ElyKar's avatar
ElyKar committed
2 3 4 5 6
package cmd

import (
	"fmt"

7
	"dfss"
8
	dapi "dfss/dfssd/api"
ElyKar's avatar
ElyKar committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// RootCmd is the main command for the dfsst application
var RootCmd = &cobra.Command{
	Use:   "dfsst",
	Short: "DFSS TTP v" + dfss.Version,
	Long: `DFSS TTP v ` + dfss.Version + `

Trusted third party resolver for the
Distributed Fair Signing System project

Sign your contract using a secure cryptographic protocol`,
	Run: func(cmd *cobra.Command, args []string) {
		_ = cmd.Help()
	},
26 27 28 29 30 31
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		dapi.Configure(viper.GetBool("verbose"), viper.GetString("demo") != "", viper.GetString("demo"), "ttp")
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		dapi.DClose()
	},
ElyKar's avatar
ElyKar committed
32 33 34 35 36 37 38 39 40 41 42
}

// All of the flags will be gathered by viper, this is why
// we do not store their values
func init() {
	// Bind flags to the dfsst command
	RootCmd.PersistentFlags().BoolP("verbose", "v", false, "print verbose messages")
	RootCmd.PersistentFlags().String("ca", "ca.pem", "path to the root certificate")
	RootCmd.PersistentFlags().String("cert", "cert.pem", "path to the ttp's certificate")
	RootCmd.PersistentFlags().String("key", "key.pem", "path to the ttp's private key")
	RootCmd.PersistentFlags().StringP("demo", "d", "", "demonstrator address and port, empty will disable it")
43 44 45 46

	startCmd.Flags().StringP("address", "a", "0.0.0.0", "address to bind for listening")
	startCmd.Flags().String("db", "mongodb://localhost/dfss", "server url in standard MongoDB format to access the database")
	startCmd.Flags().IntP("port", "p", 9020, "port to bind for listening")
ElyKar's avatar
ElyKar committed
47 48 49 50 51 52 53

	// Store flag values into viper
	_ = viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
	_ = viper.BindPFlag("file_ca", RootCmd.PersistentFlags().Lookup("ca"))
	_ = viper.BindPFlag("file_cert", RootCmd.PersistentFlags().Lookup("cert"))
	_ = viper.BindPFlag("file_key", RootCmd.PersistentFlags().Lookup("key"))
	_ = viper.BindPFlag("demo", RootCmd.PersistentFlags().Lookup("demo"))
54 55 56 57

	_ = viper.BindPFlag("port", startCmd.Flags().Lookup("port"))
	_ = viper.BindPFlag("address", startCmd.Flags().Lookup("address"))
	_ = viper.BindPFlag("dbURI", startCmd.Flags().Lookup("db"))
ElyKar's avatar
ElyKar committed
58 59 60 61 62 63 64

	if err := viper.BindEnv("password", "DFSS_TTP_PASSWORD"); err != nil {
		fmt.Println("Warning: The DFSS_TTP_PASSWORD environment variable is not set, assuming the private key is decrypted")
		viper.Set("password", "")
	}

	// Register Sub Commands
65
	RootCmd.AddCommand(dfss.VersionCmd, startCmd)
ElyKar's avatar
ElyKar committed
66 67

}