Skip to content

Commit 2807eb6

Browse files
Added fix syntax command
1 parent 81882c0 commit 2807eb6

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fallowing operations are supported:
1515
* Migrating the programming language syntax.
1616
* Generating source code documentation.
1717
* Refactoring and renaming local variables.
18+
* Fixing syntax.
1819

1920
## Supported languages
2021

cli/cli.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func (c *Cli) parseArgs() {
5454
case "refactor":
5555
c.parseRefactorArgs()
5656
break
57+
case "fix":
58+
c.parseFixArgs()
59+
break
5760
case "configure":
5861
c.configure()
5962
case "version":
@@ -253,6 +256,47 @@ func (c *Cli) parseRefactorArgs() {
253256
}
254257
}
255258

259+
func (c *Cli) parseFixArgs() {
260+
if len(os.Args) < 3 {
261+
fmt.Printf("No command specified")
262+
c.printRefactorHelp()
263+
}
264+
265+
switch os.Args[2] {
266+
case "syntax":
267+
refactorNaming := flag.NewFlagSet("fixSyntax", flag.ExitOnError)
268+
lang := refactorNaming.String("language", "", "Programming language: JavaScript, Java, Kotlin")
269+
270+
err := refactorNaming.Parse(os.Args[3:])
271+
if err != nil {
272+
c.logger.Errorf("Could not parse args %v", err)
273+
}
274+
275+
if len(refactorNaming.Args()) == 0 {
276+
c.logger.Errorf("Expected file input")
277+
fmt.Printf("Usage: codemaker fix syntax <file>")
278+
os.Exit(1)
279+
}
280+
281+
config, err := createConfig()
282+
if err != nil {
283+
c.logger.Errorf("No valid api key found %v", err)
284+
os.Exit(1)
285+
}
286+
287+
cl := c.createClient(*config)
288+
input := refactorNaming.Args()[0:]
289+
290+
if err := c.fixSyntax(cl, lang, input); err != nil {
291+
c.logger.Errorf("Could not fix syntax %v", err)
292+
}
293+
break
294+
default:
295+
fmt.Printf("Unknown command %s\n", os.Args[2])
296+
c.printFixHelp()
297+
}
298+
}
299+
256300
func (c *Cli) generateCode(cl client.Client, lang *string, replace *bool, codePath *string, files []string) error {
257301
return c.walkPath(files, func(file string) error {
258302
if lang == nil || len(*lang) == 0 {
@@ -424,6 +468,38 @@ func (c *Cli) refactorNaming(cl client.Client, lang *string, files []string) err
424468
})
425469
}
426470

471+
func (c *Cli) fixSyntax(cl client.Client, lang *string, files []string) error {
472+
return c.walkPath(files, func(file string) error {
473+
if lang == nil || len(*lang) == 0 {
474+
actLang, err := languageFromExtension(filepath.Ext(file))
475+
if err != nil {
476+
c.logger.Errorf("skipping unsupported file %s", file)
477+
return nil
478+
}
479+
lang = &actLang
480+
}
481+
482+
c.logger.Infof("Fixing syntax in file %s", file)
483+
source, err := c.readFile(file)
484+
if err != nil {
485+
c.logger.Errorf("failed to read file %s %v", file, err)
486+
return nil
487+
}
488+
489+
output, err := c.process(cl, client.ModeFixSyntax, *lang, false, nil, "", source)
490+
if err != nil {
491+
c.logger.Errorf("failed to fix syntax in file %s %v", file, err)
492+
return nil
493+
}
494+
495+
if err := c.writeFile(file, *output); err != nil {
496+
c.logger.Errorf("failed to write file %s %v", file, err)
497+
return nil
498+
}
499+
return nil
500+
})
501+
}
502+
427503
func (c *Cli) process(cl client.Client, mode string, lang string, replace bool, codePath *string, langVer string, source string) (*string, error) {
428504
modify := client.ModifyNone
429505
if replace {
@@ -557,6 +633,7 @@ func (c *Cli) printHelp() {
557633
fmt.Printf(" * generate\n")
558634
fmt.Printf(" * migrate\n")
559635
fmt.Printf(" * refactor\n")
636+
fmt.Printf(" * fix\n")
560637
fmt.Printf(" * configure\n")
561638
fmt.Printf(" * version\n")
562639
os.Exit(1)
@@ -588,6 +665,14 @@ func (c *Cli) printRefactorHelp() {
588665
os.Exit(1)
589666
}
590667

668+
func (c *Cli) printFixHelp() {
669+
fmt.Printf("Usage: codemaker fix <command>\n")
670+
fmt.Printf("\n")
671+
fmt.Printf("Commands:\n")
672+
fmt.Printf(" * syntax\n")
673+
os.Exit(1)
674+
}
675+
591676
func (c *Cli) walkPath(files []string, visitor func(file string) error) error {
592677
for _, file := range files {
593678
if err := visitor(file); err != nil {

0 commit comments

Comments
 (0)