-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathroot.go
107 lines (101 loc) · 3.19 KB
/
root.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
package cmd
import (
"fmt"
"os"
"github.com/chand1012/git2gpt/prompt"
"github.com/spf13/cobra"
)
var repoPath string
var preambleFile string
var outputFile string
var estimateTokens bool
var ignoreFilePath string
var ignoreGitignore bool
var outputJSON bool
var debug bool
var scrubComments bool
var rootCmd = &cobra.Command{
Use: "git2gpt [flags] /path/to/git/repository",
Short: "git2gpt is a utility to convert a Git repository to a text file for input into GPT-4",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
repoPath = args[0]
ignoreList := prompt.GenerateIgnoreList(repoPath, ignoreFilePath, !ignoreGitignore)
repo, err := prompt.ProcessGitRepo(repoPath, ignoreList)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
if outputJSON {
output, err := prompt.MarshalRepo(repo, scrubComments)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
if outputFile != "" {
// if output file exists, throw error
if _, err := os.Stat(outputFile); err == nil {
fmt.Printf("Error: output file %s already exists\n", outputFile)
os.Exit(1)
}
err = os.WriteFile(outputFile, []byte(output), 0644)
if err != nil {
fmt.Printf("Error: could not write to output file %s\n", outputFile)
os.Exit(1)
}
} else {
if !debug {
fmt.Println(string(output))
}
}
return
}
output, err := prompt.OutputGitRepo(repo, preambleFile, scrubComments)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
if outputFile != "" {
// if output file exists, throw error
if _, err := os.Stat(outputFile); err == nil {
fmt.Printf("Error: output file %s already exists\n", outputFile)
os.Exit(1)
}
err = os.WriteFile(outputFile, []byte(output), 0644)
if err != nil {
fmt.Printf("Error: could not write to output file %s\n", outputFile)
os.Exit(1)
}
} else {
if !debug {
fmt.Println(output)
}
}
if estimateTokens {
fmt.Printf("Estimated number of tokens: %d\n", prompt.EstimateTokens(output))
}
},
}
func init() {
rootCmd.Flags().StringVarP(&preambleFile, "preamble", "p", "", "path to preamble text file")
// output to file flag. Should be a string
rootCmd.Flags().StringVarP(&outputFile, "output", "o", "", "path to output file")
// estimate tokens. Should be a bool
rootCmd.Flags().BoolVarP(&estimateTokens, "estimate", "e", false, "estimate the number of tokens in the output")
// ignore file path. Should be a string
rootCmd.Flags().StringVarP(&ignoreFilePath, "ignore", "i", "", "path to .gptignore file")
// ignore gitignore. Should be a bool
rootCmd.Flags().BoolVarP(&ignoreGitignore, "ignore-gitignore", "g", false, "ignore .gitignore file")
// output JSON. Should be a bool
rootCmd.Flags().BoolVarP(&outputJSON, "json", "j", false, "output JSON")
// debug. Should be a bool
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "debug mode. Do not output to standard output")
// scrub comments. Should be a bool
rootCmd.Flags().BoolVarP(&scrubComments, "scrub-comments", "s", false, "scrub comments from the output. Decreases token count")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}