github.com/BrandonManuel/git-chglog@v0.0.0-20200903004639-7a62fa08787a/cmd/git-chglog/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 8 "github.com/fatih/color" 9 "github.com/mattn/go-colorable" 10 gitcmd "github.com/tsuyoshiwada/go-gitcmd" 11 "github.com/urfave/cli" 12 ) 13 14 func CreateApp(actionFunc cli.ActionFunc) *cli.App { 15 ttl := color.New(color.FgYellow).SprintFunc() 16 17 cli.AppHelpTemplate = fmt.Sprintf(` 18 %s 19 {{.Name}} [options] <tag query> 20 21 There are the following specification methods for <tag query>. 22 23 1. <old>..<new> - Commit contained in <old> tags from <new>. 24 2. <name>.. - Commit from the <name> to the latest tag. 25 3. ..<name> - Commit from the oldest tag to <name>. 26 4. <name> - Commit contained in <name>. 27 28 %s 29 {{range .Flags}}{{.}} 30 {{end}} 31 %s 32 33 $ {{.Name}} 34 35 If <tag query> is not specified, it corresponds to all tags. 36 This is the simplest example. 37 38 $ {{.Name}} 1.0.0..2.0.0 39 40 The above is a command to generate CHANGELOG including commit of 1.0.0 to 2.0.0. 41 42 $ {{.Name}} 1.0.0 43 44 The above is a command to generate CHANGELOG including commit of only 1.0.0. 45 46 $ {{.Name}} $(git describe --tags $(git rev-list --tags --max-count=1)) 47 48 The above is a command to generate CHANGELOG with the commit included in the latest tag. 49 50 $ {{.Name}} --output CHANGELOG.md 51 52 The above is a command to output to CHANGELOG.md instead of standard output. 53 54 $ {{.Name}} --config custom/dir/config.yml 55 56 The above is a command that uses a configuration file placed other than ".chglog/config.yml". 57 `, 58 ttl("USAGE:"), 59 ttl("OPTIONS:"), 60 ttl("EXAMPLE:"), 61 ) 62 63 cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) { 64 cli.HelpPrinterCustom(colorable.NewColorableStdout(), templ, data, nil) 65 } 66 67 app := cli.NewApp() 68 app.Name = "git-chglog" 69 app.Usage = "todo usage for git-chglog" 70 app.Version = Version 71 72 app.Flags = []cli.Flag{ 73 // init 74 cli.BoolFlag{ 75 Name: "init", 76 Usage: "generate the git-chglog configuration file in interactive", 77 }, 78 79 // config 80 cli.StringFlag{ 81 Name: "config, c", 82 Usage: "specifies a different configuration file to pick up", 83 Value: ".chglog/config.yml", 84 }, 85 86 // output 87 cli.StringFlag{ 88 Name: "output, o", 89 Usage: "output path and filename for the changelogs. If not specified, output to stdout", 90 }, 91 92 cli.StringFlag{ 93 Name: "next-tag", 94 Usage: "treat unreleased commits as specified tags (EXPERIMENTAL)", 95 }, 96 97 // silent 98 cli.BoolFlag{ 99 Name: "silent", 100 Usage: "disable stdout output", 101 }, 102 103 // no-color 104 cli.BoolFlag{ 105 Name: "no-color", 106 Usage: "disable color output", 107 EnvVar: "NO_COLOR", 108 }, 109 110 // no-emoji 111 cli.BoolFlag{ 112 Name: "no-emoji", 113 Usage: "disable emoji output", 114 EnvVar: "NO_EMOJI", 115 }, 116 117 // no-case 118 cli.BoolFlag{ 119 Name: "no-case", 120 Usage: "disable case sensitive filters", 121 }, 122 123 // tag-filter-pattern 124 cli.StringFlag{ 125 Name: "tag-filter-pattern, p", 126 Usage: "Regular expression of tag filter. Is specified, only matched tags will be picked", 127 }, 128 129 // help & version 130 cli.HelpFlag, 131 cli.VersionFlag, 132 } 133 134 app.Action = actionFunc 135 136 return app 137 } 138 139 func AppAction(c *cli.Context) error { 140 wd, err := os.Getwd() 141 if err != nil { 142 fmt.Fprintln(os.Stderr, "failed to get working directory", err) 143 os.Exit(ExitCodeError) 144 } 145 146 // initializer 147 if c.Bool("init") { 148 initializer := NewInitializer( 149 &InitContext{ 150 WorkingDir: wd, 151 Stdout: colorable.NewColorableStdout(), 152 Stderr: colorable.NewColorableStderr(), 153 }, 154 fs, 155 NewQuestioner( 156 gitcmd.New(&gitcmd.Config{ 157 Bin: "git", 158 }), 159 fs, 160 ), 161 NewConfigBuilder(), 162 templateBuilderFactory, 163 ) 164 165 os.Exit(initializer.Run()) 166 } 167 168 // chglog 169 chglogCLI := NewCLI( 170 &CLIContext{ 171 WorkingDir: wd, 172 Stdout: colorable.NewColorableStdout(), 173 Stderr: colorable.NewColorableStderr(), 174 ConfigPath: c.String("config"), 175 OutputPath: c.String("output"), 176 Silent: c.Bool("silent"), 177 NoColor: c.Bool("no-color"), 178 NoEmoji: c.Bool("no-emoji"), 179 NoCaseSensitive: c.Bool("no-case"), 180 Query: c.Args().First(), 181 NextTag: c.String("next-tag"), 182 TagFilterPattern: c.String("tag-filter-pattern"), 183 }, 184 fs, 185 NewConfigLoader(), 186 NewGenerator(), 187 ) 188 189 os.Exit(chglogCLI.Run()) 190 191 return nil 192 } 193 194 func main() { 195 app := CreateApp(AppAction) 196 app.Run(os.Args) 197 }