github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/cmd.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package cmd
    10  
    11  import (
    12  	"fmt"
    13  	"os"
    14  	"strings"
    15  	"syscall"
    16  
    17  	"github.com/vchain-us/vcn/pkg/cmd/alert"
    18  
    19  	"golang.org/x/crypto/ssh/terminal"
    20  
    21  	"github.com/vchain-us/vcn/pkg/cmd/bom"
    22  	"github.com/vchain-us/vcn/pkg/cmd/dashboard"
    23  	"github.com/vchain-us/vcn/pkg/cmd/info"
    24  	"github.com/vchain-us/vcn/pkg/cmd/inspect"
    25  	"github.com/vchain-us/vcn/pkg/cmd/internal/cli"
    26  	"github.com/vchain-us/vcn/pkg/cmd/internal/types"
    27  	"github.com/vchain-us/vcn/pkg/cmd/list"
    28  	"github.com/vchain-us/vcn/pkg/cmd/login"
    29  	"github.com/vchain-us/vcn/pkg/cmd/logout"
    30  	"github.com/vchain-us/vcn/pkg/cmd/serve"
    31  	"github.com/vchain-us/vcn/pkg/cmd/set"
    32  	"github.com/vchain-us/vcn/pkg/cmd/sign"
    33  	"github.com/vchain-us/vcn/pkg/cmd/verify"
    34  	"github.com/vchain-us/vcn/pkg/meta"
    35  	"github.com/vchain-us/vcn/pkg/store"
    36  
    37  	"github.com/inconshreveable/mousetrap"
    38  	"github.com/spf13/cobra"
    39  	"github.com/spf13/viper"
    40  )
    41  
    42  var cfgFile string
    43  
    44  // rootCmd represents the base command when called without any subcommands
    45  var rootCmd = &cobra.Command{
    46  	Use:     "vcn",
    47  	Version: meta.Version(),
    48  	Short:   "vChain CodeNotary - Notarize and authenticate, from code to production",
    49  	Long:    ``,
    50  }
    51  
    52  // Root returns the root &cobra.Command
    53  func Root() *cobra.Command {
    54  	return rootCmd
    55  }
    56  
    57  // Execute adds all child commands to the root command and sets flags appropriately.
    58  // This is called by main.main(). It only needs to happen once to the rootCmd.
    59  func Execute() {
    60  	var err error
    61  	var cmd *cobra.Command
    62  	var output string
    63  	if cmd, err = rootCmd.ExecuteC(); err != nil {
    64  		if !viper.IsSet("exit-code") {
    65  			viper.Set("exit-code", 1)
    66  		}
    67  		output, _ = rootCmd.PersistentFlags().GetString("output")
    68  		if output != "" && !cmd.SilenceErrors {
    69  			cli.PrintError(output, types.NewError(err))
    70  		}
    71  	}
    72  	// disable version check on lc context
    73  	var versionCheck = false
    74  	if store.Config() != nil && store.Config().CurrentContext.Email != "" {
    75  		versionCheck = true
    76  	}
    77  	preExitHook(rootCmd, versionCheck)
    78  
    79  	exitCode := meta.VcnDefaultExitCode
    80  	if viper.IsSet("exit-code") {
    81  		exitCode = viper.GetInt("exit-code")
    82  	}
    83  	os.Exit(exitCode)
    84  }
    85  
    86  func init() {
    87  
    88  	// Read in environment variables that match
    89  	viper.SetEnvPrefix(strings.ToUpper(meta.VcnPrefix))
    90  	viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
    91  	viper.AutomaticEnv()
    92  
    93  	// Set config files directory based on os.TempDir method ( Linux: /temp/.vcn, Windows: c:\temp, c:\windows\temp )
    94  	if err := store.SetDefaultDir(); err != nil {
    95  		fmt.Println(err)
    96  		os.Exit(1)
    97  	}
    98  
    99  	// Disable default behavior when started through explorer.exe
   100  	cobra.MousetrapHelpText = ""
   101  
   102  	cobra.OnInitialize(initConfig)
   103  
   104  	// Here you will define your flags and configuration settings.
   105  	// Cobra supports persistent flags, which, if defined here,
   106  	// will be global for your application.
   107  	rootCmd.PersistentFlags().StringVar(&cfgFile, "vcnpath", "", "config files (default is /tmp/.vcn/config.json on linux, c:\\temp\\config.json or c:\\windows\\temp\\config.json on Windows)")
   108  	rootCmd.PersistentFlags().StringP("output", "o", "", "output format, one of: --output=json|--output=yaml|--output=''. In Codenotary Cloud authenticate command is possible to specify also --output=attachments. It permits to download all items attached to an artifact.")
   109  	rootCmd.PersistentFlags().BoolP("silent", "S", false, "silent mode, don't show progress spinner, but it will still output the result")
   110  	rootCmd.PersistentFlags().BoolP("quit", "q", true, "if false, ask for confirmation before quitting")
   111  	rootCmd.PersistentFlags().Bool("verbose", false, "if true, print additional information")
   112  	//rootCmd.PersistentFlags().String("vcnpath", "", "if false, ask for confirmation before quitting")
   113  
   114  	rootCmd.PersistentFlags().MarkHidden("quit")
   115  
   116  	// Root command flags
   117  	rootCmd.Flags().BoolP("version", "v", false, "version for vcn") // needed for -v shorthand
   118  
   119  	// Verification group
   120  	rootCmd.AddCommand(verify.NewCommand())
   121  	rootCmd.AddCommand(inspect.NewCommand())
   122  	rootCmd.AddCommand(list.NewCommand())
   123  
   124  	// Signing group
   125  	rootCmd.AddCommand(sign.NewCommand())
   126  	rootCmd.AddCommand(sign.NewUntrustCommand())
   127  	rootCmd.AddCommand(sign.NewUnsupportCommand())
   128  
   129  	// User group
   130  	rootCmd.AddCommand(login.NewCommand())
   131  	rootCmd.AddCommand(logout.NewCommand())
   132  	rootCmd.AddCommand(dashboard.NewCommand())
   133  	rootCmd.AddCommand(info.NewCommand())
   134  
   135  	// Set command
   136  	rootCmd.AddCommand(set.NewCommand())
   137  
   138  	// Serve command
   139  	rootCmd.AddCommand(serve.NewCommand())
   140  
   141  	// Alert comand
   142  	rootCmd.AddCommand(alert.NewCommand())
   143  
   144  	// BoM
   145  	rootCmd.AddCommand(bom.NewCommand())
   146  }
   147  
   148  func preExitHook(cmd *cobra.Command, versionCheck bool) {
   149  	if output, _ := rootCmd.PersistentFlags().GetString("output"); output == "" && versionCheck {
   150  		cli.CheckVersion()
   151  	}
   152  
   153  	if quit, _ := cmd.PersistentFlags().GetBool("quit"); !quit || mousetrap.StartedByExplorer() {
   154  		fmt.Println()
   155  		fmt.Println("Press 'Enter' to continue...")
   156  		terminal.ReadPassword(int(syscall.Stdin))
   157  	}
   158  }