github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/commands/root.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"strings"
     7  
     8  	cli "github.com/robgonnella/ardi/v2/cli-wrapper"
     9  	"github.com/robgonnella/ardi/v2/core"
    10  	"github.com/robgonnella/ardi/v2/util"
    11  	log "github.com/sirupsen/logrus"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  // CommandEnv environment for all commands
    16  type CommandEnv struct {
    17  	Logger   *log.Logger
    18  	Verbose  bool
    19  	Quiet    bool
    20  	ArdiCore *core.ArdiCore
    21  	MockCli  cli.Cli
    22  }
    23  
    24  type ardiLogFormatter struct {
    25  	log.TextFormatter
    26  }
    27  
    28  func (a *ardiLogFormatter) Format(e *log.Entry) ([]byte, error) {
    29  	b, err := a.TextFormatter.Format(e)
    30  	if err != nil {
    31  		return b, err
    32  	}
    33  	str := string(b)
    34  	str = strings.Replace(str, strings.ToUpper(e.Level.String()), "ardi", 1)
    35  	return []byte(str), nil
    36  }
    37  
    38  func setLogger(env *CommandEnv) {
    39  	env.Logger.SetFormatter(&ardiLogFormatter{
    40  		TextFormatter: log.TextFormatter{
    41  			DisableTimestamp:       true,
    42  			DisableLevelTruncation: true,
    43  			PadLevelText:           true,
    44  		},
    45  	})
    46  
    47  	if env.Verbose {
    48  		env.Logger.SetLevel(log.DebugLevel)
    49  		return
    50  	}
    51  
    52  	if env.Quiet {
    53  		env.Logger.SetLevel(log.FatalLevel)
    54  	} else {
    55  		env.Logger.SetLevel(log.InfoLevel)
    56  	}
    57  
    58  	log.SetOutput(ioutil.Discard)
    59  }
    60  
    61  func requireProjectInit() error {
    62  	if !util.IsProjectDirectory() {
    63  		return errors.New("not an ardi project directory, run 'ardi project-init' first")
    64  	}
    65  	return nil
    66  }
    67  
    68  func getRootCommand(env *CommandEnv) *cobra.Command {
    69  	rootCmd := &cobra.Command{
    70  		Use:   "ardi",
    71  		Short: "Ardi is a command line build manager for arduino projects.",
    72  		Long: "\nArdi is a build tool that allows you to completely manage your arduino project from command line!\n\n" +
    73  			"- Manage and store build configurations for projects with versioned dependencies\n- Run builds in CI Pipeline\n" +
    74  			"- Compile & upload sketches to connected boards\n- Watch log output from connected boards in terminal\n" +
    75  			"- Auto recompile / reupload on save",
    76  		DisableAutoGenTag: true,
    77  	}
    78  
    79  	rootCmd.PersistentFlags().BoolVarP(&env.Verbose, "verbose", "v", false, "Print all logs")
    80  	rootCmd.PersistentFlags().BoolVarP(&env.Quiet, "quiet", "q", false, "Silence all logs")
    81  	rootCmd.SetHelpFunc(Help)
    82  	return rootCmd
    83  }
    84  
    85  // GetRootCmd adds all ardi commands to root and returns root command
    86  func GetRootCmd(env *CommandEnv) *cobra.Command {
    87  	setLogger(env)
    88  	rootCmd := getRootCommand(env)
    89  	rootCmd.AddCommand(
    90  		getAddCmd(env),
    91  		getCleanCmd(env),
    92  		getCompileCmd(env),
    93  		getCompileAndUploadCmd(env),
    94  		getInstallCmd(env),
    95  		getListCmd(env),
    96  		getProjectInitCmd(env),
    97  		getRemoveCmd(env),
    98  		getSearchCmd(env),
    99  		getUploadCmd(env),
   100  		getVersionCmd(env),
   101  		getWatchCmd(env),
   102  		getAttachCmd(env),
   103  	)
   104  	return rootCmd
   105  }