github.com/aeternity/aepp-sdk-go/v6@v6.0.0/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/v6/config"
    22  	"github.com/aeternity/aepp-sdk-go/v6/naet"
    23  	"github.com/spf13/cobra"
    24  	"github.com/spf13/viper"
    25  )
    26  
    27  // RootCmd represents the base command when called without any subcommands
    28  var RootCmd = &cobra.Command{
    29  	Use:   "aecli",
    30  	Short: "The command line client for the Aeternity blockchain",
    31  	Long:  ``,
    32  	// Uncomment the following line if your bare application
    33  	// has an action associated with it:
    34  	//	Run: func(cmd *cobra.Command, args []string) { },
    35  }
    36  
    37  var debug bool
    38  var online bool
    39  
    40  // Execute adds all child commands to the root command sets flags appropriately.
    41  // This is called by main.main(). It only needs to happen once to the rootCmd.
    42  func Execute(v string) {
    43  	RootCmd.Version = v
    44  	if err := RootCmd.Execute(); err != nil {
    45  		fmt.Println(err)
    46  		os.Exit(-1)
    47  	}
    48  }
    49  
    50  // newAeNode is just a helper function that gives you a Node so that you don't
    51  // have to maintain a Node global variable (which needs the config vars to be
    52  // read immediately, with this helper function you can defer the reading of the
    53  // variables until the subcommand's execution)
    54  func newAeNode() naet.NodeInterface {
    55  	return naet.NewNode(config.Node.URL, debug)
    56  }
    57  
    58  // newCompiler is just a helper function that gives you a Compiler so that you don't
    59  // have to maintain a Compiler global variable (which needs the config vars to be
    60  // read immediately, with this helper function you can defer the reading of the
    61  // variables until the subcommand's execution)
    62  func newCompiler() *naet.Compiler {
    63  	return naet.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", config.Node.URL)
    71  	viper.SetDefault("network-id", 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(&config.Node.URL, "external-api", "u", config.Node.URL, "node external API endpoint")
    77  	RootCmd.PersistentFlags().StringVarP(&config.Node.NetworkID, "network-id", "n", config.Node.NetworkID, "network ID for custom private net")
    78  	RootCmd.PersistentFlags().StringVarP(&compilerURL, "compiler-url", "c", "http://localhost:3080", "Compiler URL")
    79  	RootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug")
    80  	RootCmd.PersistentFlags().BoolVar(&config.Tuning.OutputFormatJSON, "json", false, "print output in json format")
    81  }