Skip to content

Commit

Permalink
refactor: move flags to a diff file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakiyo committed Aug 23, 2023
1 parent 86b715c commit 502a603
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
34 changes: 34 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"

"github.com/Yakiyo/tilde/meta"
"github.com/Yakiyo/tilde/utils"
)

func init() {
rootCmd.SetVersionTemplate(func() string {
return `tilde {{printf "version %s" .Version}}` +
fmt.Sprintf("\ntldr spec version %v\n", meta.TldrSpec)
}())

f := rootCmd.Flags()
// config flags
f.String("log-level", "", "Set log level [debug, info, warn, error]")
f.StringP("dir", "d", "", "Set root directory to use for tilde")
f.StringP("config", "c", "", "Set path to config file")
f.String("color", "", "Enable or disable color output")

// command-ish flags
f.BoolP("list", "l", false, "List all commands in cache")
f.BoolP("update", "u", false, "Update local cache")
f.Bool("clear-cache", false, "Clear local cache")
f.Bool("seed-config", false, "Creates the default configuration file at the default location")
f.StringP("render", "f", "", "Render a local file")

// command related flags
f.StringP("style", "s", "", "Set output style [fancy, plain, raw]")
f.StringP("language", "L", "", "Override language")
f.StringP("platform", "p", utils.Platform(), "Override operating system")
}
40 changes: 9 additions & 31 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ View community driven and simplified man pages in your terminal`,
config.Read()

log.SetLevel(log.ParseLevel(viper.GetString("log_level")))
where.SetRoot(viper.GetString("root_dir"))
return nil
},
Args: cobra.MaximumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
log.Debug(viper.AllSettings())

Expand Down Expand Up @@ -68,6 +70,13 @@ View community driven and simplified man pages in your terminal`,
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Error(err)
os.Exit(1)
}
}

func init() {
cc.Init(&cc.Config{
RootCmd: rootCmd,
Headings: cc.HiCyan + cc.Bold + cc.Underline,
Expand All @@ -78,35 +87,4 @@ func Execute() {
FlagsDataType: cc.Italic + cc.HiBlue,
NoExtraNewlines: true,
})

if err := rootCmd.Execute(); err != nil {
log.Error(err)
os.Exit(1)
}
}

func init() {
rootCmd.SetVersionTemplate(func() string {
return `tilde {{printf "version %s" .Version}}` +
fmt.Sprintf("\ntldr spec version %v\n", meta.TldrSpec)
}())

f := rootCmd.Flags()
// config flags
f.String("log-level", "", "Set log level [debug, info, warn, error]")
f.StringP("dir", "d", "", "Set root directory to use for tilde")
f.StringP("config", "c", "", "Set path to config file")
f.String("color", "", "Enable or disable color output")

// command-ish flags
f.BoolP("list", "l", false, "List all commands in cache")
f.BoolP("update", "u", false, "Update local cache")
f.Bool("clear-cache", false, "Clear local cache")
f.Bool("seed-config", false, "Creates the default configuration file at the default location")
f.StringP("render", "f", "", "Render a local file")

// command related flags
f.StringP("style", "s", "", "Set output style [fancy, plain, raw]")
f.StringP("language", "L", "", "Override language")
f.StringP("platform", "p", utils.Platform(), "Override operating system")
}

0 comments on commit 502a603

Please # to comment.