Removed usage of log

This commit is contained in:
mfarah 2015-10-06 16:39:19 +11:00
parent 1d5fbd5ad0
commit 955ecc2547
2 changed files with 12 additions and 9 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
// "fmt" // "fmt"
"log"
"strconv" "strconv"
) )
@ -48,7 +47,7 @@ func recurse(value interface{}, head string, tail []string) interface{} {
} }
index, err := strconv.ParseInt(head, 10, 64) index, err := strconv.ParseInt(head, 10, 64)
if err != nil { if err != nil {
log.Fatalf("Error accessing array: %v", err) die("Error accessing array: %v", err)
} }
return readArray(value.([]interface{}), index, tail) return readArray(value.([]interface{}), index, tail)
case map[interface{}]interface{}: case map[interface{}]interface{}:

18
yaml.go
View File

@ -5,7 +5,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -68,7 +67,7 @@ func readProperty(cmd *cobra.Command, args []string) {
func writeProperty(cmd *cobra.Command, args []string) { func writeProperty(cmd *cobra.Command, args []string) {
if len(args) < 3 { if len(args) < 3 {
log.Fatalf("Must provide <filename> <path_to_update> <value>") die("Must provide <filename> <path_to_update> <value>")
} }
var parsedData map[interface{}]interface{} var parsedData map[interface{}]interface{}
@ -108,7 +107,7 @@ func printYaml(context interface{}) {
func yamlToString(context interface{}) string { func yamlToString(context interface{}) string {
out, err := yaml.Marshal(context) out, err := yaml.Marshal(context)
if err != nil { if err != nil {
log.Fatalf("error printing yaml: %v", err) die("error printing yaml: %v", err)
} }
outStr := string(out) outStr := string(out)
// trim the trailing new line as it's easier for a script to add // trim the trailing new line as it's easier for a script to add
@ -121,7 +120,7 @@ func yamlToString(context interface{}) string {
func readYaml(args []string, parsedData *map[interface{}]interface{}) { func readYaml(args []string, parsedData *map[interface{}]interface{}) {
if len(args) == 0 { if len(args) == 0 {
log.Fatalf("Must provide filename") die("Must provide filename")
} }
var rawData []byte var rawData []byte
@ -133,14 +132,14 @@ func readYaml(args []string, parsedData *map[interface{}]interface{}) {
err := yaml.Unmarshal([]byte(rawData), &parsedData) err := yaml.Unmarshal([]byte(rawData), &parsedData)
if err != nil { if err != nil {
log.Fatalf("error: %v", err) die("error: %v", err)
} }
} }
func readStdin() []byte { func readStdin() []byte {
bytes, err := ioutil.ReadAll(os.Stdin) bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil { if err != nil {
log.Fatalf("error reading stdin", err) die("error reading stdin", err)
} }
return bytes return bytes
} }
@ -148,7 +147,12 @@ func readStdin() []byte {
func readFile(filename string) []byte { func readFile(filename string) []byte {
var rawData, readError = ioutil.ReadFile(filename) var rawData, readError = ioutil.ReadFile(filename)
if readError != nil { if readError != nil {
log.Fatalf("error: %v", readError) die("error: %v", readError)
} }
return rawData return rawData
} }
func die(message ...interface{}) {
fmt.Println(message)
os.Exit(1)
}