github.com/gopinath-langote/1build@v1.7.0/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/gopinath-langote/1build/cmd/del"
     6  	"github.com/gopinath-langote/1build/cmd/initialize"
     7  	"github.com/gopinath-langote/1build/cmd/list"
     8  	"github.com/gopinath-langote/1build/cmd/set"
     9  	"github.com/gopinath-langote/1build/cmd/unset"
    10  
    11  	configuration "github.com/gopinath-langote/1build/cmd/config"
    12  	"github.com/gopinath-langote/1build/cmd/exec"
    13  	"github.com/gopinath-langote/1build/cmd/utils"
    14  	"github.com/spf13/cobra"
    15  	"github.com/spf13/viper"
    16  )
    17  
    18  // Cmd cobra for root level
    19  var Cmd = &cobra.Command{
    20  	Use:     "1build",
    21  	Version: "1.4.0",
    22  	Short:   "Frictionless way of managing project-specific commands",
    23  	Args:    cobra.MinimumNArgs(0),
    24  	PreRun: func(cmd *cobra.Command, args []string) {
    25  		_, err := configuration.LoadOneBuildConfiguration()
    26  		if err != nil {
    27  			fmt.Println(err)
    28  			utils.ExitError()
    29  		}
    30  	},
    31  	Run: func(cmd *cobra.Command, args []string) {
    32  		if len(args) < 1 {
    33  			list.Cmd.Run(cmd, args)
    34  		} else {
    35  			exec.ExecutePlan(args...)
    36  		}
    37  	},
    38  }
    39  
    40  // Execute entry-point for cobra app
    41  func Execute() {
    42  	if err := Cmd.Execute(); err != nil {
    43  		fmt.Println(err)
    44  		utils.ExitError()
    45  	}
    46  }
    47  
    48  func init() {
    49  	Cmd.SetHelpCommand(&cobra.Command{Use: "no-help", Hidden: true})
    50  	Cmd.PersistentFlags().BoolP("quiet", "q", false,
    51  		"Hide output log of command & only show SUCCESS/FAILURE result")
    52  	Cmd.PersistentFlags().
    53  		StringP("file", "f", configuration.OneBuildConfigFileName, "The file path for 1build configuration file.")
    54  	_ = viper.BindPFlags(Cmd.PersistentFlags())
    55  
    56  	Cmd.AddCommand(list.Cmd)
    57  	Cmd.AddCommand(del.Cmd)
    58  	Cmd.AddCommand(initialize.Cmd)
    59  	Cmd.AddCommand(set.Cmd)
    60  	Cmd.AddCommand(unset.Cmd)
    61  }