|
| 1 | +// Package cmd contains commitlint cli |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + |
| 7 | + cli "github.com/urfave/cli/v2" |
| 8 | + |
| 9 | + "github.com/conventionalcommit/commitlint/internal" |
| 10 | +) |
| 11 | + |
| 12 | +// newCmd returns commitlint cli.App |
| 13 | +func newCmd() *cli.App { |
| 14 | + cmds := []*cli.Command{ |
| 15 | + initCmd(), |
| 16 | + lintCmd(), |
| 17 | + configCmd(), |
| 18 | + hookCmd(), |
| 19 | + } |
| 20 | + |
| 21 | + app := &cli.App{ |
| 22 | + Name: "commitlint", |
| 23 | + Usage: "linter for conventional commits", |
| 24 | + Commands: cmds, |
| 25 | + Version: internal.FullVersion(), |
| 26 | + } |
| 27 | + return app |
| 28 | +} |
| 29 | + |
| 30 | +func lintCmd() *cli.Command { |
| 31 | + return &cli.Command{ |
| 32 | + Name: "lint", |
| 33 | + Usage: "Check commit message against lint rules", |
| 34 | + Flags: []cli.Flag{ |
| 35 | + &cli.StringFlag{ |
| 36 | + Name: "config", |
| 37 | + Aliases: []string{"c", "conf"}, |
| 38 | + Value: "", |
| 39 | + Usage: "optional config file `conf.yaml`", |
| 40 | + }, |
| 41 | + &cli.StringFlag{ |
| 42 | + Name: "message", |
| 43 | + Aliases: []string{"m", "msg"}, |
| 44 | + Value: "", |
| 45 | + Usage: "path to commit message `FILE`", |
| 46 | + }, |
| 47 | + }, |
| 48 | + Action: func(ctx *cli.Context) error { |
| 49 | + confFilePath := ctx.String("config") |
| 50 | + fileInput := ctx.String("message") |
| 51 | + return lintMsg(confFilePath, fileInput) |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func initCmd() *cli.Command { |
| 57 | + confFlag := formConfFlag() |
| 58 | + replaceFlag := formReplaceFlag() |
| 59 | + |
| 60 | + globalFlag := &cli.BoolFlag{ |
| 61 | + Name: "global", |
| 62 | + Aliases: []string{"g"}, |
| 63 | + Usage: "Sets git hook in global config", |
| 64 | + } |
| 65 | + |
| 66 | + return &cli.Command{ |
| 67 | + Name: "init", |
| 68 | + Usage: "Setup commitlint for git repos", |
| 69 | + Flags: []cli.Flag{globalFlag, confFlag, replaceFlag}, |
| 70 | + Action: func(ctx *cli.Context) error { |
| 71 | + confPath := ctx.String("config") |
| 72 | + isGlobal := ctx.Bool("global") |
| 73 | + isReplace := ctx.Bool("replace") |
| 74 | + |
| 75 | + err := initLint(confPath, isGlobal, isReplace) |
| 76 | + if err != nil { |
| 77 | + if isHookExists(err) { |
| 78 | + fmt.Println("commitlint init failed") |
| 79 | + fmt.Println("run with --replace to replace existing files") |
| 80 | + return nil |
| 81 | + } |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + fmt.Println("commitlint init successfully") |
| 86 | + return nil |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func configCmd() *cli.Command { |
| 92 | + createCmd := &cli.Command{ |
| 93 | + Name: "create", |
| 94 | + Usage: "Creates default config in current directory", |
| 95 | + Flags: []cli.Flag{ |
| 96 | + &cli.BoolFlag{ |
| 97 | + Name: "replace", |
| 98 | + Aliases: []string{"r"}, |
| 99 | + Usage: "Replace conf file if already exists", |
| 100 | + Value: false, |
| 101 | + }, |
| 102 | + &cli.StringFlag{ |
| 103 | + Name: "file", |
| 104 | + Usage: "Config file name", |
| 105 | + Value: ".commitlint.yaml", |
| 106 | + }, |
| 107 | + }, |
| 108 | + Action: func(ctx *cli.Context) error { |
| 109 | + isReplace := ctx.Bool("replace") |
| 110 | + fileName := ctx.String("file") |
| 111 | + err := configCreate(fileName, isReplace) |
| 112 | + if err != nil { |
| 113 | + if isConfExists(err) { |
| 114 | + fmt.Println("config create failed") |
| 115 | + fmt.Println("run with --replace to replace existing file") |
| 116 | + return nil |
| 117 | + } |
| 118 | + return err |
| 119 | + } |
| 120 | + fmt.Println("config file created") |
| 121 | + return nil |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + checkCmd := &cli.Command{ |
| 126 | + Name: "check", |
| 127 | + Usage: "Checks if given config is valid", |
| 128 | + Flags: []cli.Flag{ |
| 129 | + &cli.StringFlag{ |
| 130 | + Name: "config", |
| 131 | + Aliases: []string{"c", "conf"}, |
| 132 | + Usage: "config file `conf.yaml`", |
| 133 | + Required: true, |
| 134 | + }, |
| 135 | + }, |
| 136 | + Action: func(ctx *cli.Context) error { |
| 137 | + confFile := ctx.String("config") |
| 138 | + errs := configCheck(confFile) |
| 139 | + if len(errs) == 0 { |
| 140 | + fmt.Printf("%s config is valid\n", confFile) |
| 141 | + return nil |
| 142 | + } |
| 143 | + if len(errs) == 1 { |
| 144 | + return errs[0] |
| 145 | + } |
| 146 | + merr := multiError(errs) |
| 147 | + return &merr |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + return &cli.Command{ |
| 152 | + Name: "config", |
| 153 | + Usage: "Manage commitlint config", |
| 154 | + Subcommands: []*cli.Command{createCmd, checkCmd}, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func hookCmd() *cli.Command { |
| 159 | + replaceFlag := formReplaceFlag() |
| 160 | + |
| 161 | + createCmd := &cli.Command{ |
| 162 | + Name: "create", |
| 163 | + Usage: "Creates git hook files in current directory", |
| 164 | + Flags: []cli.Flag{replaceFlag}, |
| 165 | + Action: func(ctx *cli.Context) error { |
| 166 | + isReplace := ctx.Bool("replace") |
| 167 | + err := hookCreate(isReplace) |
| 168 | + if err != nil { |
| 169 | + if isHookExists(err) { |
| 170 | + fmt.Println("create failed. hook files already exists") |
| 171 | + fmt.Println("run with --replace to replace existing hook files") |
| 172 | + return nil |
| 173 | + } |
| 174 | + return err |
| 175 | + } |
| 176 | + fmt.Println("hooks created") |
| 177 | + return nil |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + return &cli.Command{ |
| 182 | + Name: "hook", |
| 183 | + Usage: "Manage commitlint git hooks", |
| 184 | + Subcommands: []*cli.Command{createCmd}, |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func formConfFlag() *cli.StringFlag { |
| 189 | + return &cli.StringFlag{ |
| 190 | + Name: "config", |
| 191 | + Aliases: []string{"c", "conf"}, |
| 192 | + Value: "", |
| 193 | + Usage: "Optional config file `conf.yaml` which will be passed to 'commitlint lint'. Check config precedence", |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func formReplaceFlag() *cli.BoolFlag { |
| 198 | + return &cli.BoolFlag{ |
| 199 | + Name: "replace", |
| 200 | + Usage: "Replace hook files if already exists", |
| 201 | + } |
| 202 | +} |
0 commit comments