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

feat: read from stdin, write to stdout (#1831) #1832

Merged
merged 1 commit into from
Jun 21, 2024
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
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 @@
searchDirFlag = "dir"
excludeFlag = "exclude"
generalInfoFlag = "generalInfo"
pipeFlag = "pipe"
propertyStrategyFlag = "propertyStrategy"
outputFlag = "output"
outputTypesFlag = "outputTypes"
Expand Down Expand Up @@ -195,11 +196,17 @@
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 @@
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 @@
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 @@
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 @@
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -81,7 +82,8 @@

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 @@
}
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
}
Loading