github.com/aeternity/aepp-sdk-go/v4@v4.0.1/cmd/root.go (about)

     1  // Copyright © 2018 NAME HERE <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/aeternity/aepp-sdk-go/aeternity"
    22  	"github.com/spf13/cobra"
    23  	"github.com/spf13/viper"
    24  )
    25  
    26  // RootCmd represents the base command when called without any subcommands
    27  var RootCmd = &cobra.Command{
    28  	Use:   "aecli",
    29  	Short: "The command line client for the Aeternity blockchain",
    30  	Long:  ``,
    31  	// Uncomment the following line if your bare application
    32  	// has an action associated with it:
    33  	//	Run: func(cmd *cobra.Command, args []string) { },
    34  }
    35  
    36  var debug bool
    37  
    38  // Execute adds all child commands to the root command sets flags appropriately.
    39  // This is called by main.main(). It only needs to happen once to the rootCmd.
    40  func Execute(v string) {
    41  	RootCmd.Version = v
    42  	if err := RootCmd.Execute(); err != nil {
    43  		fmt.Println(err)
    44  		os.Exit(-1)
    45  	}
    46  }
    47  
    48  // NewAeNode is just a helper function that gives you a Node so that you don't
    49  // have to maintain a Node global variable (which needs the config vars to be
    50  // read immediately, with this helper function you can defer the reading of the
    51  // variables until the subcommand's execution) Defined as a var so unittests can
    52  // mock this function out.
    53  var NewAeNode = func() *aeternity.Node {
    54  	return aeternity.NewNode(aeternity.Config.Node.URL, debug)
    55  }
    56  
    57  // NewCompiler is just a helper function that gives you a Compiler so that you don't
    58  // have to maintain a Compiler global variable (which needs the config vars to be
    59  // read immediately, with this helper function you can defer the reading of the
    60  // variables until the subcommand's execution) Defined as a var so unittests can
    61  // mock this function out.
    62  var NewCompiler = func() *aeternity.Compiler {
    63  	return aeternity.NewCompiler(compilerURL, debug)
    64  }
    65  
    66  func init() {
    67  	// cobra.OnInitialize(initConfig)
    68  	viper.AutomaticEnv() // read in environment variables that match
    69  	viper.SetEnvPrefix("AETERNITY")
    70  	viper.SetDefault("external-api", aeternity.Config.Node.URL)
    71  	viper.SetDefault("network-id", aeternity.Config.Node.NetworkID)
    72  
    73  	// Here you will define your flags and configuration settings.
    74  	// Cobra supports Persistent Flags, which, if defined here,
    75  	// will be global for your application.
    76  	RootCmd.PersistentFlags().StringVarP(&aeternity.Config.Node.URL, "external-api", "u", aeternity.Config.Node.URL, "node external API endpoint")
    77  	RootCmd.PersistentFlags().StringVarP(&aeternity.Config.Node.NetworkID, "network-id", "n", aeternity.Config.Node.NetworkID, "network ID for custom private net")
    78  	RootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug")
    79  	RootCmd.PersistentFlags().BoolVar(&aeternity.Config.Tuning.OutputFormatJSON, "json", false, "print output in json format")
    80  }