Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes for issues found when running against our codebase #10

Merged
merged 6 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"os"
)



var (
doWrite = flag.Bool("w", false, "doWrite result to (source) file instead of stdout")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
Expand All @@ -20,6 +18,9 @@ var (
)

func report(err error) {
if err == nil {
return
}
scanner.PrintError(os.Stderr, err)
exitCode = 1
}
Expand All @@ -37,7 +38,6 @@ func usage() {
os.Exit(2)
}


func main() {
flag.Usage = usage
paths := parseFlags()
Expand Down
29 changes: 15 additions & 14 deletions pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (p *pkg) fmt() []byte {
ret = append(ret, linebreak)
}
}
if ret[len(ret)-1] == linebreak {
if len(ret) > 0 && ret[len(ret)-1] == linebreak {
ret = ret[:len(ret)-1]
}

Expand All @@ -143,9 +143,9 @@ func getPkgInfo(line string, comment bool) (string, string, string) {
s := strings.Split(line, commentFlag)
pkgArray := strings.Split(s[0], blank)
if len(pkgArray) > 1 {
return pkgArray[1], pkgArray[0], fmt.Sprintf("%s%s%s", commentFlag, blank, s[1])
return pkgArray[1], pkgArray[0], fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1]))
} else {
return pkgArray[0], "", fmt.Sprintf("%s%s%s", commentFlag, blank, s[1])
return strings.TrimSpace(pkgArray[0]), "", fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1]))
}
} else {
pkgArray := strings.Split(line, blank)
Expand Down Expand Up @@ -328,28 +328,34 @@ func processFile(filename string, out io.Writer, set *FlagSet) error {
return err
}

func Run(filename string, set *FlagSet) ([]byte, error) {
// Run return source and result in []byte if succeed
func Run(filename string, set *FlagSet) ([]byte, []byte, error) {
var err error

f, err := os.Open(filename)
if err != nil {
return nil, err
return nil, nil, err
}
defer f.Close()

src, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
return nil, nil, err
}

ori := make([]byte, len(src))
copy(ori, src)
start := bytes.Index(src, importStartFlag)
// in case no importStartFlag or importStartFlag exist in the commentFlag
if start < 0 {
return nil, nil
return nil, nil, nil
}
end := bytes.Index(src[start:], importEndFlag) + start

// in case import flags are part of a codegen template, or otherwise "wrong"
if start+len(importStartFlag) > end {
return nil, nil, nil
}

ret := bytes.Split(src[start+len(importStartFlag):end], []byte(linebreak))

Expand All @@ -358,13 +364,8 @@ func Run(filename string, set *FlagSet) ([]byte, error) {
res := append(src[:start+len(importStartFlag)], append(p.fmt(), src[end+1:]...)...)

if bytes.Equal(ori, res) {
return nil, nil
}

data, err := diff(ori, res, filename)
if err != nil {
return nil, fmt.Errorf("failed to diff: %v", err)
return ori, nil, nil
}

return data, nil
return ori, res, nil
}