github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/cmd/root.go (about) 1 // Copyright 2021 The TrueBlocks Authors. All rights reserved. 2 // Use of this source code is governed by a license that can 3 // be found in the LICENSE file. 4 5 package cmd 6 7 import ( 8 "errors" 9 "fmt" 10 "os" 11 12 "github.com/spf13/cobra" 13 ) 14 15 // chifraCmd represents the base command when called without any subcommands 16 var chifraCmd = &cobra.Command{ 17 Use: "chifra [flags] commands", 18 Short: "access to all TrueBlocks tools (chifra <cmd> --help for more)", 19 Long: `Purpose: 20 Access to all TrueBlocks tools (chifra <cmd> --help for more).`, 21 Version: versionText, 22 } 23 24 // Execute adds all child commands to the root command and sets flags appropriately. 25 func Execute() { 26 if err := chifraCmd.Execute(); err != nil { 27 // fmt.Fprintf(os.Stderr, "%s", err) 28 os.Exit(1) 29 } 30 } 31 32 func init() { 33 chifraCmd.SetOut(os.Stderr) 34 chifraCmd.SetFlagErrorFunc(ErrFunc) 35 chifraCmd.Flags().SortFlags = false 36 chifraCmd.SetUsageTemplate(helpText) 37 } 38 39 func ErrFunc(cmd *cobra.Command, errMsg error) error { 40 msg := fmt.Sprintf("%s", errMsg) 41 if os.Getenv("TEST_MODE") == "true" { 42 msg = "\n " + msg + "\n" 43 } else { 44 msg = "\n \033[31m" + msg + "\033[0m\n" 45 } 46 return errors.New(msg) 47 } 48 49 func UsageWithNotes(notes string) string { 50 t := `Usage:{{if .Runnable}} 51 {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} 52 {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}} 53 54 Aliases: 55 {{.NameAndAliases}}{{end}}{{if .HasExample}} 56 57 Examples: 58 {{.Example}}{{end}}{{if .HasAvailableSubCommands}} 59 60 Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}} 61 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} 62 63 Flags: 64 {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} 65 66 Global Flags: 67 {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}} 68 69 Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}} 70 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}} 71 72 Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}} 73 ` 74 75 usage := t + coloredNotes(notes) 76 if usage != "" && usage[len(usage)-1] != '\n' { 77 usage += "\n" 78 } 79 return usage 80 } 81 82 // TODO: Coloring in notes (search in makeClass for this note) 83 func coloredNotes(notes string) string { 84 // if !strings.Contains(notes, "++") { 85 // return notes 86 // } 87 // if os.Getenv("NO_COLOR") == "true" { 88 // return strings.Replace(notes, "++", "", -1) 89 // } 90 // cnt := 0 91 // for { 92 // if !strings.Contains(notes, "++") { 93 // break 94 // } 95 // if cnt%2 == 0 { 96 // notes = strings.Replace(notes, "++", colors.Cyan, 1) 97 // } else { 98 // notes = strings.Replace(notes, "++", colors.Off, 1) 99 // } 100 // cnt++ 101 // } 102 // if cnt%2 == 1 { 103 // logger.Panic("Mismatched backticks in notes. Quitting...") 104 // } 105 106 return notes 107 }