Skip to content

Commit e55c557

Browse files
bfbonattoBruno Bonatto
and
Bruno Bonatto
authored
feat: read from stdin, write to stdout (#1831) (#1832)
Co-authored-by: Bruno Bonatto <bruno.bonatto@simfrete.com.br>
1 parent fd2fa83 commit e55c557

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

cmd/swag/main.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
searchDirFlag = "dir"
1919
excludeFlag = "exclude"
2020
generalInfoFlag = "generalInfo"
21+
pipeFlag = "pipe"
2122
propertyStrategyFlag = "propertyStrategy"
2223
outputFlag = "output"
2324
outputTypesFlag = "outputTypes"
@@ -195,11 +196,17 @@ func initAction(ctx *cli.Context) error {
195196
if ctx.IsSet(templateDelimsFlag) {
196197
delims := strings.Split(ctx.String(templateDelimsFlag), ",")
197198
if len(delims) != 2 {
198-
return fmt.Errorf("exactly two template delimiters must be provided, comma separated")
199+
return fmt.Errorf(
200+
"exactly two template delimiters must be provided, comma separated",
201+
)
199202
} else if delims[0] == delims[1] {
200203
return fmt.Errorf("template delimiters must be different")
201204
}
202-
leftDelim, rightDelim = strings.TrimSpace(delims[0]), strings.TrimSpace(delims[1])
205+
leftDelim, rightDelim = strings.TrimSpace(
206+
delims[0],
207+
), strings.TrimSpace(
208+
delims[1],
209+
)
203210
}
204211

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

214-
collectionFormat := swag.TransToValidCollectionFormat(ctx.String(collectionFormatFlag))
221+
collectionFormat := swag.TransToValidCollectionFormat(
222+
ctx.String(collectionFormatFlag),
223+
)
215224
if collectionFormat == "" {
216-
return fmt.Errorf("not supported %s collectionFormat", ctx.String(collectionFormat))
225+
return fmt.Errorf(
226+
"not supported %s collectionFormat",
227+
ctx.String(collectionFormat),
228+
)
217229
}
218230

219231
var pdv = ctx.Int(parseDependencyLevelFlag)
@@ -269,6 +281,11 @@ func main() {
269281
Aliases: []string{"f"},
270282
Usage: "format swag comments",
271283
Action: func(c *cli.Context) error {
284+
285+
if c.Bool(pipeFlag) {
286+
return format.New().Run(os.Stdin, os.Stdout)
287+
}
288+
272289
searchDir := c.String(searchDirFlag)
273290
excludeDir := c.String(excludeFlag)
274291
mainFile := c.String(generalInfoFlag)
@@ -296,6 +313,12 @@ func main() {
296313
Value: "main.go",
297314
Usage: "Go file path in which 'swagger general API Info' is written",
298315
},
316+
&cli.BoolFlag{
317+
Name: "pipe",
318+
Aliases: []string{"p"},
319+
Value: false,
320+
Usage: "Read from stdin, write to stdout.",
321+
},
299322
},
300323
},
301324
}

format/format.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package format
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"os"
78
"path/filepath"
89
"strings"
@@ -81,7 +82,8 @@ func (f *Format) visit(path string, fileInfo os.FileInfo, err error) error {
8182

8283
func (f *Format) excludeDir(path string) bool {
8384
return f.exclude[path] ||
84-
filepath.Base(path)[0] == '.' && len(filepath.Base(path)) > 1 // exclude hidden folders
85+
filepath.Base(path)[0] == '.' &&
86+
len(filepath.Base(path)) > 1 // exclude hidden folders
8587
}
8688

8789
func (f *Format) excludeFile(path string) bool {
@@ -127,3 +129,20 @@ func write(path string, contents []byte) error {
127129
}
128130
return os.Rename(f.Name(), path)
129131
}
132+
133+
// Run the format on src and write the result to dst.
134+
func (f *Format) Run(src io.Reader, dst io.Writer) error {
135+
contents, err := io.ReadAll(src)
136+
if err != nil {
137+
return err
138+
}
139+
result, err := f.formatter.Format("", contents)
140+
if err != nil {
141+
return err
142+
}
143+
r := bytes.NewReader(result)
144+
if _, err := io.Copy(dst, r); err != nil {
145+
return err
146+
}
147+
return nil
148+
}

0 commit comments

Comments
 (0)