Skip to content

Commit

Permalink
Merge pull request #29 from fgblomqvist/output-to-stdin
Browse files Browse the repository at this point in the history
Add option for output destination
  • Loading branch information
incu6us authored Oct 21, 2020
2 parents 2a888ec + 6279fd6 commit 573c4d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Usage of goimports-reviser:
Your project name(ex.: github.com/incu6us/goimports-reviser). Optional parameter.
-local string
Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.
-output string
Can be "file" or "stdout". Whether to write the formatted content back to the file or to stdout. Optional parameter. (default "file")
-rm-unused
Remove unused imports. Optional parameter.
-set-alias
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Tool for Golang to sort goimports by 3 groups: std, general and project dependencies.
// Tool for Golang to sort goimports by 3-4 groups: std, general, local(which is optional) and project dependencies.
// It will help you to keep your code cleaner.
//
// Example:
Expand Down
28 changes: 21 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
removeUnusedImportsArg = "rm-unused"
setAliasArg = "set-alias"
localPkgPrefixesArg = "local"
outputArg = "output"
)

// Project build specific vars
Expand All @@ -35,7 +36,7 @@ var (
shouldSetAlias *bool
)

var projectName, filePath, localPkgPrefixes string
var projectName, filePath, localPkgPrefixes, output string

func init() {
flag.StringVar(
Expand All @@ -56,7 +57,14 @@ func init() {
&localPkgPrefixes,
localPkgPrefixesArg,
"",
`Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.`,
"Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.",
)

flag.StringVar(
&output,
outputArg,
"file",
`Can be "file" or "stdout". Whether to write the formatted content back to the file or to stdout. Optional parameter.`,
)

shouldRemoveUnusedImports = flag.Bool(
Expand Down Expand Up @@ -135,12 +143,18 @@ func main() {
log.Fatalf("%+v", errors.WithStack(err))
}

if !hasChange {
return
}
if output == "stdout" {
fmt.Print(string(formattedOutput))
} else if output == "file" {
if !hasChange {
return
}

if err := ioutil.WriteFile(filePath, formattedOutput, 0644); err != nil {
log.Fatalf("failed to write fixed result to file(%s): %+v", filePath, errors.WithStack(err))
if err := ioutil.WriteFile(filePath, formattedOutput, 0644); err != nil {
log.Fatalf("failed to write fixed result to file(%s): %+v", filePath, errors.WithStack(err))
}
} else {
log.Fatalf(`invalid output "%s" specified`, output)
}
}

Expand Down

0 comments on commit 573c4d8

Please sign in to comment.