From e55c55730eb7554180a49a14db4b23e2a9f8dd32 Mon Sep 17 00:00:00 2001 From: bfbonatto Date: Fri, 21 Jun 2024 03:46:28 -0300 Subject: [PATCH] feat: read from stdin, write to stdout (#1831) (#1832) Co-authored-by: Bruno Bonatto --- cmd/swag/main.go | 31 +++++++++++++++++++++++++++---- format/format.go | 21 ++++++++++++++++++++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/cmd/swag/main.go b/cmd/swag/main.go index 9848bfd77..6c997b971 100644 --- a/cmd/swag/main.go +++ b/cmd/swag/main.go @@ -18,6 +18,7 @@ const ( searchDirFlag = "dir" excludeFlag = "exclude" generalInfoFlag = "generalInfo" + pipeFlag = "pipe" propertyStrategyFlag = "propertyStrategy" outputFlag = "output" outputTypesFlag = "outputTypes" @@ -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", + ) } 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], + ) } outputTypes := strings.Split(ctx.String(outputTypesFlag), ",") @@ -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), + ) if collectionFormat == "" { - return fmt.Errorf("not supported %s collectionFormat", ctx.String(collectionFormat)) + return fmt.Errorf( + "not supported %s collectionFormat", + ctx.String(collectionFormat), + ) } var pdv = ctx.Int(parseDependencyLevelFlag) @@ -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) + } + searchDir := c.String(searchDirFlag) excludeDir := c.String(excludeFlag) mainFile := c.String(generalInfoFlag) @@ -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.", + }, }, }, } diff --git a/format/format.go b/format/format.go index 0b9dbfc2d..01761ed3c 100644 --- a/format/format.go +++ b/format/format.go @@ -3,6 +3,7 @@ package format import ( "bytes" "fmt" + "io" "os" "path/filepath" "strings" @@ -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 { @@ -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 + } + result, err := f.formatter.Format("", contents) + if err != nil { + return err + } + r := bytes.NewReader(result) + if _, err := io.Copy(dst, r); err != nil { + return err + } + return nil +}