Skip to content

Commit

Permalink
CodeMaker CLI v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
codemakerai-dev committed Apr 6, 2024
1 parent 95b4d7b commit 4205f01
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 92 deletions.
112 changes: 44 additions & 68 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cli

import (
"context"
"flag"
"fmt"
"github.com/codemakerai/codemaker-sdk-go/client"
Expand Down Expand Up @@ -74,6 +75,7 @@ func (c *Cli) parseGenerateArgs() {
lang := generateDocsCmd.String("language", "", "Programming language: JavaScript, Java, Kotlin")
replace := generateDocsCmd.Bool("replace", false, "Determines if the existing code is replaced")
codePath := generateDocsCmd.String("codepath", "", "The codepath to match.")
model := generateDocsCmd.String("model", "", "The fine-tuned model name.")

err := generateDocsCmd.Parse(os.Args[3:])
if err != nil {
Expand All @@ -93,10 +95,15 @@ func (c *Cli) parseGenerateArgs() {
os.Exit(1)
}

cl := c.createClient(*config)
cl, err := c.createClient(*config)
if err != nil {
c.logger.Errorf("Can not create client: %v", err)
os.Exit(1)
}

files := generateDocsCmd.Args()[0:]

if err := c.generateCode(cl, lang, replace, codePath, files); err != nil {
if err := c.generateCode(cl, lang, replace, codePath, model, files); err != nil {
c.logger.Errorf("Could not generate the code %v", err)
}
break
Expand Down Expand Up @@ -124,7 +131,12 @@ func (c *Cli) parseGenerateArgs() {
os.Exit(1)
}

cl := c.createClient(*config)
cl, err := c.createClient(*config)
if err != nil {
c.logger.Errorf("Can not create client: %v", err)
os.Exit(1)
}

files := generateDocsCmd.Args()[0:]

if err := c.generateDocumentation(cl, lang, replace, codePath, files); err != nil {
Expand Down Expand Up @@ -165,7 +177,12 @@ func (c *Cli) parseFixArgs() {
os.Exit(1)
}

cl := c.createClient(*config)
cl, err := c.createClient(*config)
if err != nil {
c.logger.Errorf("Can not create client: %v", err)
os.Exit(1)
}

input := refactorNaming.Args()[0:]

if err := c.fixSyntax(cl, lang, input); err != nil {
Expand All @@ -178,7 +195,7 @@ func (c *Cli) parseFixArgs() {
}
}

func (c *Cli) generateCode(cl client.Client, lang *string, replace *bool, codePath *string, files []string) error {
func (c *Cli) generateCode(cl client.Client, lang *string, replace *bool, codePath *string, model *string, files []string) error {
return c.walkPath(files, func(file string) error {
if lang == nil || len(*lang) == 0 {
actLang, err := languageFromExtension(filepath.Ext(file))
Expand All @@ -194,7 +211,7 @@ func (c *Cli) generateCode(cl client.Client, lang *string, replace *bool, codePa
return err
}

output, err := c.process(cl, client.ModeCode, *lang, *replace, codePath, "", source)
output, err := c.process(cl, client.ModeCode, *lang, *replace, codePath, model, source)
if err != nil {
return err
}
Expand Down Expand Up @@ -223,7 +240,7 @@ func (c *Cli) generateDocumentation(cl client.Client, lang *string, replace *boo
return err
}

output, err := c.process(cl, client.ModeDocument, *lang, *replace, codePath, "", source)
output, err := c.process(cl, client.ModeDocument, *lang, *replace, codePath, nil, source)
if err != nil {
return err
}
Expand Down Expand Up @@ -254,7 +271,7 @@ func (c *Cli) generateTests(cl client.Client, lang *string, files []string, outp
return err
}

output, err := c.process(cl, client.ModeUnitTest, *lang, false, nil, "", source)
output, err := c.process(cl, client.ModeUnitTest, *lang, false, nil, nil, source)
if err != nil {
c.logger.Errorf("failed to generate documentation for file %s %v", file, err)
return err
Expand Down Expand Up @@ -285,7 +302,7 @@ func (c *Cli) generateTests(cl client.Client, lang *string, files []string, outp
})
}

func (c *Cli) migrateSyntax(cl client.Client, lang *string, langVer *string, files []string) error {
func (c *Cli) migrateSyntax(cl client.Client, lang *string, files []string) error {
return c.walkPath(files, func(file string) error {
if lang == nil || len(*lang) == 0 {
actLang, err := languageFromExtension(filepath.Ext(file))
Expand All @@ -303,7 +320,7 @@ func (c *Cli) migrateSyntax(cl client.Client, lang *string, langVer *string, fil
return nil
}

output, err := c.process(cl, client.ModeMigrateSyntax, *lang, false, nil, *langVer, source)
output, err := c.process(cl, client.ModeMigrateSyntax, *lang, false, nil, nil, source)
if err != nil {
c.logger.Errorf("failed to migrate syntax in file %s %v", file, err)
return nil
Expand Down Expand Up @@ -335,7 +352,7 @@ func (c *Cli) refactorNaming(cl client.Client, lang *string, files []string) err
return nil
}

output, err := c.process(cl, client.ModeRefactorNaming, *lang, false, nil, "", source)
output, err := c.process(cl, client.ModeRefactorNaming, *lang, false, nil, nil, source)
if err != nil {
c.logger.Errorf("failed to rename variables in file %s %v", file, err)
return nil
Expand Down Expand Up @@ -367,7 +384,7 @@ func (c *Cli) fixSyntax(cl client.Client, lang *string, files []string) error {
return nil
}

output, err := c.process(cl, client.ModeFixSyntax, *lang, false, nil, "", source)
output, err := c.process(cl, client.ModeFixSyntax, *lang, false, nil, nil, source)
if err != nil {
c.logger.Errorf("failed to fix syntax in file %s %v", file, err)
return nil
Expand All @@ -381,7 +398,9 @@ func (c *Cli) fixSyntax(cl client.Client, lang *string, files []string) error {
})
}

func (c *Cli) process(cl client.Client, mode string, lang string, replace bool, codePath *string, langVer string, source string) (*string, error) {
func (c *Cli) process(cl client.Client, mode string, lang string, replace bool, codePath *string, model *string, source string) (*string, error) {
ctx := context.Background()

modify := client.ModifyNone
if replace {
modify = client.ModifyReplace
Expand All @@ -391,57 +410,23 @@ func (c *Cli) process(cl client.Client, mode string, lang string, replace bool,
codePath = nil
}

process, err := cl.CreateProcess(&client.CreateProcessRequest{
Process: client.Process{
Mode: mode,
Language: lang,
Input: client.Input{
Source: source,
},
Options: &client.Options{
LanguageVersion: &langVer,
Modify: &modify,
CodePath: codePath,
},
process, err := cl.Process(ctx, &client.ProcessRequest{
Mode: mode,
Language: lang,
Input: client.Input{
Source: source,
},
Options: &client.Options{
Modify: &modify,
CodePath: codePath,
Model: model,
},
})
if err != nil {
return nil, err
}

retry := 0
timeout := time.After(processTimeout)
for {
status, err := cl.GetProcessStatus(&client.GetProcessStatusRequest{
Id: process.Id,
})
if err != nil {
return nil, err
}

if c.isCompleted(status) {
break
} else if c.isFailed(status) {
return nil, fmt.Errorf("the task processing has failed")
}

select {
case <-timeout:
return nil, fmt.Errorf("the task processing had timed out")
default:
c.backoff(retry)
retry++
}
}

output, err := cl.GetProcessOutput(&client.GetProcessOutputRequest{
Id: process.Id,
})
if err != nil {
return nil, err
}

return &output.Output.Source, nil
return &process.Source, nil
}

func (c *Cli) configure() error {
Expand Down Expand Up @@ -480,15 +465,6 @@ func (c *Cli) printVersion() {
c.logger.Infof("CodeMaker CLI version %s (Build %s)", Version, Build)
}

func (c *Cli) isCompleted(status *client.GetProcessStatusResponse) bool {
return status.Status == client.StatusCompleted
}

func (c *Cli) isFailed(status *client.GetProcessStatusResponse) bool {
return status.Status == client.StatusFailed ||
status.Status == client.StatusTimedOut
}

func (c *Cli) backoff(retry int) {
retry -= nonExponentRetries
if retry < 0 {
Expand Down Expand Up @@ -576,7 +552,7 @@ func (c *Cli) writeFile(file string, source string) error {
return os.WriteFile(file, []byte(source), 0644)
}

func (c *Cli) createClient(config client.Config) client.Client {
func (c *Cli) createClient(config client.Config) (client.Client, error) {
return client.NewClient(config)
}

Expand Down
4 changes: 2 additions & 2 deletions cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
package cli

var (
Version string = "0.0.17"
Build string = "20240406"
Version string = "1.0.0"
Build string = "20240405"
)
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module github.com/codemakerai/codemaker-cli
go 1.21

require (
github.com/codemakerai/codemaker-sdk-go v0.0.16
github.com/codemakerai/codemaker-sdk-go v1.0.0
github.com/joho/godotenv v1.5.1
go.uber.org/zap v1.27.0
)

require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
38 changes: 18 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/codemakerai/codemaker-sdk-go v0.0.14 h1:DWKKAQ9Lmr8+HDHgC4z4EtSO63zSUwvkvl8TcBscFW0=
github.com/codemakerai/codemaker-sdk-go v0.0.14/go.mod h1:mPB+xrOVXMLP+Ol+I/0eWkW3dbwKd71I23PzAYvuu+k=
github.com/codemakerai/codemaker-sdk-go v0.0.15 h1:tGzHAscwHc8dHPxpHmsEQGJopb6OG/Xi8Zdku135hRU=
github.com/codemakerai/codemaker-sdk-go v0.0.15/go.mod h1:mPB+xrOVXMLP+Ol+I/0eWkW3dbwKd71I23PzAYvuu+k=
github.com/codemakerai/codemaker-sdk-go v0.0.16 h1:6LwFpGFdotMuS9ESTNygV0SfZzgcGZRDlVx6pS9OWSI=
github.com/codemakerai/codemaker-sdk-go v0.0.16/go.mod h1:mPB+xrOVXMLP+Ol+I/0eWkW3dbwKd71I23PzAYvuu+k=
github.com/codemakerai/codemaker-sdk-go v1.0.0 h1:xisPtlcJkRFBGE3JSvzPMjKKtCsEW5lqY5MpnKh5kSg=
github.com/codemakerai/codemaker-sdk-go v1.0.0/go.mod h1:T1nfKPMJgL/qyMTc0CCbdqiAlcwXK9ljGnJbuifbr2c=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
google.golang.org/grpc v1.63.0 h1:WjKe+dnvABXyPJMD7KDNLxtoGk5tgk+YFWN6cBWjZE8=
google.golang.org/grpc v1.63.0/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 4205f01

Please sign in to comment.