Repository: codenotary/immudb
File: cmd/immuadmin/immuadmin.go (Go)

Code before:
	"github.com/codenotary/immudb/pkg/gw"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"

	"github.com/codenotary/immudb/pkg/client"
)

var o = &c.Options{}
var tokenFileName = client.TokenFileName
var immuClient client.ImmuClient

func init() {
	cobra.OnInitialize(func() { o.InitConfig("immuadmin") })
}

func main() {

	cmd := &cobra.Command{
		Use:   "immuadmin",
		Short: "CLI admin client for the ImmuDB tamperproof database",
		Long: `CLI admin client for the ImmuDB tamperproof database.

Environment variables:
  IMMUADMIN_ADDRESS=127.0.0.1
  IMMUADMIN_PORT=3322
  IMMUADMIN_MTLS=true`,
		SilenceUsage:      true,
		SilenceErrors:     true,
		DisableAutoGenTag: true,
	}

	commands.Init(cmd)

	if err := configureOptions(cmd, o); err != nil {
		c.QuitToStdErr(err)
	}

	if err := cmd.Execute(); err != nil {
		c.QuitToStdErr(err)
	}
}

func configureOptions(cmd *cobra.Command, o *c.Options) error {
	cmd.PersistentFlags().IntP("port", "p", gw.DefaultOptions().ImmudbPort, "immudb port number")
	cmd.PersistentFlags().StringP("address", "a", gw.DefaultOptions().ImmudbAddress, "immudb host address")
	cmd.PersistentFlags().StringVar(&o.CfgFn, "config", "", "config file (default path are configs or $HOME. Default filename is immuclient.ini)")
	cmd.PersistentFlags().BoolP("mtls", "m", client.DefaultOptions().MTLs, "enable mutual tls")
	cmd.PersistentFlags().String("servername", client.DefaultMTLsOptions().Servername, "used to verify the hostname on the returned certificates")
	cmd.PersistentFlags().String("certificate", client.DefaultMTLsOptions().Certificate, "server certificate file path")
	cmd.PersistentFlags().String("pkey", client.DefaultMTLsOptions().Pkey, "server private key path")
	cmd.PersistentFlags().String("clientcas", client.DefaultMTLsOptions().ClientCAs, "clients certificates list. Aka certificate authority")

Review comment (suggestion):
@mmeloni Same remark as dor `SilenceUsage`: we should probably set it dynamically if it’s possible.

Code after:
	"github.com/codenotary/immudb/pkg/gw"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"

	"github.com/codenotary/immudb/pkg/client"
)

var o = &c.Options{}

func init() {
	cobra.OnInitialize(func() { o.InitConfig("immuadmin") })
}

func main() {

	cmd := &cobra.Command{
		Use:   "immuadmin",
		Short: "CLI admin client for the ImmuDB tamperproof database",
		Long: `CLI admin client for the ImmuDB tamperproof database.

Environment variables:
  IMMUADMIN_ADDRESS=127.0.0.1
  IMMUADMIN_PORT=3322
  IMMUADMIN_MTLS=true`,
		SilenceUsage:      true,
		SilenceErrors:     true,
		DisableAutoGenTag: true,
	}

	commands.Init(cmd)

	if err := configureOptions(cmd, o); err != nil {
		c.QuitToStdErr(err)
	}

	if err := cmd.Execute(); err != nil {
		c.QuitToStdErr(err)
	}
}

func configureOptions(cmd *cobra.Command, o *c.Options) error {
	cmd.PersistentFlags().IntP("port", "p", gw.DefaultOptions().ImmudbPort, "immudb port number")
	cmd.PersistentFlags().StringP("address", "a", gw.DefaultOptions().ImmudbAddress, "immudb host address")
	cmd.PersistentFlags().StringVar(&o.CfgFn, "config", "", "config file (default path are configs or $HOME. Default filename is immuclient.ini)")
	cmd.PersistentFlags().BoolP("mtls", "m", client.DefaultOptions().MTLs, "enable mutual tls")
	cmd.PersistentFlags().String("servername", client.DefaultMTLsOptions().Servername, "used to verify the hostname on the returned certificates")
	cmd.PersistentFlags().String("certificate", client.DefaultMTLsOptions().Certificate, "server certificate file path")
	cmd.PersistentFlags().String("pkey", client.DefaultMTLsOptions().Pkey, "server private key path")
	cmd.PersistentFlags().String("clientcas", client.DefaultMTLsOptions().ClientCAs, "clients certificates list. Aka certificate authority")
	if err := viper.BindPFlag("default.port", cmd.PersistentFlags().Lookup("port")); err != nil {
		return err