command

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GitCheck = func(cCtx *cli.Context) error {
	_, err := os.Stat(".git")
	if err != nil {
		if os.IsNotExist(err) {
			return cli.Exit("Must run in Git repository", 1)
		} else {
			return cli.Exit("Could not detect Git repository", 1)
		}
	}
	return nil
}
View Source
var SetTemplateCommand = &cli.Command{
	Name:  "set",
	Usage: "Set up template for commit message",
	Flags: []cli.Flag{
		&cli.BoolFlag{Name: "dry-run", Aliases: []string{"d"}, Usage: "Print template to stdout", Value: false},
		&cli.StringFlag{Name: "issue", Aliases: []string{"i"}, Usage: "Issue reference to add to template", Value: ""},
		&cli.StringSliceFlag{Name: "pair", Aliases: []string{"p"}, Usage: "Co-author(s) to add to template", Value: cli.NewStringSlice()},
	},
	Before: GitCheck,
	Action: func(c *cli.Context) error {
		var cfg Config
		err := envconfig.Process("GIT_COMMIT_TEMPLATE", &cfg)
		if err != nil {
			return cli.Exit("Reading environment variables failed", 1)
		}

		var f *os.File
		if c.Bool("dry-run") {
			f = os.Stdout
		} else {
			var err error
			f, err = os.Create(".git/.gitmessage.txt")
			if err != nil {
				return cli.Exit("Saving template file failed", 1)
			}
			defer f.Close()
		}

		_ = message.Execute(f, struct {
			Issue     string
			CoAuthors []string
		}{issueRef(c.String("issue"), cfg.IssuePrefix), findCoAuthors(c.StringSlice("pair"), cfg.AuthorFile)})

		if !c.Bool("dry-run") {
			err := exec.Command("git", "config", "--local", "commit.template", ".git/.gitmessage.txt").Run()
			if err != nil {
				return cli.Exit("Updating Git config failed", 1)
			}
		}

		return nil
	},
}
View Source
var UnsetTemplateCommand = &cli.Command{
	Name:   "unset",
	Usage:  "Unset current template",
	Before: GitCheck,
	Action: func(c *cli.Context) error {

		_ = os.Remove(".git/.gitmessage.txt")

		_ = exec.Command("git", "config", "--local", "--unset", "commit.template").Run()
		return nil
	},
}

Functions

This section is empty.

Types

type Config added in v0.1.1

type Config struct {
	IssuePrefix string `split_words:"true" default:"#"`
	AuthorFile  string `split_words:"true" default:"$HOME/.git-commit-template-authors"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL