Skip to content

Commit 355bb86

Browse files
authored
Merge pull request #50 from issamoxix/feature/create-run-refactor-function
feat(tools): create RunRefactor function to handle refactor command processing
2 parents 6c75cda + a72ad81 commit 355bb86

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func main() {
5050
" -c, c <arg> : Get a prompt response based on the given argument.\n" +
5151
" login, -login, --login <token> : Login to the application.\n" +
5252
" -l, l, live, --live : Get a live prompt response.\n" +
53+
" refactor, r, -r, --refactor : Run the refactor command.\n" +
5354
" help, h, -h, --help : Show this help message.")
5455
return
5556

@@ -80,6 +81,10 @@ func main() {
8081
fmt.Println("Login successful! Token stored. ", token)
8182
return
8283

84+
case "refactor", "r", "-r", "--refactor":
85+
tools.RunRefactor()
86+
return
87+
8388
default:
8489
fmt.Printf("Unknown command: %s\nUse \"comit help\" to see available commands", os.Args[1])
8590
return

services/openai/ai.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type commitResponse struct {
2121
Message []string `json:"message"`
2222
}
2323

24+
type refactorResponse struct {
25+
Message string `json:"message"`
26+
}
27+
2428
type RequestData struct {
2529
Code string `json:"code"`
2630
}
@@ -45,6 +49,44 @@ type BranchSelector interface {
4549
SelectBranchMessage(messages []string, context string) error
4650
}
4751

52+
func GetRefactorMessage(content string, token string) string {
53+
var url = utils.ComitURL + "/refactore" + "?token=" + token
54+
payload := RequestData{
55+
Code: content,
56+
}
57+
58+
jsonData, err := json.Marshal(payload)
59+
if err != nil {
60+
return "Error: " + err.Error()
61+
}
62+
63+
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
64+
65+
if err != nil {
66+
return "Error: " + err.Error()
67+
}
68+
69+
if resp.StatusCode != 200 {
70+
return "Error: " + resp.Status
71+
}
72+
73+
defer resp.Body.Close()
74+
75+
body, err := io.ReadAll(resp.Body)
76+
if err != nil {
77+
return "Error: " + err.Error()
78+
}
79+
80+
var data refactorResponse
81+
if err := json.Unmarshal(body, &data); err != nil {
82+
return "Error: " + err.Error()
83+
}
84+
85+
lines := strings.Split(data.Message, "\n")
86+
PretterPromptResponse(lines)
87+
return "Ok"
88+
}
89+
4890
func GetCommitMessage(content string, selector CommitMessageSelector, token string) string {
4991
var url = utils.ComitURL + "/commit" + "?token=" + token
5092
payload := RequestData{

services/utils/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package utils
22

3-
var Version = "0.9"
3+
var Version = "0.9.1"
44

55
var (
66
UpdateLink = "https://github.com/issamoxix/Comit/releases/download/%s/%s"

services/utils/tools/commit_helper.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ func RunCommit() {
131131
}
132132
}
133133

134+
func RunRefactor() {
135+
output, err := CheckStage()
136+
if err != nil {
137+
fmt.Println("Error:", err)
138+
return
139+
}
140+
token, _ := auth.GetToken()
141+
if token == "" {
142+
token = "default"
143+
}
144+
messageStatus := ai.GetRefactorMessage(output, token)
145+
if messageStatus != "Ok" {
146+
fmt.Println("Something went wrong please try again")
147+
return
148+
}
149+
}
150+
134151
func allEmpty(messages []string) bool {
135152
for _, msg := range messages {
136153
if msg != "" {

0 commit comments

Comments
 (0)