Skip to content

Commit

Permalink
feat: default to standard output for the merge command (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored May 9, 2024
1 parent e2e1197 commit 26043bf
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions tools/cli/internal/cli/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package merge

import (
"bytes"
"encoding/json"
"fmt"
"log"

Expand All @@ -25,10 +27,6 @@ import (
"github.com/spf13/cobra"
)

const (
DefaultOutputFileName = "FOAS.json"
)

type Opts struct {
Merger openapi.Merger
fs afero.Fs
Expand All @@ -48,6 +46,10 @@ func (o *Opts) Run() error {
return err
}

if o.outputPath == "" {
return prettyPrintJson(federatedBytes)
}

return o.saveFile(federatedBytes)
}

Expand All @@ -65,6 +67,15 @@ func (o *Opts) PreRunE(_ []string) error {
return err
}

func prettyPrintJson(jsonBytes []byte) error {

Check failure on line 70 in tools/cli/internal/cli/merge/merge.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func prettyPrintJson should be prettyPrintJSON (revive)
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, jsonBytes, "", " "); err != nil {
return err
}
fmt.Println(prettyJSON.String())
return nil
}

func (o *Opts) saveFile(data []byte) error {
if err := afero.WriteFile(o.fs, o.outputPath, data, 0o600); err != nil {
return err
Expand Down Expand Up @@ -95,6 +106,6 @@ func Builder() *cobra.Command {

cmd.Flags().StringVarP(&opts.basePath, flag.Base, flag.BaseShort, "", usage.Base)
cmd.Flags().StringArrayVarP(&opts.externalPaths, flag.External, flag.ExternalShort, nil, usage.External)
cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, DefaultOutputFileName, usage.Output)
cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output)
return cmd
}

0 comments on commit 26043bf

Please sign in to comment.