mirror of
https://github.com/Direct-Dev-Ru/go-lcg.git
synced 2025-11-16 17:49:55 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d11017d792 | ||
|
|
1c4113d0c2 | ||
|
|
00b2ea6614 | ||
|
|
9538b0fed5 | ||
|
|
5141cb69a3 |
13
README.md
13
README.md
@@ -21,15 +21,18 @@ Completed in 0.92 seconds
|
||||
|
||||
tar -xvzf linux-command-gpt.tar.gz
|
||||
|
||||
Do you want to (e)xecute, (r)egenerate, or take (N)o action on the command? (e/r/N):
|
||||
Do you want to (c)opy, (r)egenerate, or take (N)o action on the command? (c/r/N):
|
||||
```
|
||||
|
||||
To use the "copy to clipboard" feature, you need to install either the `xclip` or `xsel` package.
|
||||
|
||||
### Options
|
||||
```bash
|
||||
> lcg [options]
|
||||
|
||||
--help output usage information
|
||||
--version output the version number
|
||||
--update-key update the API key
|
||||
--delete-key delete the API key
|
||||
--help -h output usage information
|
||||
--version -v output the version number
|
||||
--file -f read command from file
|
||||
--update-key -u update the API key
|
||||
--delete-key -d delete the API key
|
||||
```
|
||||
|
||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
||||
module github.com/asrul/linux-command-gpt
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/atotto/clipboard v0.1.4
|
||||
|
||||
2
go.sum
2
go.sum
@@ -0,0 +1,2 @@
|
||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -88,7 +88,7 @@ func (gpt3 *Gpt3) loadApiKey() bool {
|
||||
if _, err := os.Stat(apiKeyFile); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
apiKey, err := ioutil.ReadFile(apiKeyFile)
|
||||
apiKey, err := os.ReadFile(apiKeyFile)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (gpt3 *Gpt3) Completions(ask string) string {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
req.Body = ioutil.NopCloser(bytes.NewBuffer(payloadJson))
|
||||
req.Body = io.NopCloser(bytes.NewBuffer(payloadJson))
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -153,7 +153,7 @@ func (gpt3 *Gpt3) Completions(ask string) string {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
48
main.go
48
main.go
@@ -4,12 +4,13 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/asrul/linux-command-gpt/gpt"
|
||||
"github.com/asrul/linux-command-gpt/reader"
|
||||
"github.com/atotto/clipboard"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -26,15 +27,17 @@ const (
|
||||
|
||||
Usage: lcg [options]
|
||||
|
||||
--help output usage information
|
||||
--version output the version number
|
||||
--update-key update the API key
|
||||
--delete-key delete the API key
|
||||
--help -h output usage information
|
||||
--version -v output the version number
|
||||
--file -f read command from file
|
||||
--update-key -u update the API key
|
||||
--delete-key -d delete the API key
|
||||
|
||||
Example Usage: lcg I want to extract linux-command-gpt.tar.gz file
|
||||
Example Usage: lcg --file /path/to/file.json I want to print object questions with jq
|
||||
`
|
||||
|
||||
VERSION = "0.1.3"
|
||||
VERSION = "0.2.1"
|
||||
CMD_HELP = 100
|
||||
CMD_VERSION = 101
|
||||
CMD_UPDATE = 102
|
||||
@@ -97,9 +100,24 @@ func main() {
|
||||
|
||||
args := os.Args
|
||||
cmd := ""
|
||||
file := ""
|
||||
if len(args) > 1 {
|
||||
cmd = strings.Join(args[1:], " ")
|
||||
start := 1
|
||||
if args[1] == "--file" || args[1] == "-f" {
|
||||
file = args[2]
|
||||
start = 3
|
||||
}
|
||||
cmd = strings.Join(args[start:], " ")
|
||||
}
|
||||
|
||||
if file != "" {
|
||||
err := reader.FileToPrompt(&cmd, file)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
h := handleCommand(cmd)
|
||||
|
||||
if h == CMD_HELP {
|
||||
@@ -138,8 +156,10 @@ func main() {
|
||||
c = "N"
|
||||
fmt.Printf("Completed in %v seconds\n\n", elapsed)
|
||||
fmt.Println(r)
|
||||
fmt.Print("\nDo you want to (e)xecute, (r)egenerate, or take (N)o action on the command? (e/r/N): ")
|
||||
fmt.Print("\nDo you want to (c)opy, (r)egenerate, or take (N)o action on the command? (c/r/N): ")
|
||||
fmt.Scanln(&c)
|
||||
|
||||
// No action
|
||||
if c == "N" || c == "n" {
|
||||
return
|
||||
}
|
||||
@@ -148,13 +168,11 @@ func main() {
|
||||
if r == "" {
|
||||
return
|
||||
}
|
||||
cmsplit := strings.Split(r, " ")
|
||||
cm := exec.Command(cmsplit[0], cmsplit[1:]...)
|
||||
out, err := cm.Output()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
|
||||
// Copy to clipboard
|
||||
if c == "C" || c == "c" {
|
||||
clipboard.WriteAll(r)
|
||||
fmt.Println("\033[33mCopied to clipboard")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
}
|
||||
|
||||
24
reader/file.go
Normal file
24
reader/file.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package reader
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
)
|
||||
|
||||
func FileToPrompt(cmd *string, filePath string) error {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
reader := bufio.NewReader(f)
|
||||
*cmd = *cmd + "\nFile path: " + filePath + "\n"
|
||||
for {
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
*cmd = *cmd + "\n" + line
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user