Skip to content

Commit

Permalink
Add --output-file and pretty-print option
Browse files Browse the repository at this point in the history
Allow local file as input for release index and corresponding
release metadata
  • Loading branch information
sinnykumari committed Jul 9, 2019
1 parent 02f89c9 commit 1da0423
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
67 changes: 52 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"time"
)
Expand Down Expand Up @@ -168,6 +169,10 @@ func run() error {
flag.StringVar(&releasesURL, "releases", "", "Release index location for the required stream")
var overrideFilename string
flag.StringVar(&overrideFilename, "override", "", "Override file location for the required stream")
var outputFile string
flag.StringVar(&outputFile, "output-file", "", "Save output into a file")
var prettyPrint bool
flag.BoolVar(&prettyPrint, "pretty-print", false, "Pretty-print output")

flag.Parse()

Expand All @@ -176,20 +181,36 @@ func run() error {
}

releasePath, err := ReleaseURL(releasesURL)

if err != nil {
return fmt.Errorf("Error with Release Index: %v", err)
}

resp, err := http.Get(releasePath)
parsedURL, err := url.Parse(releasePath)
if err != nil {
return fmt.Errorf("Error while fetching: %v", err)
return fmt.Errorf("Error while parsing release path: %v", err)
}

defer resp.Body.Close()
var decoder *json.Decoder
if parsedURL.Scheme == "" {
// It is most likely a local file.
releasesMetadataFile, err := os.Open(releasePath)
if err != nil {
return fmt.Errorf("Error opening file: %v", err)
}

defer releasesMetadataFile.Close()
decoder = json.NewDecoder(releasesMetadataFile)
} else {
resp, err := http.Get(releasePath)
if err != nil {
return fmt.Errorf("Error while fetching: %v", err)
}

defer resp.Body.Close()
decoder = json.NewDecoder(resp.Body)
}

var release Release
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(&release); err != nil {
return fmt.Errorf("Error while decoding json: %v", err)
}
Expand All @@ -206,13 +227,6 @@ func run() error {
Architectures: streamArch,
}

streamFile, err := os.Create("stream.json")
if err != nil {
return fmt.Errorf("Can't open file: %v", err)
}

defer streamFile.Close()

if overrideFilename != "" {
overrideFile, err := os.Open(overrideFilename)
if err != nil {
Expand Down Expand Up @@ -247,11 +261,34 @@ func run() error {
return fmt.Errorf("Error during Unmarshal: %v", err)
}
}
// Save final json into file
encoder := json.NewEncoder(streamFile)
if err = encoder.Encode(&streamMetadata); err != nil {

// If outputFile option not specified print on stdout
if outputFile != "" {
streamFile, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("Can't open file: %v", err)
}

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
}

encoder := json.NewEncoder(os.Stdout)
if prettyPrint {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(&streamMetadata); err != nil {
return fmt.Errorf("Error while encoding: %v", err)
}

return nil
}

Expand Down
26 changes: 23 additions & 3 deletions release_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
)

// ReleaseIndex for accessing Release Index metadata
Expand All @@ -19,13 +21,31 @@ type Index struct {
// ReleaseURL gets path for latest release.json available
func ReleaseURL(releaseIndexURL string) (string, error) {
var relIndex ReleaseIndex
resp, err := http.Get(releaseIndexURL)
parsedURL, err := url.Parse(releaseIndexURL)
if err != nil {
return "", err
}
defer resp.Body.Close()

decoder := json.NewDecoder(resp.Body)
var decoder *json.Decoder
if parsedURL.Scheme == "" {
// It is most likely a local file.
releases, err := os.Open(releaseIndexURL)
if err != nil {
return "", err
}

defer releases.Close()
decoder = json.NewDecoder(releases)
} else {
resp, err := http.Get(releaseIndexURL)
if err != nil {
return "", err
}

defer resp.Body.Close()
decoder = json.NewDecoder(resp.Body)
}

if err := decoder.Decode(&relIndex); err != nil {
return "", err
}
Expand Down

0 comments on commit 1da0423

Please sign in to comment.