-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (74 loc) · 2.02 KB
/
main.go
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
84
85
86
87
88
package main
import (
_ "embed"
"fmt"
"os"
"strings"
"github.com/zaidfadhil/kemit/cli"
"github.com/zaidfadhil/kemit/config"
"github.com/zaidfadhil/kemit/engine"
"github.com/zaidfadhil/kemit/git"
)
//go:embed VERSION
var version string
func main() {
cmd := cli.New()
cfg := &config.Config{}
if err := cfg.Load(); err != nil {
end(err)
}
// config
var configCmd *cli.Command
configCmd = cmd.AddCommand("config", "Set the configuration", func(_ []string) {
cfg.Provider = configCmd.Flags.Lookup("provider").Value.String()
cfg.OllamaHost = configCmd.Flags.Lookup("ollama_host").Value.String()
cfg.OllamaModel = configCmd.Flags.Lookup("ollama_model").Value.String()
cfg.CommitStyle = configCmd.Flags.Lookup("commit_style").Value.String()
if err := cfg.Save(); err != nil {
end(err)
}
})
configCmd.Flags.String("provider", cfg.Provider, "LLM models provider. ex: ollama")
configCmd.Flags.String("ollama_host", cfg.OllamaHost, "Ollama host. ex: localhost:11434")
configCmd.Flags.String("ollama_model", cfg.OllamaModel, "Ollama model. ex: llama3.1")
configCmd.Flags.String("commit_style", cfg.CommitStyle, "Commit style. ex: conventional-commit")
// version
cmd.AddCommand("version", "Show the version", func(_ []string) {
fmt.Println("version:", strings.TrimSpace(version))
})
// help
cmd.AddCommand("help", "Help about any command", func(_ []string) {
cmd.PrintHelp()
})
cmd.SetDefaultCommand(cmd.AddCommand("", "", func(_ []string) {
if err := cfg.Validate(); err != nil {
end(err)
}
run(cfg)
}))
if err := cmd.Execute(); err != nil {
end(err)
}
}
func run(cfg *config.Config) {
diff, err := git.Diff()
if err != nil {
end(err)
}
if diff == "" {
fmt.Println("Nothing to commit.")
} else {
// TODO: move this to the enigne pkg
ollama := engine.NewOllama(cfg.OllamaHost, cfg.OllamaModel)
message, err := ollama.GetCommitMessage(diff, cfg.CommitStyle)
if err != nil {
end(err)
} else {
fmt.Println(message)
}
}
}
func end(err error) {
fmt.Println("error:", err)
os.Exit(1)
}