├── .github └── workflows │ └── release.yaml ├── .gitignore ├── .goreleaser.yaml ├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── gpt ├── gpt.go └── gpt_test.go ├── main.go ├── main_test.go └── reader └── file.go /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: basebuild 2 | 3 | on: 4 | pull_request: 5 | push: 6 | 7 | jobs: 8 | goreleaser: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v3 13 | with: 14 | fetch-depth: 0 15 | 16 | - name: Set up Go 17 | uses: actions/setup-go@v3 18 | with: 19 | go-version: '>=1.18.0' 20 | 21 | - name: Run tests 22 | run: go test ./... 23 | 24 | - name: Run GoReleaser 25 | uses: goreleaser/goreleaser-action@v6 26 | with: 27 | distribution: goreleaser 28 | version: latest 29 | args: release --clean 30 | env: 31 | GITHUB_TOKEN: ${{ secrets.TOKEN }} 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.exe~ 3 | *.dll 4 | *.so 5 | *.dylib 6 | *.test 7 | *.out 8 | go.work 9 | *.log 10 | lcg 11 | dist/ 12 | -------------------------------------------------------------------------------- /.goreleaser.yaml: -------------------------------------------------------------------------------- 1 | archives: 2 | - format: tar.gz 3 | 4 | builds: 5 | - binary: lcg 6 | env: 7 | - CGO_ENABLED=0 8 | goarch: 9 | - amd64 10 | - arm64 11 | - arm 12 | goos: 13 | - linux 14 | - darwin 15 | 16 | changelog: 17 | filters: 18 | exclude: 19 | - '^docs:' 20 | - '^test:' 21 | sort: asc 22 | 23 | checksum: 24 | name_template: 'checksums.txt' 25 | 26 | release: 27 | draft: true 28 | 29 | snapshot: 30 | name_template: "{{ incpatch .Version }}-next" 31 | 32 | # yaml-language-server: $schema=https://goreleaser.com/static/schema.json 33 | # vim: set ts=2 sw=2 tw=0 fo=cnqoj 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 asrul10 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Linux Command GPT (lcg) 2 | Get Linux commands in natural language with the power of ChatGPT. 3 | 4 | ### Installation 5 | Build from source 6 | ```bash 7 | > git clone --depth 1 https://github.com/asrul10/linux-command-gpt.git ~/.linux-command-gpt 8 | > cd ~/.linux-command-gpt 9 | > go build -o lcg 10 | # Add to your environment $PATH 11 | > ln -s ~/.linux-command-gpt/lcg ~/.local/bin 12 | ``` 13 | 14 | Or you can [download lcg executable file](https://github.com/asrul10/linux-command-gpt/releases) 15 | 16 | ### Example Usage 17 | 18 | ```bash 19 | > lcg I want to extract linux-command-gpt.tar.gz file 20 | Completed in 0.92 seconds 21 | 22 | tar -xvzf linux-command-gpt.tar.gz 23 | 24 | Do you want to (c)opy, (r)egenerate, or take (N)o action on the command? (c/r/N): 25 | ``` 26 | 27 | To use the "copy to clipboard" feature, you need to install either the `xclip` or `xsel` package. 28 | 29 | ### Options 30 | ```bash 31 | > lcg [options] 32 | 33 | --help -h output usage information 34 | --version -v output the version number 35 | --file -f read command from file 36 | --update-key -u update the API key 37 | --delete-key -d delete the API key 38 | ``` 39 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/asrul/linux-command-gpt 2 | 3 | go 1.18 4 | 5 | require github.com/atotto/clipboard v0.1.4 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= 2 | github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= 3 | -------------------------------------------------------------------------------- /gpt/gpt.go: -------------------------------------------------------------------------------- 1 | package gpt 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "io" 8 | "net/http" 9 | "os" 10 | "path/filepath" 11 | "strings" 12 | ) 13 | 14 | type Gpt3 struct { 15 | CompletionUrl string 16 | Prompt string 17 | Model string 18 | HomeDir string 19 | ApiKeyFile string 20 | ApiKey string 21 | Temperature float64 22 | } 23 | 24 | type Chat struct { 25 | Role string `json:"role"` 26 | Content string `json:"content"` 27 | } 28 | 29 | type Gpt3Request struct { 30 | Model string `json:"model"` 31 | Messages []Chat `json:"messages"` 32 | Temperature float64 `json:"temperature"` 33 | } 34 | 35 | type Gpt3Response struct { 36 | Choices []struct { 37 | Message Chat `json:"message"` 38 | } `json:"choices"` 39 | } 40 | 41 | func (gpt3 *Gpt3) deleteApiKey() { 42 | filePath := gpt3.HomeDir + string(filepath.Separator) + gpt3.ApiKeyFile 43 | if _, err := os.Stat(filePath); os.IsNotExist(err) { 44 | return 45 | } 46 | err := os.Remove(filePath) 47 | if err != nil { 48 | panic(err) 49 | } 50 | } 51 | 52 | func (gpt3 *Gpt3) updateApiKey(apiKey string) { 53 | filePath := gpt3.HomeDir + string(filepath.Separator) + gpt3.ApiKeyFile 54 | file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC, 0644) 55 | if err != nil { 56 | panic(err) 57 | } 58 | defer file.Close() 59 | 60 | apiKey = strings.TrimSpace(apiKey) 61 | _, err = file.WriteString(apiKey) 62 | if err != nil { 63 | panic(err) 64 | } 65 | gpt3.ApiKey = apiKey 66 | } 67 | 68 | func (gpt3 *Gpt3) storeApiKey(apiKey string) { 69 | if apiKey == "" { 70 | return 71 | } 72 | filePath := gpt3.HomeDir + string(filepath.Separator) + gpt3.ApiKeyFile 73 | file, err := os.Create(filePath) 74 | if err != nil { 75 | panic(err) 76 | } 77 | defer file.Close() 78 | 79 | apiKey = strings.TrimSpace(apiKey) 80 | _, err = file.WriteString(apiKey) 81 | if err != nil { 82 | panic(err) 83 | } 84 | gpt3.ApiKey = apiKey 85 | } 86 | 87 | func (gpt3 *Gpt3) loadApiKey() bool { 88 | dirSeparator := string(filepath.Separator) 89 | apiKeyFile := gpt3.HomeDir + dirSeparator + gpt3.ApiKeyFile 90 | if _, err := os.Stat(apiKeyFile); os.IsNotExist(err) { 91 | return false 92 | } 93 | apiKey, err := os.ReadFile(apiKeyFile) 94 | if err != nil { 95 | return false 96 | } 97 | gpt3.ApiKey = string(apiKey) 98 | 99 | return true 100 | } 101 | 102 | func (gpt3 *Gpt3) UpdateKey() { 103 | var apiKey string 104 | fmt.Print("OpenAI API Key: ") 105 | fmt.Scanln(&apiKey) 106 | gpt3.updateApiKey(apiKey) 107 | } 108 | 109 | func (gpt3 *Gpt3) DeleteKey() { 110 | var c string 111 | fmt.Print("Are you sure you want to delete the API key? (y/N): ") 112 | fmt.Scanln(&c) 113 | if c == "Y" || c == "y" { 114 | gpt3.deleteApiKey() 115 | } 116 | } 117 | 118 | func (gpt3 *Gpt3) InitKey() { 119 | load := gpt3.loadApiKey() 120 | if load { 121 | return 122 | } 123 | var apiKey string 124 | fmt.Print("OpenAI API Key: ") 125 | fmt.Scanln(&apiKey) 126 | gpt3.storeApiKey(apiKey) 127 | } 128 | 129 | func (gpt3 *Gpt3) Completions(ask string) string { 130 | req, err := http.NewRequest("POST", gpt3.CompletionUrl, nil) 131 | if err != nil { 132 | panic(err) 133 | } 134 | req.Header.Set("Content-Type", "application/json") 135 | req.Header.Set("Authorization", "Bearer "+strings.TrimSpace(gpt3.ApiKey)) 136 | 137 | messages := []Chat{ 138 | {"system", gpt3.Prompt}, 139 | {"user", ask}, 140 | } 141 | payload := Gpt3Request{ 142 | gpt3.Model, 143 | messages, 144 | gpt3.Temperature, 145 | } 146 | payloadJson, err := json.Marshal(payload) 147 | if err != nil { 148 | panic(err) 149 | } 150 | req.Body = io.NopCloser(bytes.NewBuffer(payloadJson)) 151 | 152 | client := &http.Client{} 153 | resp, err := client.Do(req) 154 | if err != nil { 155 | panic(err) 156 | } 157 | defer resp.Body.Close() 158 | 159 | body, err := io.ReadAll(resp.Body) 160 | if err != nil { 161 | panic(err) 162 | } 163 | 164 | if resp.StatusCode != http.StatusOK { 165 | fmt.Println(string(body)) 166 | return "" 167 | } 168 | 169 | var res Gpt3Response 170 | err = json.Unmarshal(body, &res) 171 | if err != nil { 172 | panic(err) 173 | } 174 | 175 | return strings.TrimSpace(res.Choices[0].Message.Content) 176 | } 177 | -------------------------------------------------------------------------------- /gpt/gpt_test.go: -------------------------------------------------------------------------------- 1 | package gpt 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestApiKey(t *testing.T) { 8 | gpt3 := Gpt3{ 9 | ApiKeyFile: ".openai_api_key_test", 10 | } 11 | 12 | tests := []struct { 13 | homeDir string 14 | apiKey string 15 | expected bool 16 | expectedApiKey string 17 | }{ 18 | {".", "", false, ""}, 19 | {"./", "", false, ""}, 20 | {".", "the key 123", true, "the key 123"}, 21 | {".", "the key 123\n", true, "the key 123"}, 22 | {".", " the key 123 ", true, "the key 123"}, 23 | {".", " \n\n the key 123 \n\n", true, "the key 123"}, 24 | } 25 | defer gpt3.deleteApiKey() 26 | 27 | for _, test := range tests { 28 | gpt3.HomeDir = test.homeDir 29 | gpt3.storeApiKey(test.apiKey) 30 | load := gpt3.loadApiKey() 31 | gpt3.deleteApiKey() 32 | if load != test.expected { 33 | t.Error("Expected load to be", test.expected, "got", load) 34 | } 35 | if gpt3.ApiKey != test.expectedApiKey { 36 | t.Error("Expected ApiKey to be", test.expectedApiKey, "got", gpt3.ApiKey) 37 | } 38 | } 39 | 40 | // Test update api key 41 | gpt3.HomeDir = "." 42 | gpt3.storeApiKey("test") 43 | updateTests := []struct { 44 | apiKey string 45 | expectedApiKey string 46 | }{ 47 | {"the key 123", "the key 123"}, 48 | {"the key 123\n", "the key 123"}, 49 | {" the key 123 ", "the key 123"}, 50 | {" \n\n the key 123 \n\n", "the key 123"}, 51 | } 52 | for _, test := range updateTests { 53 | gpt3.updateApiKey(test.apiKey) 54 | if gpt3.ApiKey != test.expectedApiKey { 55 | t.Error("Expected ApiKey to be", test.expectedApiKey, "got", gpt3.ApiKey) 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "os" 7 | "os/user" 8 | "strings" 9 | "time" 10 | 11 | "github.com/asrul/linux-command-gpt/gpt" 12 | "github.com/asrul/linux-command-gpt/reader" 13 | "github.com/atotto/clipboard" 14 | ) 15 | 16 | const ( 17 | HOST = "https://api.openai.com/v1/" 18 | COMPLETIONS = "chat/completions" 19 | MODEL = "gpt-4o-mini" 20 | PROMPT = "Reply with linux command and nothing else. No need explanation. No need code blocks" 21 | 22 | // This file is created in the user's home directory 23 | // Example: /home/username/.openai_api_key 24 | API_KEY_FILE = ".openai_api_key" 25 | 26 | HELP = ` 27 | 28 | Usage: lcg [options] 29 | 30 | --help -h output usage information 31 | --version -v output the version number 32 | --file -f read command from file 33 | --update-key -u update the API key 34 | --delete-key -d delete the API key 35 | 36 | Example Usage: lcg I want to extract linux-command-gpt.tar.gz file 37 | Example Usage: lcg --file /path/to/file.json I want to print object questions with jq 38 | ` 39 | 40 | VERSION = "0.2.1" 41 | CMD_HELP = 100 42 | CMD_VERSION = 101 43 | CMD_UPDATE = 102 44 | CMD_DELETE = 103 45 | CMD_COMPLETION = 110 46 | ) 47 | 48 | func handleCommand(cmd string) int { 49 | if cmd == "" || cmd == "--help" || cmd == "-h" { 50 | return CMD_HELP 51 | } 52 | if cmd == "--version" || cmd == "-v" { 53 | return CMD_VERSION 54 | } 55 | if cmd == "--update-key" || cmd == "-u" { 56 | return CMD_UPDATE 57 | } 58 | if cmd == "--delete-key" || cmd == "-d" { 59 | return CMD_DELETE 60 | } 61 | return CMD_COMPLETION 62 | } 63 | 64 | func getCommand(gpt3 gpt.Gpt3, cmd string) (string, float64) { 65 | gpt3.InitKey() 66 | s := time.Now() 67 | done := make(chan bool) 68 | go func() { 69 | loadingChars := []rune{'-', '\\', '|', '/'} 70 | i := 0 71 | for { 72 | select { 73 | case <-done: 74 | fmt.Printf("\r") 75 | return 76 | default: 77 | fmt.Printf("\rLoading %c", loadingChars[i]) 78 | i = (i + 1) % len(loadingChars) 79 | time.Sleep(30 * time.Millisecond) 80 | } 81 | } 82 | }() 83 | 84 | r := gpt3.Completions(cmd) 85 | done <- true 86 | elapsed := time.Since(s).Seconds() 87 | elapsed = math.Round(elapsed*100) / 100 88 | 89 | if r == "" { 90 | return "", elapsed 91 | } 92 | return r, elapsed 93 | } 94 | 95 | func main() { 96 | currentUser, err := user.Current() 97 | if err != nil { 98 | panic(err) 99 | } 100 | 101 | args := os.Args 102 | cmd := "" 103 | file := "" 104 | if len(args) > 1 { 105 | start := 1 106 | if args[1] == "--file" || args[1] == "-f" { 107 | file = args[2] 108 | start = 3 109 | } 110 | cmd = strings.Join(args[start:], " ") 111 | } 112 | 113 | if file != "" { 114 | err := reader.FileToPrompt(&cmd, file) 115 | if err != nil { 116 | fmt.Println(err) 117 | return 118 | } 119 | } 120 | 121 | h := handleCommand(cmd) 122 | 123 | if h == CMD_HELP { 124 | fmt.Println(HELP) 125 | return 126 | } 127 | 128 | if h == CMD_VERSION { 129 | fmt.Println(VERSION) 130 | return 131 | } 132 | 133 | gpt3 := gpt.Gpt3{ 134 | CompletionUrl: HOST + COMPLETIONS, 135 | Model: MODEL, 136 | Prompt: PROMPT, 137 | HomeDir: currentUser.HomeDir, 138 | ApiKeyFile: API_KEY_FILE, 139 | Temperature: 0.01, 140 | } 141 | 142 | if h == CMD_UPDATE { 143 | gpt3.UpdateKey() 144 | return 145 | } 146 | 147 | if h == CMD_DELETE { 148 | gpt3.DeleteKey() 149 | return 150 | } 151 | 152 | c := "R" 153 | r := "" 154 | elapsed := 0.0 155 | for c == "R" || c == "r" { 156 | r, elapsed = getCommand(gpt3, cmd) 157 | c = "N" 158 | fmt.Printf("Completed in %v seconds\n\n", elapsed) 159 | fmt.Println(r) 160 | fmt.Print("\nDo you want to (c)opy, (r)egenerate, or take (N)o action on the command? (c/r/N): ") 161 | fmt.Scanln(&c) 162 | 163 | // No action 164 | if c == "N" || c == "n" { 165 | return 166 | } 167 | } 168 | 169 | if r == "" { 170 | return 171 | } 172 | 173 | // Copy to clipboard 174 | if c == "C" || c == "c" { 175 | clipboard.WriteAll(r) 176 | fmt.Println("\033[33mCopied to clipboard") 177 | return 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestHandleCommand(t *testing.T) { 8 | tests := []struct { 9 | command string 10 | expected int 11 | }{ 12 | {"", CMD_HELP}, 13 | {"--help", CMD_HELP}, 14 | {"-h", CMD_HELP}, 15 | {"--version", CMD_VERSION}, 16 | {"-v", CMD_VERSION}, 17 | {"--update-key", CMD_UPDATE}, 18 | {"-u", CMD_UPDATE}, 19 | {"--delete-key", CMD_DELETE}, 20 | {"-d", CMD_DELETE}, 21 | {"random strings", CMD_COMPLETION}, 22 | {"--test", CMD_COMPLETION}, 23 | {"-test", CMD_COMPLETION}, 24 | {"how to extract test.tar.gz", CMD_COMPLETION}, 25 | } 26 | 27 | for _, test := range tests { 28 | result := handleCommand(test.command) 29 | if result != test.expected { 30 | t.Error("Expected", test.expected, "got", result) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /reader/file.go: -------------------------------------------------------------------------------- 1 | package reader 2 | 3 | import ( 4 | "bufio" 5 | "os" 6 | ) 7 | 8 | func FileToPrompt(cmd *string, filePath string) error { 9 | f, err := os.Open(filePath) 10 | if err != nil { 11 | return err 12 | } 13 | defer f.Close() 14 | reader := bufio.NewReader(f) 15 | *cmd = *cmd + "\nFile path: " + filePath + "\n" 16 | for { 17 | line, err := reader.ReadString('\n') 18 | if err != nil { 19 | break 20 | } 21 | *cmd = *cmd + "\n" + line 22 | } 23 | return nil 24 | } 25 | --------------------------------------------------------------------------------