-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
83 lines (72 loc) · 1.96 KB
/
Copy pathinit.go
File metadata and controls
83 lines (72 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cmd
import (
_ "embed"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/zcube/commit-checker/internal/i18n"
)
var (
initForce bool
initLang string
)
// defaultConfigTemplate: 기본 설정 YAML 템플릿 (locale 자리에 fmt 의 %s 사용).
//
//go:embed templates/config.yml.tmpl
var defaultConfigTemplate string
// localeToLanguageName: 로케일 코드를 required_language 값으로 변환.
var localeToLanguageName = map[string]string{
"ko": "korean",
"en": "english",
"ja": "japanese",
"zh": "chinese",
}
func getDefaultConfig(lang string) string {
if lang == "" {
lang = i18n.DetectLocale()
}
lang = strings.ToLower(lang)
locale := "ko"
if _, ok := localeToLanguageName[lang]; ok {
locale = lang
}
return fmt.Sprintf(defaultConfigTemplate, locale, locale, locale, localizedExample(locale), locale)
}
// localizedExample: 해당 로케일의 대표 타입 별칭 예시를 반환.
func localizedExample(locale string) string {
switch locale {
case "ko":
return "기능"
case "ja":
return "機能"
case "zh":
return "功能"
default:
return "feat"
}
}
var initCmd = &cobra.Command{
Use: "init",
RunE: func(cmd *cobra.Command, args []string) error {
target := configFile
if !initForce {
if _, err := os.Stat(target); err == nil {
return fmt.Errorf("%s", i18n.T("init.already_exists", map[string]any{"Path": target}))
}
}
content := getDefaultConfig(initLang)
if err := os.WriteFile(target, []byte(content), 0644); err != nil {
return fmt.Errorf("%s", i18n.T("init.fail_write", map[string]any{"Error": err.Error()}))
}
fmt.Println(i18n.T("init.created", map[string]any{"Path": target}))
return nil
},
}
func init() {
initCmd.Short = i18n.T("cmd.init.short", nil)
initCmd.Long = i18n.T("cmd.init.long", nil)
initCmd.Flags().BoolVar(&initForce, "force", false, i18n.T("flag.init_force", nil))
initCmd.Flags().StringVar(&initLang, "lang", "", i18n.T("flag.init_lang", nil))
rootCmd.AddCommand(initCmd)
}