Skip to content

Commit

Permalink
Refactor - use out var to set where we want output
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnykumari committed Jul 11, 2019
1 parent e1b577b commit a9b28c8
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -266,6 +267,7 @@ func run() error {
}
}

var out io.Writer
// If outputFile option not specified print on stdout
if outputFile != "" {
streamFile, err := os.Create(outputFile)
Expand All @@ -274,18 +276,12 @@ func run() error {
}

defer streamFile.Close()
// Save final json output into file
encoder := json.NewEncoder(streamFile)
if prettyPrint {
encoder.SetIndent("", " ")
}
if err = encoder.Encode(&streamMetadata); err != nil {
return fmt.Errorf("Error while encoding: %v", err)
}
return nil
out = streamFile
} else {
out = os.Stdout
}

encoder := json.NewEncoder(os.Stdout)
encoder := json.NewEncoder(out)
if prettyPrint {
encoder.SetIndent("", " ")
}
Expand Down

0 comments on commit a9b28c8

Please sign in to comment.