Retab README.md

This commit is contained in:
Albert Nigmatzianov 2017-07-26 14:55:05 +02:00
parent 1723331773
commit cb747385b3

366
README.md
View File

@ -151,17 +151,17 @@ In a Cobra app, typically the main.go file is very bare. It serves one purpose:
package main package main
import ( import (
"fmt" "fmt"
"os" "os"
"{pathToYourApp}/cmd" "{pathToYourApp}/cmd"
) )
func main() { func main() {
if err := cmd.RootCmd.Execute(); err != nil { if err := cmd.RootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
} }
``` ```
@ -277,14 +277,14 @@ Ideally you place this in app/cmd/root.go:
```go ```go
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "hugo", Use: "hugo",
Short: "Hugo is a very fast static site generator", Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go. love by spf13 and friends in Go.
Complete documentation is available at http://hugo.spf13.com`, Complete documentation is available at http://hugo.spf13.com`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here // Do Stuff Here
}, },
} }
``` ```
@ -294,54 +294,54 @@ For example cmd/root.go:
```go ```go
import ( import (
"fmt" "fmt"
"os" "os"
homedir "github.com/mitchellh/go-homedir" homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/") RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")
RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution") RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)") RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")
RootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration") RootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")
viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author")) viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))
viper.BindPFlag("projectbase", RootCmd.PersistentFlags().Lookup("projectbase")) viper.BindPFlag("projectbase", RootCmd.PersistentFlags().Lookup("projectbase"))
viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper")) viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>") viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache") viper.SetDefault("license", "apache")
} }
func Execute() { func Execute() {
RootCmd.Execute() RootCmd.Execute()
} }
func initConfig() { func initConfig() {
// Don't forget to read config either from cfgFile or from home directory! // Don't forget to read config either from cfgFile or from home directory!
if cfgFile != "" { if cfgFile != "" {
// Use config file from the flag. // Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
// Find home directory. // Find home directory.
home, err := homedir.Dir() home, err := homedir.Dir()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
// Search config in home directory with name ".cobra" (without extension). // Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home) viper.AddConfigPath(home)
viper.SetConfigName(".cobra") viper.SetConfigName(".cobra")
} }
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
fmt.Println("Can't read config:", err) fmt.Println("Can't read config:", err)
os.Exit(1) os.Exit(1)
} }
} }
``` ```
@ -356,17 +356,17 @@ In a Cobra app, typically the main.go file is very bare. It serves, one purpose,
package main package main
import ( import (
"fmt" "fmt"
"os" "os"
"{pathToYourApp}/cmd" "{pathToYourApp}/cmd"
) )
func main() { func main() {
if err := cmd.RootCmd.Execute(); err != nil { if err := cmd.RootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
} }
``` ```
@ -382,21 +382,21 @@ populate it with the following:
package cmd package cmd
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"fmt" "fmt"
) )
func init() { func init() {
RootCmd.AddCommand(versionCmd) RootCmd.AddCommand(versionCmd)
} }
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
Short: "Print the version number of Hugo", Short: "Print the version number of Hugo",
Long: `All software has versions. This is Hugo's`, Long: `All software has versions. This is Hugo's`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hugo Static Site Generator v0.9 -- HEAD") fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")
}, },
} }
``` ```
@ -453,8 +453,8 @@ You can also bind your flags with [viper](https://github.com/spf13/viper):
var author string var author string
func init() { func init() {
RootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution") RootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")
viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author")) viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))
} }
``` ```
@ -483,19 +483,19 @@ An example of setting the custom validator:
```go ```go
var cmd = &cobra.Command{ var cmd = &cobra.Command{
Short: "hello", Short: "hello",
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 { if len(args) < 1 {
return errors.New("requires at least one arg") return errors.New("requires at least one arg")
} }
if myapp.IsValidColor(args[0]) { if myapp.IsValidColor(args[0]) {
return nil return nil
} }
return fmt.Errorf("invalid color specified: %s", args[0]) return fmt.Errorf("invalid color specified: %s", args[0])
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello, World!") fmt.Println("Hello, World!")
}, },
} }
``` ```
@ -514,56 +514,56 @@ More documentation about flags is available at https://github.com/spf13/pflag
package main package main
import ( import (
"fmt" "fmt"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func main() { func main() {
var echoTimes int var echoTimes int
var cmdPrint = &cobra.Command{ var cmdPrint = &cobra.Command{
Use: "print [string to print]", Use: "print [string to print]",
Short: "Print anything to the screen", Short: "Print anything to the screen",
Long: `print is for printing anything back to the screen. Long: `print is for printing anything back to the screen.
For many years people have printed back to the screen.`, For many years people have printed back to the screen.`,
Args: cobra.MinimumArgs(1), Args: cobra.MinimumArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Print: " + strings.Join(args, " ")) fmt.Println("Print: " + strings.Join(args, " "))
}, },
} }
var cmdEcho = &cobra.Command{ var cmdEcho = &cobra.Command{
Use: "echo [string to echo]", Use: "echo [string to echo]",
Short: "Echo anything to the screen", Short: "Echo anything to the screen",
Long: `echo is for echoing anything back. Long: `echo is for echoing anything back.
Echo works a lot like print, except it has a child command.`, Echo works a lot like print, except it has a child command.`,
Args: cobra.MinimumArgs(1), Args: cobra.MinimumArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Print: " + strings.Join(args, " ")) fmt.Println("Print: " + strings.Join(args, " "))
}, },
} }
var cmdTimes = &cobra.Command{ var cmdTimes = &cobra.Command{
Use: "times [# times] [string to echo]", Use: "times [# times] [string to echo]",
Short: "Echo anything to the screen more times", Short: "Echo anything to the screen more times",
Long: `echo things multiple times back to the user by providing Long: `echo things multiple times back to the user by providing
a count and a string.`, a count and a string.`,
Args: cobra.MinimumArgs(1), Args: cobra.MinimumArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
for i := 0; i < echoTimes; i++ { for i := 0; i < echoTimes; i++ {
fmt.Println("Echo: " + strings.Join(args, " ")) fmt.Println("Echo: " + strings.Join(args, " "))
} }
}, },
} }
cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input") cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")
var rootCmd = &cobra.Command{Use: "app"} var rootCmd = &cobra.Command{Use: "app"}
rootCmd.AddCommand(cmdPrint, cmdEcho) rootCmd.AddCommand(cmdPrint, cmdEcho)
cmdEcho.AddCommand(cmdTimes) cmdEcho.AddCommand(cmdTimes)
rootCmd.Execute() rootCmd.Execute()
} }
``` ```
@ -649,16 +649,16 @@ The default help command is
```go ```go
func (c *Command) initHelp() { func (c *Command) initHelp() {
if c.helpCommand == nil { if c.helpCommand == nil {
c.helpCommand = &Command{ c.helpCommand = &Command{
Use: "help [command]", Use: "help [command]",
Short: "Help about any command", Short: "Help about any command",
Long: `Help provides help for any command in the application. Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`, Simply type ` + c.Name() + ` help [path to command] for full details.`,
Run: c.HelpFunc(), Run: c.HelpFunc(),
} }
} }
c.AddCommand(c.helpCommand) c.AddCommand(c.helpCommand)
} }
``` ```
@ -732,8 +732,8 @@ The default usage function is:
```go ```go
return func(c *Command) error { return func(c *Command) error {
err := tmpl(c.Out(), c.UsageTemplate(), c) err := tmpl(c.Out(), c.UsageTemplate(), c)
return err return err
} }
``` ```
@ -761,57 +761,57 @@ An example of two commands which use all of these features is below. When the s
package main package main
import ( import (
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func main() { func main() {
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "root [sub]", Use: "root [sub]",
Short: "My root command", Short: "My root command",
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside rootCmd PersistentPreRun with args: %v\n", args) fmt.Printf("Inside rootCmd PersistentPreRun with args: %v\n", args)
}, },
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside rootCmd PreRun with args: %v\n", args) fmt.Printf("Inside rootCmd PreRun with args: %v\n", args)
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside rootCmd Run with args: %v\n", args) fmt.Printf("Inside rootCmd Run with args: %v\n", args)
}, },
PostRun: func(cmd *cobra.Command, args []string) { PostRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside rootCmd PostRun with args: %v\n", args) fmt.Printf("Inside rootCmd PostRun with args: %v\n", args)
}, },
PersistentPostRun: func(cmd *cobra.Command, args []string) { PersistentPostRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside rootCmd PersistentPostRun with args: %v\n", args) fmt.Printf("Inside rootCmd PersistentPostRun with args: %v\n", args)
}, },
} }
var subCmd = &cobra.Command{ var subCmd = &cobra.Command{
Use: "sub [no options!]", Use: "sub [no options!]",
Short: "My subcommand", Short: "My subcommand",
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside subCmd PreRun with args: %v\n", args) fmt.Printf("Inside subCmd PreRun with args: %v\n", args)
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside subCmd Run with args: %v\n", args) fmt.Printf("Inside subCmd Run with args: %v\n", args)
}, },
PostRun: func(cmd *cobra.Command, args []string) { PostRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside subCmd PostRun with args: %v\n", args) fmt.Printf("Inside subCmd PostRun with args: %v\n", args)
}, },
PersistentPostRun: func(cmd *cobra.Command, args []string) { PersistentPostRun: func(cmd *cobra.Command, args []string) {
fmt.Printf("Inside subCmd PersistentPostRun with args: %v\n", args) fmt.Printf("Inside subCmd PersistentPostRun with args: %v\n", args)
}, },
} }
rootCmd.AddCommand(subCmd) rootCmd.AddCommand(subCmd)
rootCmd.SetArgs([]string{""}) rootCmd.SetArgs([]string{""})
rootCmd.Execute() rootCmd.Execute()
fmt.Println() fmt.Println()
rootCmd.SetArgs([]string{"sub", "arg1", "arg2"}) rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})
rootCmd.Execute() rootCmd.Execute()
} }
``` ```
@ -837,28 +837,28 @@ command.
package main package main
import ( import (
"errors" "errors"
"log" "log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func main() { func main() {
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "hugo", Use: "hugo",
Short: "Hugo is a very fast static site generator", Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go. love by spf13 and friends in Go.
Complete documentation is available at http://hugo.spf13.com`, Complete documentation is available at http://hugo.spf13.com`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
// Do Stuff Here // Do Stuff Here
return errors.New("some random error") return errors.New("some random error")
}, },
} }
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
``` ```