mirror of
https://github.com/Direct-Dev-Ru/go-lcg.git
synced 2025-11-16 09:39:56 +00:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d11017d792 | ||
|
|
1c4113d0c2 | ||
|
|
00b2ea6614 | ||
|
|
9538b0fed5 | ||
|
|
5141cb69a3 | ||
|
|
ae90ef6cfb | ||
|
|
7f81b1942b | ||
|
|
dafcaaff0f | ||
|
|
fbb68d2a28 | ||
|
|
2d6fef23aa | ||
|
|
432bfc61db | ||
|
|
0e50c8ec04 | ||
|
|
148e1d9420 | ||
|
|
952eee1a29 | ||
|
|
c2619a2864 | ||
|
|
b1166a724d | ||
|
|
c6b1474117 | ||
|
|
b04f7016b8 | ||
|
|
4f52b5bbad | ||
|
|
dce4360043 |
31
.github/workflows/release.yaml
vendored
Normal file
31
.github/workflows/release.yaml
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: basebuild
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
goreleaser:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '>=1.18.0'
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: go test ./...
|
||||||
|
|
||||||
|
- name: Run GoReleaser
|
||||||
|
uses: goreleaser/goreleaser-action@v3
|
||||||
|
with:
|
||||||
|
distribution: goreleaser
|
||||||
|
version: latest
|
||||||
|
args: release --rm-dist
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.TOKEN }}
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,4 +8,4 @@
|
|||||||
go.work
|
go.work
|
||||||
*.log
|
*.log
|
||||||
lcg
|
lcg
|
||||||
|
dist/
|
||||||
|
|||||||
33
.goreleaser.yaml
Normal file
33
.goreleaser.yaml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
archives:
|
||||||
|
- format: tar.gz
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- binary: lcg
|
||||||
|
env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
goarch:
|
||||||
|
- amd64
|
||||||
|
- arm64
|
||||||
|
- arm
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- darwin
|
||||||
|
|
||||||
|
changelog:
|
||||||
|
filters:
|
||||||
|
exclude:
|
||||||
|
- '^docs:'
|
||||||
|
- '^test:'
|
||||||
|
sort: asc
|
||||||
|
|
||||||
|
checksum:
|
||||||
|
name_template: 'checksums.txt'
|
||||||
|
|
||||||
|
release:
|
||||||
|
draft: true
|
||||||
|
|
||||||
|
snapshot:
|
||||||
|
name_template: "{{ incpatch .Version }}-next"
|
||||||
|
|
||||||
|
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
||||||
|
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 asrul10
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
23
README.md
23
README.md
@@ -11,23 +11,28 @@ Build from source
|
|||||||
> ln -s ~/.linux-command-gpt/lcg ~/.local/bin
|
> ln -s ~/.linux-command-gpt/lcg ~/.local/bin
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Or you can [download lcg executable file](https://github.com/asrul10/linux-command-gpt/releases)
|
||||||
|
|
||||||
### Example Usage
|
### Example Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
> lcg I want to extract file linux-command-gpt.tar.gz
|
> lcg I want to extract linux-command-gpt.tar.gz file
|
||||||
Completed in 0.92 seconds
|
Completed in 0.92 seconds
|
||||||
┌────────────────────────────────────┐
|
|
||||||
│ tar -xvzf linux-command-gpt.tar.gz │
|
tar -xvzf linux-command-gpt.tar.gz
|
||||||
└────────────────────────────────────┘
|
|
||||||
Are you sure you want to execute the command? (Y/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
|
### Options
|
||||||
```bash
|
```bash
|
||||||
> lcg [options]
|
> lcg [options]
|
||||||
|
|
||||||
--help output usage information
|
--help -h output usage information
|
||||||
--version output the version number
|
--version -v output the version number
|
||||||
--update-key update the API key
|
--file -f read command from file
|
||||||
--delete-key delete the API key
|
--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
|
module github.com/asrul/linux-command-gpt
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/atotto/clipboard v0.1.4
|
||||||
|
|||||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -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"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -88,7 +88,7 @@ func (gpt3 *Gpt3) loadApiKey() bool {
|
|||||||
if _, err := os.Stat(apiKeyFile); os.IsNotExist(err) {
|
if _, err := os.Stat(apiKeyFile); os.IsNotExist(err) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
apiKey, err := ioutil.ReadFile(apiKeyFile)
|
apiKey, err := os.ReadFile(apiKeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -144,7 +144,7 @@ func (gpt3 *Gpt3) Completions(ask string) string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
req.Body = ioutil.NopCloser(bytes.NewBuffer(payloadJson))
|
req.Body = io.NopCloser(bytes.NewBuffer(payloadJson))
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
@@ -153,7 +153,7 @@ func (gpt3 *Gpt3) Completions(ask string) string {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
124
main.go
124
main.go
@@ -4,12 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/user"
|
"os/user"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/asrul/linux-command-gpt/gpt"
|
"github.com/asrul/linux-command-gpt/gpt"
|
||||||
|
"github.com/asrul/linux-command-gpt/reader"
|
||||||
|
"github.com/atotto/clipboard"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -26,14 +27,17 @@ const (
|
|||||||
|
|
||||||
Usage: lcg [options]
|
Usage: lcg [options]
|
||||||
|
|
||||||
--help output usage information
|
--help -h output usage information
|
||||||
--version output the version number
|
--version -v output the version number
|
||||||
--update-key update the API key
|
--file -f read command from file
|
||||||
--delete-key delete the API key
|
--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.0"
|
VERSION = "0.2.1"
|
||||||
CMD_HELP = 100
|
CMD_HELP = 100
|
||||||
CMD_VERSION = 101
|
CMD_VERSION = 101
|
||||||
CMD_UPDATE = 102
|
CMD_UPDATE = 102
|
||||||
@@ -57,6 +61,37 @@ func handleCommand(cmd string) int {
|
|||||||
return CMD_COMPLETION
|
return CMD_COMPLETION
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCommand(gpt3 gpt.Gpt3, cmd string) (string, float64) {
|
||||||
|
gpt3.InitKey()
|
||||||
|
s := time.Now()
|
||||||
|
done := make(chan bool)
|
||||||
|
go func() {
|
||||||
|
loadingChars := []rune{'-', '\\', '|', '/'}
|
||||||
|
i := 0
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
fmt.Printf("\r")
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
fmt.Printf("\rLoading %c", loadingChars[i])
|
||||||
|
i = (i + 1) % len(loadingChars)
|
||||||
|
time.Sleep(30 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
r := gpt3.Completions(cmd)
|
||||||
|
done <- true
|
||||||
|
elapsed := time.Since(s).Seconds()
|
||||||
|
elapsed = math.Round(elapsed*100) / 100
|
||||||
|
|
||||||
|
if r == "" {
|
||||||
|
return "", elapsed
|
||||||
|
}
|
||||||
|
return r, elapsed
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
currentUser, err := user.Current()
|
currentUser, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,9 +100,24 @@ func main() {
|
|||||||
|
|
||||||
args := os.Args
|
args := os.Args
|
||||||
cmd := ""
|
cmd := ""
|
||||||
|
file := ""
|
||||||
if len(args) > 1 {
|
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)
|
h := handleCommand(cmd)
|
||||||
|
|
||||||
if h == CMD_HELP {
|
if h == CMD_HELP {
|
||||||
@@ -98,51 +148,31 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s := time.Now()
|
c := "R"
|
||||||
done := make(chan bool)
|
r := ""
|
||||||
go func() {
|
elapsed := 0.0
|
||||||
loadingChars := []rune{'-', '\\', '|', '/'}
|
for c == "R" || c == "r" {
|
||||||
i := 0
|
r, elapsed = getCommand(gpt3, cmd)
|
||||||
for {
|
c = "N"
|
||||||
select {
|
fmt.Printf("Completed in %v seconds\n\n", elapsed)
|
||||||
case <-done:
|
fmt.Println(r)
|
||||||
fmt.Printf("\r")
|
fmt.Print("\nDo you want to (c)opy, (r)egenerate, or take (N)o action on the command? (c/r/N): ")
|
||||||
return
|
fmt.Scanln(&c)
|
||||||
default:
|
|
||||||
fmt.Printf("\rLoading %c", loadingChars[i])
|
// No action
|
||||||
i = (i + 1) % len(loadingChars)
|
if c == "N" || c == "n" {
|
||||||
time.Sleep(30 * time.Millisecond)
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
gpt3.InitKey()
|
|
||||||
r := gpt3.Completions(cmd)
|
|
||||||
done <- true
|
|
||||||
if r == "" {
|
if r == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c := "Y"
|
// Copy to clipboard
|
||||||
elapsed := time.Since(s).Seconds()
|
if c == "C" || c == "c" {
|
||||||
elapsed = math.Round(elapsed*100) / 100
|
clipboard.WriteAll(r)
|
||||||
fmt.Printf("Completed in %v seconds\n", elapsed)
|
fmt.Println("\033[33mCopied to clipboard")
|
||||||
fmt.Printf("┌%s┐\n", strings.Repeat("─", len(r)+2))
|
|
||||||
fmt.Printf("│ %s │\n", r)
|
|
||||||
fmt.Printf("└%s┘\n", strings.Repeat("─", len(r)+2))
|
|
||||||
fmt.Print("Are you sure you want to execute the command? (Y/n): ")
|
|
||||||
fmt.Scanln(&c)
|
|
||||||
if c != "Y" && c != "y" {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmsplit := strings.Split(r, " ")
|
|
||||||
cm := exec.Command(cmsplit[0], cmsplit[1:]...)
|
|
||||||
out, err := cm.Output()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
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