Skip to content

Commit

Permalink
Clean up error printing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlloyd committed Jan 27, 2018
1 parent d8285b9 commit 5462c06
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions cmd/rif.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package main

import (
"fmt"
"github.com/docopt/docopt-go"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/docopt/docopt-go"

"github.com/turingincomplete/rif/internal/app/variables"
"github.com/turingincomplete/rif/internal/pkg/rif2req"
"github.com/turingincomplete/rif/internal/pkg/templating"
Expand Down Expand Up @@ -82,74 +83,63 @@ func main() {

rawFile, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading .rif file: %s\n", err.Error())
os.Exit(1)
errorAndExit("Error reading .rif file", err)
}

rFile := rifYamlFile{}
err = yaml.Unmarshal(rawFile, &rFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing .rif file: %s\n", err.Error())
os.Exit(1)
errorAndExit("Error parsing .rif file", err)
}

// Work out variable values
rawVars := arguments["<variable>"].([]string)
inputVars, err := parseInputVars(rawVars)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
errorAndExit("Error parsing variables", err)
}

varDefinitions, err := preprocessVariableDefinitions(rFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
errorAndExit("Invalid variable definition", err)
}

varMap, err := variables.MakeMap(varDefinitions, inputVars)
if err != nil {
fmt.Fprintf(os.Stderr, "Encountered an error calculating template variables: %s\n", err.Error())
os.Exit(1)
errorAndExit("Encountered an error calculating template variables", err)
}

// Apply template substitutions
urlTemplate, err := templating.Parse(rFile.URL)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading URL template: %s", err.Error())
os.Exit(1)
errorAndExit("Error reading URL template", err)
}
rFile.URL, err = urlTemplate(varMap)
if err != nil {
fmt.Fprintf(os.Stderr, "Error rendering URL template: %s", err.Error())
os.Exit(1)
errorAndExit("Error rendering URL template", err)
}

newHeaders := map[string]string{}
for headerName, headerValue := range rFile.Headers {
headerTemplate, err := templating.Parse(headerValue)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading header template: %s", err.Error())
os.Exit(1)
errorAndExit("Error reading header template", err)
}
renderedHeader, err := headerTemplate(varMap)
if err != nil {
fmt.Fprintf(os.Stderr, "Error rendering header template: %s", err.Error())
os.Exit(1)
errorAndExit("Error rendering header template", err)
}
newHeaders[headerName] = renderedHeader
}
rFile.Headers = newHeaders

bodyTemplate, err := templating.Parse(rFile.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading body template: %s", err.Error())
os.Exit(1)
errorAndExit("Error reading body template", err)
}
rFile.Body, err = bodyTemplate(varMap)
if err != nil {
fmt.Fprintf(os.Stderr, "Error rendering body template: %s", err.Error())
os.Exit(1)
errorAndExit("Error rendering body template", err)
}

// Make the request
Expand All @@ -163,29 +153,31 @@ func main() {
version,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error making request: %s\n", err.Error())
os.Exit(1)
errorAndExit("Error making request", err)
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error making request: %s\n", err.Error())
os.Exit(1)
errorAndExit("Error making request", err)
}
defer func() {
_ = resp.Body.Close()
}()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error making request: %s\n", err.Error())
os.Exit(1)
errorAndExit("Error making request", err)
}

fmt.Println(string(body))
}

func errorAndExit(errPrefix string, err error) {
fmt.Fprintf(os.Stderr, "%s: %s\n", errPrefix, err.Error())
os.Exit(1)
}

func parseInputVars(rawVars []string) (map[string]string, error) {
vars := map[string]string{}

Expand Down

0 comments on commit 5462c06

Please sign in to comment.