github.com/aeternity/aepp-sdk-go@v1.0.3-0.20190606142815-1c0ffdc21fd9/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  // NewAeCli is just a helper function that gives you a aeCli so that
    49  // you don't have to maintain a aeCli global variable (which needs the
    50  // config vars to be read immediately, with this helper function you can
    51  // defer the reading of the variables until the subcommand's execution)
    52  func NewAeCli() *aeternity.Client {
    53  	return aeternity.NewClient(aeternity.Config.Node.URL, debug)
    54  }
    55  
    56  func init() {
    57  	// cobra.OnInitialize(initConfig)
    58  	viper.AutomaticEnv() // read in environment variables that match
    59  	viper.SetEnvPrefix("AETERNITY")
    60  	viper.SetDefault("external-api", aeternity.Config.Node.URL)
    61  	viper.SetDefault("network-id", aeternity.Config.Node.NetworkID)
    62  
    63  	// Here you will define your flags and configuration settings.
    64  	// Cobra supports Persistent Flags, which, if defined here,
    65  	// will be global for your application.
    66  	RootCmd.PersistentFlags().StringVarP(&aeternity.Config.Node.URL, "external-api", "u", aeternity.Config.Node.URL, "node external API endpoint")
    67  	RootCmd.PersistentFlags().StringVarP(&aeternity.Config.Node.NetworkID, "network-id", "n", aeternity.Config.Node.NetworkID, "network ID for custom private net")
    68  	RootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug")
    69  	RootCmd.PersistentFlags().BoolVar(&aeternity.Config.Tuning.OutputFormatJSON, "json", false, "print output in json format")
    70  }