Skip to content

Commit b4de1ac

Browse files
committed
Move doc functionality to docs
1 parent b7fd09d commit b4de1ac

File tree

13 files changed

+132
-192
lines changed

13 files changed

+132
-192
lines changed

Justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ run *commands:
1414
go run main.go {{commands}}
1515

1616
cobra-docs:
17+
rm docs/*.md
1718
go run docs/gen_docs.go
1819

1920
install: build

cmd/cmd.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ import (
1717
// cmdCmd represents the cmd command
1818
var cmdCmd = &cobra.Command{
1919
Use: "cmd",
20-
Short: "Browses Bash",
21-
Long: `A longer description that spans multiple lines and likely contains examples
22-
and usage of using your command. For example:
23-
24-
Cobra is a CLI library for Go that empowers applications.
25-
This application is a tool to generate the needed files
26-
to quickly create a Cobra application.`,
20+
Short: "Have ChatGPT suggest a command to run next",
21+
Long: `Have ChatGPT suggest a command to run next. This command will use your shell history to suggest a command to run next.
22+
This command is only supported on MacOS and Linux using Bash or Zsh. Windows and other shells coming soon!`,
2723
Run: func(cmd *cobra.Command, args []string) {
2824
conf, err := config.Load()
2925
if err != nil || conf.APIKey == "" {
@@ -62,13 +58,5 @@ to quickly create a Cobra application.`,
6258
func init() {
6359
RootCmd.AddCommand(cmdCmd)
6460

65-
// Here you will define your flags and configuration settings.
66-
67-
// Cobra supports Persistent Flags which will work for this command
68-
// and all subcommands, e.g.:
69-
// cmdCmd.PersistentFlags().String("foo", "", "A help for foo")
70-
71-
// Cobra supports local flags which will only run when this command
72-
// is called directly, e.g.:
73-
// cmdCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
61+
cmdCmd.Flags().StringVarP(&chatPrompt, "question", "q", "", "The prompt to use for the chat session")
7462
}

cmd/doc.go

Lines changed: 0 additions & 98 deletions
This file was deleted.

cmd/docs.go

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ import (
1717
// docsCmd represents the docs command
1818
var docsCmd = &cobra.Command{
1919
Use: "docs",
20-
Short: "Document a repository of files",
20+
Short: "Document a repository of files or a single file",
2121
Long: `Document an entire repository of files. Specify the path to the repo as the first positional argument. This command will recursively
22-
search for files in the directory and document them.
22+
search for files in the directory and document them. If a single file is specified, it will be documented.
2323
`,
24-
Args: cobra.PositionalArgs(func(cmd *cobra.Command, args []string) error {
25-
if len(args) < 1 {
26-
return fmt.Errorf("requires a path to a repository")
27-
}
28-
return nil
29-
}),
24+
Args: cobra.ExactArgs(1),
3025
Run: func(cmd *cobra.Command, args []string) {
3126
repoPath = args[0]
3227

@@ -61,58 +56,119 @@ search for files in the directory and document them.
6156
os.Exit(1)
6257
}
6358

64-
repo, err := utils.GetRepo(repoPath, ignoreFilePath, ignoreGitignore)
59+
info, err := os.Stat(repoPath)
6560
if err != nil {
66-
log.Errorf("Error: %s", err)
61+
log.Errorf("Error getting file info: %s", err)
6762
os.Exit(1)
6863
}
6964

70-
for _, file := range repo.Files {
71-
var contents string
65+
if info.IsDir() {
66+
repo, err := utils.GetRepo(repoPath, ignoreFilePath, ignoreGitignore)
67+
if err != nil {
68+
log.Errorf("Error: %s", err)
69+
os.Exit(1)
70+
}
7271

73-
path := filepath.Join(repoPath, file.Path)
72+
for _, file := range repo.Files {
73+
var contents string
7474

75-
if outputFile != "" {
76-
fmt.Println("Documenting file", file.Path)
77-
}
75+
path := filepath.Join(repoPath, file.Path)
76+
77+
if outputFile != "" {
78+
fmt.Println("Documenting file", file.Path)
79+
}
80+
81+
if chatPrompt == "" {
82+
chatPrompt = "Write documentation for the following code snippet. The file name is" + file.Path + ":"
83+
}
84+
85+
fileContents, err := utils.LoadFile(path)
86+
if err != nil {
87+
log.Warnf("Error loading file %s: %s", path, err)
88+
continue
89+
}
90+
91+
if inlineMode || !markdownMode {
92+
contents, err = ai.SingleFile(path, fileContents, chatPrompt, conf.APIKey, conf.Model)
93+
} else {
94+
contents, err = ai.Markdown(path, fileContents, chatPrompt, conf.APIKey, conf.Model)
95+
}
96+
97+
if err != nil {
98+
log.Warnf("Error documenting file %s: %s", path, err)
99+
continue
100+
}
78101

102+
if outputFile != "" && markdownMode {
103+
// write the string to the output file
104+
// append if the file already exists
105+
file, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
106+
if err != nil {
107+
log.Errorf("Error: %s", err)
108+
os.Exit(1)
109+
}
110+
111+
_, err = file.WriteString(contents)
112+
if err != nil {
113+
log.Errorf("Error: %s", err)
114+
os.Exit(1)
115+
}
116+
117+
file.Close()
118+
} else if overwriteOriginal {
119+
// overwrite the original file
120+
// clear the contents of the file
121+
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
122+
if err != nil {
123+
log.Errorf("Error: %s", err)
124+
os.Exit(1)
125+
}
126+
127+
// write the new contents to the file
128+
_, err = file.WriteString(contents)
129+
if err != nil {
130+
log.Errorf("Error: %s", err)
131+
os.Exit(1)
132+
}
133+
134+
file.Close()
135+
} else {
136+
fmt.Println(contents)
137+
}
138+
}
139+
} else {
79140
if chatPrompt == "" {
80-
chatPrompt = "Write documentation for the following code snippet. The file name is" + file.Path + ":"
141+
chatPrompt = "Write documentation for the following code snippet:"
81142
}
82143

83-
fileContents, err := utils.LoadFile(path)
144+
filePath := repoPath
145+
146+
var contents string
147+
148+
fileContents, err := utils.LoadFile(filePath)
84149
if err != nil {
85-
log.Warnf("Error loading file %s: %s", path, err)
86-
continue
150+
log.Errorf("Error: %s", err)
151+
os.Exit(1)
87152
}
88153

89154
if inlineMode || !markdownMode {
90-
contents, err = ai.SingleFile(path, fileContents, chatPrompt, conf.APIKey, conf.Model)
155+
contents, err = ai.SingleFile(filePath, fileContents, chatPrompt, conf.APIKey, conf.Model)
91156
} else {
92-
contents, err = ai.Markdown(path, fileContents, chatPrompt, conf.APIKey, conf.Model)
157+
contents, err = ai.Markdown(filePath, fileContents, chatPrompt, conf.APIKey, conf.Model)
93158
}
94159

95160
if err != nil {
96-
log.Warnf("Error documenting file %s: %s", path, err)
97-
continue
161+
log.Errorf("Error: %s", err)
162+
os.Exit(1)
98163
}
99164

100-
if outputFile != "" && markdownMode {
165+
if outputFile != "" {
101166
// write the string to the output file
102-
// append if the file already exists
103-
file, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
167+
err = os.WriteFile(outputFile, []byte(contents), 0644)
104168
if err != nil {
105169
log.Errorf("Error: %s", err)
106170
os.Exit(1)
107171
}
108-
109-
_, err = file.WriteString(contents)
110-
if err != nil {
111-
log.Errorf("Error: %s", err)
112-
os.Exit(1)
113-
}
114-
115-
file.Close()
116172
} else if overwriteOriginal {
117173
// overwrite the original file
118174
// clear the contents of the file
@@ -128,13 +184,10 @@ search for files in the directory and document them.
128184
log.Errorf("Error: %s", err)
129185
os.Exit(1)
130186
}
131-
132-
file.Close()
133187
} else {
134188
fmt.Println(contents)
135189
}
136190
}
137-
138191
},
139192
}
140193

docs/otto.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ Code documentation made easy using GPT.
1616

1717
* [otto ask](otto_ask.md) - Ask a question about a file or repo
1818
* [otto chat](otto_chat.md) - Ask ChatGPT a question from the command line.
19-
* [otto doc](otto_doc.md) - Document a file
20-
* [otto docs](otto_docs.md) - Document a repository of files
19+
* [otto cmd](otto_cmd.md) - Have ChatGPT suggest a command to run next
20+
* [otto docs](otto_docs.md) - Document a repository of files or a single file
2121
* [otto login](otto_login.md) - Add an API key to your configuration
2222
* [otto prompt](otto_prompt.md) - Generates a ChatGPT prompt from a given Git repo
2323
* [otto setModel](otto_setModel.md) - Set the model to use for documentation
2424

25-
###### Auto generated by spf13/cobra on 18-Apr-2023
25+
###### Auto generated by spf13/cobra on 21-Apr-2023

docs/otto_ask.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ otto ask [flags]
2424

2525
* [otto](otto.md) - Document your code with ease
2626

27-
###### Auto generated by spf13/cobra on 18-Apr-2023
27+
###### Auto generated by spf13/cobra on 21-Apr-2023

docs/otto_chat.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ otto chat [flags]
2424

2525
* [otto](otto.md) - Document your code with ease
2626

27-
###### Auto generated by spf13/cobra on 18-Apr-2023
27+
###### Auto generated by spf13/cobra on 21-Apr-2023

docs/otto_cmd.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## otto cmd
2+
3+
Have ChatGPT suggest a command to run next
4+
5+
### Synopsis
6+
7+
Have ChatGPT suggest a command to run next. This command will use your shell history to suggest a command to run next.
8+
This command is only supported on MacOS and Linux using Bash or Zsh. Windows and other shells coming soon!
9+
10+
```
11+
otto cmd [flags]
12+
```
13+
14+
### Options
15+
16+
```
17+
-h, --help help for cmd
18+
-q, --question string The prompt to use for the chat session
19+
```
20+
21+
### SEE ALSO
22+
23+
* [otto](otto.md) - Document your code with ease
24+
25+
###### Auto generated by spf13/cobra on 21-Apr-2023

0 commit comments

Comments
 (0)