-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (90 loc) · 1.65 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
package main
import (
"flag"
"fmt"
"os"
"github.com/dshills/termai/config"
)
func main() {
opts := getFlags()
// Create defaults
if opts.InitConfig {
if err := config.InitializeDefaults(); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
// load config
conf, err := config.LoadDefault()
if err != nil {
ShowUsageAndExit(err.Error(), 1)
}
// Create the ai
aimgr, err := newAI(conf.Generators, conf, opts)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Print the conversation
if opts.PrintConv {
aimgr.printConv()
os.Exit(0)
}
// Print the default model
if opts.PrintDefModel {
aimgr.printDefaultModel()
os.Exit(0)
}
// List: List Models
if opts.ListModels {
aimgr.listModels()
os.Exit(0)
}
// Help: Print usage
if opts.Help {
ShowUsageAndExit("", 0)
}
// --- Query ---
query := opts.Query
if query == "" {
ShowUsageAndExit("You didn't ask anything", 1)
}
if err := aimgr.createPrompt(query); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Prompt: Print the prompt without running
if opts.PrintPrompt {
aimgr.printPrompt()
os.Exit(0)
}
output, err := aimgr.usePrompt()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if opts.Out != "" {
file, err := os.Create(opts.Out)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
_, _ = file.WriteString(output)
} else {
fmt.Println(output)
}
if err := aimgr.saveConv(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func ShowUsageAndExit(msg string, exitcode int) {
if msg != "" {
fmt.Println(msg)
}
fmt.Println("Usage: termai [options] [query]")
flag.PrintDefaults()
os.Exit(exitcode)
}