Skip to content

Commit

Permalink
fix: arg parser trailing space and plaster.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanv8060 committed Jul 17, 2022
1 parent 497426f commit 8431fe2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
8 changes: 8 additions & 0 deletions code_regions/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
imports:
- import "aquila/cmd"
- ""
main:
- func main() {
- "\tcmd.Execute()"
- '}'
- ""
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package main

// #aqstart imports
import "aquila/cmd"

// #aqend imports

// #aqstart main
func main() {
cmd.Execute()
}

// #aqend main
8 changes: 7 additions & 1 deletion models/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
)

var BlankLine = "^\\s*$"
var LeadingWhiteSpace = "^[ \\t]*"
var directiveRegex = "^(\\s*)(\\S.*?)?#(?P<DirectiveType>aqstart|aqend)\\b\\s*(?P<args>.*?)(?:\\s*(?:-->|\\*\\/))?\\s*$"

type Directive struct {
Expand All @@ -31,8 +33,12 @@ func GetDirective(line string) *Directive {
} else {
kind = EndDirective
}
args := strings.Split(match[4], ",")
for i, arg := range args {
args[i] = strings.TrimSpace(arg)
}
return &Directive{
Regions: strings.Split(match[4], ","),
Regions: args,
Kind: kind,
}
} else {
Expand Down
32 changes: 32 additions & 0 deletions utils/readFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gopkg.in/yaml.v2"
"os"
"path/filepath"
"regexp"
)

var entireFile = ""
Expand Down Expand Up @@ -40,9 +41,39 @@ func ReadFile() {
log.Fatal().Msgf("scan file error: %v", err)
return
}
// iterate over regions and update them
for region, lines := range regions {
//call function to update regions[region]
regions[region] = *removeTrailingLines(&lines)
regions[region] = *removeTrailingPlaster(&lines)
}

saveToYamlFile()
}

func removeTrailingPlaster(lines *[]string) *[]string {
// remove last element if it matches ...
if len(*lines) > 0 {
if (*lines)[len(*lines)-1] == "..." {
*lines = (*lines)[:len(*lines)-1]
}
}
return lines
}

func removeTrailingLines(lines *[]string) *[]string {

// remove all blank lines from end in lines
for i := len(*lines) - 1; i >= 0; i-- {
blank, err := regexp.MatchString(directive.BlankLine, (*lines)[i])
if err == nil && blank {
*lines = (*lines)[:i]
} else {
break
}
}
return lines
}
func saveToYamlFile() {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -105,6 +136,7 @@ func regionEnd(directive directive.Directive) {
for _, region := range _regions {
if openRegions.Contains(region) {
regions[region] = append(regions[region], "...")
openRegions.Remove(region)
} else {
log.Debug().Msg(region + "doesn't have a start")
}
Expand Down

0 comments on commit 8431fe2

Please sign in to comment.