-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (78 loc) · 2.44 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
package main
import (
"fmt"
"os"
"strings"
"github.com/TastefulElk/clai/command"
"github.com/TastefulElk/clai/logger"
"github.com/TastefulElk/clai/openai"
"github.com/integrii/flaggy"
)
var (
defaultModel string = "gpt-4o"
version string = "0.0.1"
)
func main() {
flaggy.SetName("GPT CLI")
flaggy.SetDescription("Ask the CLI for what you want do do then confirm the result to run it.")
flaggy.SetVersion(version)
var query string
flaggy.AddPositionalValue(&query, "query", 1, true, "The problem you want to solve.")
var model string
flaggy.String(&model, "m", "model", "The model you want to use. (Default gpt-4o)")
var apiToken string
flaggy.String(&apiToken, "t", "api-token", "The API token you want to use.")
var verbose bool
flaggy.Bool(&verbose, "v", "verbose", "Enable verbose logging.")
flaggy.Parse()
log := logger.GetLogger(verbose)
log.Println("parsed 'query': ", query)
log.Println("parsed 'model': ", model)
log.Println("parsed 'verbose': ", verbose)
if strings.TrimSpace(query) == "" {
log.Println("no query provided, exiting.")
flaggy.ShowHelpAndExit("'query' is required.")
}
if strings.TrimSpace(apiToken) == "" {
log.Println("no api token provided, checking environment var CLAI_OPENAI_TOKEN.")
apiToken = os.Getenv("CLAI_OPENAI_TOKEN")
if apiToken == "" {
log.Println("no api token found in environment either, exiting.")
flaggy.ShowHelpAndExit("No API token found in environment. Either pass it as a flag or set the CLAI_OPENAI_TOKEN environment variable.")
}
}
if strings.TrimSpace(model) == "" {
log.Println("no model provided, using default model ", defaultModel)
model = defaultModel
}
shell, _ := command.GetShell()
log.Println("detected shell: ", shell)
res, err := openai.Query(openai.QueryArg{
Query: query,
Model: model,
ApiToken: apiToken,
Shell: shell,
})
if err != nil {
fmt.Println("Error generating result:", err)
log.Fatal("Error querying:", err)
}
log.Println("suggested command(s): ", res)
const (
Green = "\033[32m"
Reset = "\033[0m"
)
fmt.Println("Here's the suggested command(s) to run:")
fmt.Println(Green + res + Reset)
fmt.Println("Do you want to run it directly? y/N")
// read user input
var input string
fmt.Scanln(&input)
if input != "y" {
log.Println("user chose not to run the command.")
return
}
log.Println("user chose to run the command - executing.")
command.RunCommand(res)
log.Println("command executed, exiting")
}