github.com/fredbi/git-chglog@v0.0.0-20190706071416-d35c598eac81/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 main() {
    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  		// help & version
   118  		cli.HelpFlag,
   119  		cli.VersionFlag,
   120  	}
   121  
   122  	app.Action = func(c *cli.Context) error {
   123  		wd, err := os.Getwd()
   124  		if err != nil {
   125  			fmt.Fprintln(os.Stderr, "failed to get working directory", err)
   126  			os.Exit(ExitCodeError)
   127  		}
   128  
   129  		// initializer
   130  		if c.Bool("init") {
   131  			initializer := NewInitializer(
   132  				&InitContext{
   133  					WorkingDir: wd,
   134  					Stdout:     colorable.NewColorableStdout(),
   135  					Stderr:     colorable.NewColorableStderr(),
   136  				},
   137  				fs,
   138  				NewQuestioner(
   139  					gitcmd.New(&gitcmd.Config{
   140  						Bin: "git",
   141  					}),
   142  					fs,
   143  				),
   144  				NewConfigBuilder(),
   145  				templateBuilderFactory,
   146  			)
   147  
   148  			os.Exit(initializer.Run())
   149  		}
   150  
   151  		// chglog
   152  		chglogCLI := NewCLI(
   153  			&CLIContext{
   154  				WorkingDir: wd,
   155  				Stdout:     colorable.NewColorableStdout(),
   156  				Stderr:     colorable.NewColorableStderr(),
   157  				ConfigPath: c.String("config"),
   158  				OutputPath: c.String("output"),
   159  				Silent:     c.Bool("silent"),
   160  				NoColor:    c.Bool("no-color"),
   161  				NoEmoji:    c.Bool("no-emoji"),
   162  				Query:      c.Args().First(),
   163  				NextTag:    c.String("next-tag"),
   164  			},
   165  			fs,
   166  			NewConfigLoader(),
   167  			NewGenerator(),
   168  		)
   169  
   170  		os.Exit(chglogCLI.Run())
   171  
   172  		return nil
   173  	}
   174  
   175  	app.Run(os.Args)
   176  }