From 0bd2748a52a33a65f860c3d9ab3df9cc9879c6ec Mon Sep 17 00:00:00 2001 From: "Nathan J. Mehl" Date: Fri, 5 Jul 2024 11:15:59 -0400 Subject: [PATCH] Allow multiple targets on command line This addresses https://github.com/incu6us/goimports-reviser/issues/96 - iterate over the list of remaining arguments treating each as an origin path - fully remove support for the -filePath option - defer printing deprecation warnings until after all paths are processed - print deprecation warnings to stderr, not stdout --- README.md | 5 ++ main.go | 196 +++++++++++++++++++++++++++--------------------------- 2 files changed, 103 insertions(+), 98 deletions(-) diff --git a/README.md b/README.md index 047a485..f0d03f4 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,11 @@ goimports-reviser -rm-unused -set-alias -format -recursive reviser goimports-reviser -rm-unused -set-alias -format ./... ``` +You can also apply rules to multiple targets: +```bash +goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go ./pkg/... +``` + ### Example, to configure it with JetBrains IDEs (via file watcher plugin): ![example](./images/image.png) diff --git a/main.go b/main.go index b376175..b62f15a 100644 --- a/main.go +++ b/main.go @@ -205,20 +205,24 @@ func main() { return } - originPath := flag.Arg(0) + originPaths := flag.Args() + if filePath != "" { - deprecatedMessagesCh <- fmt.Sprintf("-%s is deprecated. Put file name as last argument to the command(Example: goimports-reviser -rm-unused -set-alias -format goimports-reviser/main.go)", filePathArg) - originPath = filePath + deprecatedMessagesCh <- fmt.Sprintf("-%s is deprecated. Put file name(s) as last argument to the command(Example: goimports-reviser -rm-unused -set-alias -format goimports-reviser/main.go)", filePathArg) + originPaths = append(originPaths, filePath) } - if originPath == "" { - originPath = reviser.StandardInput + var originPath string + if len(originPaths) == 0 || (len(originPaths) == 1 && originPaths[0] == "-") { + originPaths[0] = reviser.StandardInput } - if err := validateRequiredParam(originPath); err != nil { - fmt.Printf("%s\n\n", err) - printUsage() - os.Exit(1) + for _, originPath = range originPaths { + if err := validateRequiredParam(originPath); err != nil { + fmt.Printf("%s\n\n", err) + printUsage() + os.Exit(1) + } } var options reviser.SourceFileOptions @@ -267,108 +271,110 @@ func main() { } close(deprecatedMessagesCh) - - if _, ok := reviser.IsDir(originPath); ok { - if *listFileName { - unformattedFiles, err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Find(options...) + var hasChange bool + log.Printf("Paths: %v\n", originPaths) + for _, originPath = range originPaths { + log.Printf("Processing %s\n", originPath) + if _, ok := reviser.IsDir(originPath); ok { + if *listFileName { + unformattedFiles, err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Find(options...) + if err != nil { + log.Fatalf("Failed to find unformatted files %s: %+v\n", originPath, err) + } + fmt.Printf("%s\n", unformattedFiles.String()) + continue + } + err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Fix(options...) if err != nil { - log.Fatalf("Failed to find unformatted files %s: %+v\n", originPath, err) + log.Fatalf("Failed to fix directory %s: %+v\n", originPath, err) } - fmt.Printf("%s\n", unformattedFiles.String()) - return - } - err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Fix(options...) - if err != nil { - log.Fatalf("Failed to fix directory %s: %+v\n", originPath, err) + continue } - return - } - if originPath != reviser.StandardInput { - originPath, err = filepath.Abs(originPath) - if err != nil { - log.Fatalf("Failed to get abs path: %+v\n", err) + if originPath != reviser.StandardInput { + originPath, err = filepath.Abs(originPath) + if err != nil { + log.Fatalf("Failed to get abs path: %+v\n", err) + } } - } - var formattedOutput []byte - var hasChange bool - if *isUseCache { - hash := md5.Sum([]byte(originPath)) + var formattedOutput []byte + var pathHasChange bool + if *isUseCache { + hash := md5.Sum([]byte(originPath)) - u, err := user.Current() - if err != nil { - log.Fatalf("Failed to get current user: %+v\n", err) - } - cacheDir := path.Join(u.HomeDir, ".cache", "goimports-reviser") - if err = os.MkdirAll(cacheDir, os.ModePerm); err != nil { - log.Fatalf("Failed to create cache directory: %+v\n", err) - } - cacheFile := path.Join(cacheDir, hex.EncodeToString(hash[:])) - - var cacheContent, fileContent []byte - if cacheContent, err = os.ReadFile(cacheFile); err == nil { - // compare file content hash - var fileHashHex string - if fileContent, err = os.ReadFile(originPath); err == nil { - fileHash := md5.Sum(fileContent) - fileHashHex = hex.EncodeToString(fileHash[:]) + u, err := user.Current() + if err != nil { + log.Fatalf("Failed to get current user: %+v\n", err) } - if string(cacheContent) == fileHashHex { - // point to cache - return + cacheDir := path.Join(u.HomeDir, ".cache", "goimports-reviser") + if err = os.MkdirAll(cacheDir, os.ModePerm); err != nil { + log.Fatalf("Failed to create cache directory: %+v\n", err) } - } - formattedOutput, _, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...) - if err != nil { - log.Fatalf("Failed to fix file: %+v\n", err) - } - fileHash := md5.Sum(formattedOutput) - fileHashHex := hex.EncodeToString(fileHash[:]) - if fileInfo, err := os.Stat(cacheFile); err != nil || fileInfo.IsDir() { - if _, err = os.Create(cacheFile); err != nil { - log.Fatalf("Failed to create cache file: %+v\n", err) + cacheFile := path.Join(cacheDir, hex.EncodeToString(hash[:])) + + var cacheContent, fileContent []byte + if cacheContent, err = os.ReadFile(cacheFile); err == nil { + // compare file content hash + var fileHashHex string + if fileContent, err = os.ReadFile(originPath); err == nil { + fileHash := md5.Sum(fileContent) + fileHashHex = hex.EncodeToString(fileHash[:]) + } + if string(cacheContent) == fileHashHex { + // point to cache + continue + } + } + formattedOutput, _, pathHasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...) + if err != nil { + log.Fatalf("Failed to fix file: %+v\n", err) + } + fileHash := md5.Sum(formattedOutput) + fileHashHex := hex.EncodeToString(fileHash[:]) + if fileInfo, err := os.Stat(cacheFile); err != nil || fileInfo.IsDir() { + if _, err = os.Create(cacheFile); err != nil { + log.Fatalf("Failed to create cache file: %+v\n", err) + } + } + file, _ := os.OpenFile(cacheFile, os.O_RDWR, os.ModePerm) + defer func() { + _ = file.Close() + }() + if err = file.Truncate(0); err != nil { + log.Fatalf("Failed file truncate: %+v\n", err) + } + if _, err = file.Seek(0, 0); err != nil { + log.Fatalf("Failed file seek: %+v\n", err) + } + if _, err = file.WriteString(fileHashHex); err != nil { + log.Fatalf("Failed to write file hash: %+v\n", err) + } + } else { + formattedOutput, _, pathHasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...) + if err != nil { + log.Fatalf("Failed to fix file: %+v\n", err) } } - file, _ := os.OpenFile(cacheFile, os.O_RDWR, os.ModePerm) - defer func() { - _ = file.Close() - }() - if err = file.Truncate(0); err != nil { - log.Fatalf("Failed file truncate: %+v\n", err) - } - if _, err = file.Seek(0, 0); err != nil { - log.Fatalf("Failed file seek: %+v\n", err) - } - if _, err = file.WriteString(fileHashHex); err != nil { - log.Fatalf("Failed to write file hash: %+v\n", err) - } - } else { - formattedOutput, _, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...) - if err != nil { - log.Fatalf("Failed to fix file: %+v\n", err) + if !hasChange && pathHasChange { + hasChange = pathHasChange } - } - resultPostProcess(hasChange, deprecatedMessagesCh, originPath, formattedOutput) + resultPostProcess(hasChange, originPath, formattedOutput) + } + printDeprecations(deprecatedMessagesCh) + if hasChange && *setExitStatus { + os.Exit(1) + } } -func resultPostProcess(hasChange bool, deprecatedMessagesCh chan string, originFilePath string, formattedOutput []byte) { - if !hasChange && *listFileName { - printDeprecations(deprecatedMessagesCh) - return - } +func resultPostProcess(hasChange bool, originFilePath string, formattedOutput []byte) { switch { case hasChange && *listFileName && output != "write": fmt.Println(originFilePath) case output == "stdout" || originFilePath == reviser.StandardInput: fmt.Print(string(formattedOutput)) case output == "file" || output == "write": - if !hasChange { - printDeprecations(deprecatedMessagesCh) - return - } - if err := os.WriteFile(originFilePath, formattedOutput, 0o644); err != nil { log.Fatalf("failed to write fixed result to file(%s): %+v\n", originFilePath, err) } @@ -378,12 +384,6 @@ func resultPostProcess(hasChange bool, deprecatedMessagesCh chan string, originF default: log.Fatalf(`invalid output %q specified`, output) } - - if hasChange && *setExitStatus { - os.Exit(1) - } - - printDeprecations(deprecatedMessagesCh) } func validateRequiredParam(filePath string) error { @@ -401,10 +401,10 @@ func printDeprecations(deprecatedMessagesCh chan string) { var hasDeprecations bool for deprecatedMessage := range deprecatedMessagesCh { hasDeprecations = true - fmt.Printf("%s\n", deprecatedMessage) + log.Printf("%s\n", deprecatedMessage) } if hasDeprecations { - fmt.Printf("All changes to file are applied, but command-line syntax should be fixed\n") + log.Printf("All changes to file are applied, but command-line syntax should be fixed\n") os.Exit(1) } }