|  | 
| 1 | 1 | /* | 
| 2 | 2 |  * @Author: robert zhang <robertzhangwenjie@gmail.com> | 
| 3 | 3 |  * @Date: 2022-08-04 17:12:31 | 
| 4 |  | - * @LastEditTime: 2022-08-10 16:55:52 | 
|  | 4 | + * @LastEditTime: 2022-08-29 11:51:15 | 
| 5 | 5 |  * @LastEditors: robert zhang | 
| 6 | 6 |  * @Description: | 
| 7 | 7 |  */ | 
| 8 | 8 | package commit | 
| 9 | 9 | 
 | 
|  | 10 | +import ( | 
|  | 11 | +	"encoding/json" | 
|  | 12 | +	"fmt" | 
|  | 13 | +	"os" | 
|  | 14 | +	"path/filepath" | 
|  | 15 | + | 
|  | 16 | +	"github.com/robertzhangwenjie/commitizen/git" | 
|  | 17 | +) | 
|  | 18 | + | 
|  | 19 | +var configName = ".git-czrc" | 
|  | 20 | + | 
|  | 21 | +type messageConfig struct { | 
|  | 22 | +	Template string | 
|  | 23 | +	Items    []*Form | 
|  | 24 | +} | 
|  | 25 | + | 
|  | 26 | +func loadConfig() (*messageConfig, error) { | 
|  | 27 | +	var msgConfig = new(messageConfig) | 
|  | 28 | + | 
|  | 29 | +	// 如果当前git仓库根目录下拥有configFile,则优先使用它作为配置文件 | 
|  | 30 | +	// loadConfig | 
|  | 31 | +	gitRoot, _ := git.GetCurrentRepositoryRoot() | 
|  | 32 | +	path, config, err := getConfigIn(gitRoot) | 
|  | 33 | +	if err == nil { | 
|  | 34 | +		err := json.Unmarshal(config, &msgConfig) | 
|  | 35 | +		if err != nil { | 
|  | 36 | +			return msgConfig, fmt.Errorf("config %s is not valid: %v", path, err) | 
|  | 37 | +		} | 
|  | 38 | +		return msgConfig, nil | 
|  | 39 | +	} | 
|  | 40 | + | 
|  | 41 | +	// 如果git根目录下没有,家目录下有配置文件,则使用家目录下的配置文件 | 
|  | 42 | +	homePath, err := os.UserHomeDir() | 
|  | 43 | +	if err == nil { | 
|  | 44 | +		path, config, err := getConfigIn(homePath) | 
|  | 45 | +		if err == nil { | 
|  | 46 | +			err := json.Unmarshal(config, &msgConfig) | 
|  | 47 | +			if err != nil { | 
|  | 48 | +				return msgConfig, fmt.Errorf("config %s is not valid: %v", path, err) | 
|  | 49 | +			} | 
|  | 50 | +			return msgConfig, nil | 
|  | 51 | +		} | 
|  | 52 | +	} | 
|  | 53 | + | 
|  | 54 | +	// 缺省配置 | 
|  | 55 | +	err = json.Unmarshal([]byte(defaultConfig), &msgConfig) | 
|  | 56 | +	return msgConfig, err | 
|  | 57 | +} | 
|  | 58 | + | 
|  | 59 | +// getConfigIn return the config contenct if config exists | 
|  | 60 | +func getConfigIn(path string) (string, []byte, error) { | 
|  | 61 | +	configPath := filepath.Join(path, configName) | 
|  | 62 | +	config, err := os.ReadFile(configPath) | 
|  | 63 | +	// 如果家目录下有配置文件,且git仓库根目录下没有,则使用家目录下的配置文件 | 
|  | 64 | +	if err != nil { | 
|  | 65 | +		return configPath, nil, err | 
|  | 66 | +	} | 
|  | 67 | +	return configPath, config, nil | 
|  | 68 | +} | 
|  | 69 | + | 
| 10 | 70 | var selectQuestionTemplate = ` | 
| 11 | 71 | {{- define "option"}} | 
| 12 | 72 |     {{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }} {{else}}{{color "default"}}  {{end}} | 
|  | 
0 commit comments