-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ollama request to include diff string and fix json decoding error
- Loading branch information
1 parent
3cf7673
commit ab4177e
Showing
7 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
var configKey = "kemit-config" | ||
|
||
type Config struct { | ||
OllamaHost string `json:"ollama_host"` | ||
OllamaModel string `json:"ollama_model"` | ||
} | ||
|
||
func (cfg *Config) SaveConfig() error { | ||
jsonData, err := json.Marshal(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch runtime.GOOS { | ||
case "darwin": | ||
return saveConfigMacOS(string(jsonData), configKey) | ||
case "linux": | ||
return saveConfigLinux(string(jsonData), configKey) | ||
default: | ||
return fmt.Errorf("unsupported platform: %s\n", runtime.GOOS) | ||
} | ||
} | ||
|
||
func (cfg *Config) LoadConfig() error { | ||
var output []byte | ||
var err error | ||
switch runtime.GOOS { | ||
case "darwin": | ||
output, err = loadConfigMacOS(configKey) | ||
case "linux": | ||
output, err = loadConfigLinux(configKey) | ||
default: | ||
return fmt.Errorf("unsupported platform: %s", runtime.GOOS) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(output, cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package config | ||
|
||
import "os/exec" | ||
|
||
func saveConfigMacOS(value, key string) error { | ||
cmd := exec.Command("security", "add-generic-password", "-a", key, "-s", key, "-w", value, "-U") | ||
return cmd.Run() | ||
} | ||
|
||
func saveConfigLinux(value, key string) error { | ||
cmd := exec.Command("secret-tool", "store", "--label=kemit", key, value) | ||
return cmd.Run() | ||
} | ||
|
||
func loadConfigMacOS(key string) ([]byte, error) { | ||
cmd := exec.Command("security", "find-generic-password", "-a", key, "-s", key, "-w") | ||
return cmd.Output() | ||
} | ||
|
||
func loadConfigLinux(key string) ([]byte, error) { | ||
cmd := exec.Command("secret-tool", "lookup", key) | ||
return cmd.Output() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package engine | ||
|
||
type Engine interface { | ||
GetCommit() (string, error) | ||
GetCommit(diff string) (string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
mv kemit /usr/local/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/zaidfadhil/kemit/config" | ||
"github.com/zaidfadhil/kemit/engine" | ||
"github.com/zaidfadhil/kemit/git" | ||
) | ||
|
||
func main() { | ||
cfg := &config.Config{} | ||
cfg.LoadConfig() | ||
|
||
if len(os.Args) > 1 { | ||
if os.Args[1] == "config" { | ||
setConfig(os.Args[2:], cfg) | ||
} | ||
} else { | ||
run(cfg) | ||
} | ||
} | ||
|
||
func run(cfg *config.Config) { | ||
diff, err := git.Diff() | ||
if err != nil { | ||
fmt.Println("error:", err) | ||
} | ||
|
||
if len(diff) == 0 { | ||
if diff == "" { | ||
fmt.Println("nothing to commit") | ||
} else { | ||
ollama := engine.NewOllama(diff) | ||
message, err := ollama.GetCommit() | ||
ollama := engine.NewOllama(cfg.OllamaHost, cfg.OllamaModel) | ||
message, err := ollama.GetCommit(diff) | ||
if err != nil { | ||
fmt.Println("error:", err) | ||
} else { | ||
fmt.Println(message) | ||
} | ||
} | ||
} | ||
|
||
var flags = flag.NewFlagSet("config", flag.ExitOnError) | ||
|
||
var configUsage = `Usage: kemit config command [options] | ||
Options: | ||
Commands: | ||
--ollama_host Set ollama host. ex: http://localhost:11434 | ||
--ollama_model Set ollama host. ex: llama3` | ||
|
||
func setConfig(args []string, cfg *config.Config) error { | ||
flags.Usage = func() { | ||
fmt.Fprintln(os.Stderr, configUsage) | ||
} | ||
|
||
flags.StringVar(&cfg.OllamaHost, "ollama_host", cfg.OllamaHost, "ollama host") | ||
flags.StringVar(&cfg.OllamaModel, "ollama_model", cfg.OllamaModel, "ollama model") | ||
|
||
flags.Parse(args) | ||
|
||
if len(args) == 0 { | ||
flags.Usage() | ||
} | ||
|
||
err := cfg.SaveConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |