github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/cmd/cmd_commit_msg.go (about)

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/artisanhe/tools/project/commit_msg"
    10  )
    11  
    12  var cmdCommitMsgFlagInit bool
    13  
    14  var cmdCommitMsg = &cobra.Command{
    15  	Use:   "commit_msg",
    16  	Short: "commit_msg hooks",
    17  	Run: func(cmd *cobra.Command, args []string) {
    18  		if cmdCommitMsgFlagInit {
    19  			ioutil.WriteFile(".git/hooks/commit-msg", []byte(`#!/bin/sh
    20  tools commit_msg $1
    21  `), os.ModePerm)
    22  		} else {
    23  			data, _ := ioutil.ReadFile(args[0])
    24  			err := commit_msg.CheckCommit(string(data))
    25  			if err != nil {
    26  				panic(err)
    27  			}
    28  		}
    29  	},
    30  }
    31  
    32  func init() {
    33  	cmdCommitMsg.Flags().
    34  		BoolVarP(&cmdCommitMsgFlagInit, "init", "", false, "init commit msg hook")
    35  
    36  	cmdRoot.AddCommand(cmdCommitMsg)
    37  }