Skip to content

Commit

Permalink
feat: read from stdin, write to stdout (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Bonatto committed Jun 20, 2024
1 parent 937c239 commit 8800568
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
31 changes: 27 additions & 4 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
searchDirFlag = "dir"
excludeFlag = "exclude"
generalInfoFlag = "generalInfo"
pipeFlag = "pipe"
propertyStrategyFlag = "propertyStrategy"
outputFlag = "output"
outputTypesFlag = "outputTypes"
Expand Down Expand Up @@ -195,11 +196,17 @@ func initAction(ctx *cli.Context) error {
if ctx.IsSet(templateDelimsFlag) {
delims := strings.Split(ctx.String(templateDelimsFlag), ",")
if len(delims) != 2 {
return fmt.Errorf("exactly two template delimiters must be provided, comma separated")
return fmt.Errorf(
"exactly two template delimiters must be provided, comma separated",
)

Check warning on line 201 in cmd/swag/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/swag/main.go#L199-L201

Added lines #L199 - L201 were not covered by tests
} else if delims[0] == delims[1] {
return fmt.Errorf("template delimiters must be different")
}
leftDelim, rightDelim = strings.TrimSpace(delims[0]), strings.TrimSpace(delims[1])
leftDelim, rightDelim = strings.TrimSpace(
delims[0],
), strings.TrimSpace(
delims[1],
)

Check warning on line 209 in cmd/swag/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/swag/main.go#L205-L209

Added lines #L205 - L209 were not covered by tests
}

outputTypes := strings.Split(ctx.String(outputTypesFlag), ",")
Expand All @@ -211,9 +218,14 @@ func initAction(ctx *cli.Context) error {
logger = log.New(io.Discard, "", log.LstdFlags)
}

collectionFormat := swag.TransToValidCollectionFormat(ctx.String(collectionFormatFlag))
collectionFormat := swag.TransToValidCollectionFormat(
ctx.String(collectionFormatFlag),
)

Check warning on line 223 in cmd/swag/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/swag/main.go#L221-L223

Added lines #L221 - L223 were not covered by tests
if collectionFormat == "" {
return fmt.Errorf("not supported %s collectionFormat", ctx.String(collectionFormat))
return fmt.Errorf(
"not supported %s collectionFormat",
ctx.String(collectionFormat),
)

Check warning on line 228 in cmd/swag/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/swag/main.go#L225-L228

Added lines #L225 - L228 were not covered by tests
}

var pdv = ctx.Int(parseDependencyLevelFlag)
Expand Down Expand Up @@ -269,6 +281,11 @@ func main() {
Aliases: []string{"f"},
Usage: "format swag comments",
Action: func(c *cli.Context) error {

if c.Bool(pipeFlag) {
return format.New().Run(os.Stdin, os.Stdout)

Check warning on line 286 in cmd/swag/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/swag/main.go#L285-L286

Added lines #L285 - L286 were not covered by tests
}

searchDir := c.String(searchDirFlag)
excludeDir := c.String(excludeFlag)
mainFile := c.String(generalInfoFlag)
Expand Down Expand Up @@ -296,6 +313,12 @@ func main() {
Value: "main.go",
Usage: "Go file path in which 'swagger general API Info' is written",
},
&cli.BoolFlag{
Name: "pipe",
Aliases: []string{"p"},
Value: false,
Usage: "Read from stdin, write to stdout.",
},
},
},
}
Expand Down
21 changes: 20 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package format
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -81,7 +82,8 @@ func (f *Format) visit(path string, fileInfo os.FileInfo, err error) error {

func (f *Format) excludeDir(path string) bool {
return f.exclude[path] ||
filepath.Base(path)[0] == '.' && len(filepath.Base(path)) > 1 // exclude hidden folders
filepath.Base(path)[0] == '.' &&
len(filepath.Base(path)) > 1 // exclude hidden folders
}

func (f *Format) excludeFile(path string) bool {
Expand Down Expand Up @@ -127,3 +129,20 @@ func write(path string, contents []byte) error {
}
return os.Rename(f.Name(), path)
}

// Run the format on src and write the result to dst.
func (f *Format) Run(src io.Reader, dst io.Writer) error {
contents, err := io.ReadAll(src)
if err != nil {
return err

Check warning on line 137 in format/format.go

View check run for this annotation

Codecov / codecov/patch

format/format.go#L134-L137

Added lines #L134 - L137 were not covered by tests
}
result, err := f.formatter.Format("", contents)
if err != nil {
return err

Check warning on line 141 in format/format.go

View check run for this annotation

Codecov / codecov/patch

format/format.go#L139-L141

Added lines #L139 - L141 were not covered by tests
}
r := bytes.NewReader(result)
if _, err := io.Copy(dst, r); err != nil {
return err

Check warning on line 145 in format/format.go

View check run for this annotation

Codecov / codecov/patch

format/format.go#L143-L145

Added lines #L143 - L145 were not covered by tests
}
return nil

Check warning on line 147 in format/format.go

View check run for this annotation

Codecov / codecov/patch

format/format.go#L147

Added line #L147 was not covered by tests
}

0 comments on commit 8800568

Please sign in to comment.