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

Add a new command line argument to re-arrange the order of the three #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 31 additions & 3 deletions goimportssort.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
list = flag.Bool("l", false, "write results to stdout")
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
localPrefix = flag.String("local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
order = flag.String("o", "iel", "custom the order of the section of imports. e.g. ixl means inbuilt, external, and local")
verbose bool // verbose logging
standardPackages = make(map[string]struct{})
)
Expand Down Expand Up @@ -290,6 +291,25 @@ func countImports(impModels [][]impModel) int {
func convertImportsToSlice(node *dst.File) ([][]impModel, error) {
importCategories := make([][]impModel, 3)

inbuild := &importCategories[0]
external := &importCategories[1]
local := &importCategories[2]
if sortString(*order) == sortString("iel") {
chars := []rune(*order)
for i := 0; i < 3; i++ {
switch chars[i] {
case 'l':
local = &importCategories[i]
case 'e':
external = &importCategories[i]
case 'i':
inbuild = &importCategories[i]
default:
return importCategories, fmt.Errorf("cannot parse the order argument given: %s", *order)
}
}
}

for _, importSpec := range node.Imports {
impName := importSpec.Path.Value
impNameWithoutQuotes := strings.Trim(impName, "\"")
Expand All @@ -302,17 +322,25 @@ func convertImportsToSlice(node *dst.File) ([][]impModel, error) {
locImpModel.path = impName

if *localPrefix != "" && strings.Count(impName, *localPrefix) > 0 {
importCategories[2] = append(importCategories[2], locImpModel)
*local = append(*local, locImpModel)
} else if isStandardPackage(impNameWithoutQuotes) {
importCategories[0] = append(importCategories[0], locImpModel)
*inbuild = append(*inbuild, locImpModel)
} else {
importCategories[1] = append(importCategories[1], locImpModel)
*external = append(*external, locImpModel)
}
}

return importCategories, nil
}

func sortString(str string) string {
charArray := []rune(str)
sort.Slice(charArray, func(i int, j int) bool {
return charArray[i] < charArray[j]
})
return string(charArray)
}

// loadStandardPackages tries to fetch all golang std packages
func loadStandardPackages() error {
pkgs, err := packages.Load(nil, "std")
Expand Down