-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (95 loc) 路 2.36 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/viper"
)
type env struct {
DEBUG bool
DRY bool
USE_CONVENTIONAL bool
}
const VERSION = "1.1"
var environment env
func setupLogging() *os.File {
if environment.DEBUG {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
return f
}
return nil
}
func createConfigFile() (bool, error) {
home, err := os.UserHomeDir()
if err != nil {
return false, err
}
dirPath := filepath.Join(home, ".config", "autocomm")
mkdirErr := os.MkdirAll(dirPath, os.ModePerm)
if mkdirErr != nil {
return false, mkdirErr
}
filePath := filepath.Join(dirPath, "autocomm.toml")
f, fileMakeErr := os.Create(filePath)
defer f.Close()
if err != nil {
return false, fileMakeErr
}
return true, nil
}
func setupConfig() {
viper.SetConfigName("autocomm.toml")
viper.SetConfigType("toml")
home, homeDirErr := os.UserHomeDir()
if homeDirErr != nil {
fmt.Println("An unexpected error occurred when getting your home directory 馃槙")
}
viper.AddConfigPath(filepath.Join(home, ".config", "autocomm"))
viper.AddConfigPath(home)
err := viper.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
if ok, createErr := createConfigFile(); !ok {
fmt.Println("An unexpected error occurred when creating the config file 馃槙")
logi(createErr.Error())
os.Exit(1)
}
logi("Config file created at $HOME/.config/autocomm/.autocomm.toml")
}
} else {
logi("Config file found!")
}
viper.SetDefault("apiKey", "")
viper.SetDefault("aiModel", "text-davinci-003")
}
func main() {
if !userInGitRepo() {
fmt.Println("This ain't a git repo 馃え")
os.Exit(1)
}
environment = env{
DEBUG: false,
DRY: false,
USE_CONVENTIONAL: false,
}
flag.BoolVar(&environment.DEBUG, "debug", false, "enable debug logging")
flag.BoolVar(&environment.DRY, "dry", false, "dry run (doesn't make API calls)")
flag.BoolVar(&environment.USE_CONVENTIONAL, "conventional", false, "use conventional commits")
flag.Parse()
f := setupLogging()
if f != nil {
defer f.Close()
}
setupConfig()
prog := tea.NewProgram(InitalModel())
if _, err := prog.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}