Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
feat: add helper method to unmarshal json or yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Dec 13, 2019
1 parent 8447da7 commit c773f73
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package utils

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/logrusorgru/aurora"
"gopkg.in/yaml.v2"
)

// ProcessCustomClaimInput take in the input from cmd flags which a map of strings
// and convert it to a map of interface
func ProcessCustomClaimInput(input map[string]string) map[string]interface{} {
Expand All @@ -25,3 +33,25 @@ func StdOutSuccess(format string, a ...interface{}) {
fmt.Fprintf(os.Stdout, "%s\n", m)
}

// UnmashalFormatFile read and unmashal either a json/yaml file into a struct
func UnmashalFormatFile(path string, extension string, v interface{}) error {
content, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("Error reading %s: %w", path, err)
}
switch extension {
case "yaml":
err = yaml.Unmarshal(content, v)
if err != nil {
return fmt.Errorf("Error decoding yaml: %w", err)
}
case "json":
err = json.Unmarshal(content, v)
if err != nil {
return fmt.Errorf("Error decoding json: %w", err)
}
default:
return fmt.Errorf("Unsupported file type")
}
return nil
}

0 comments on commit c773f73

Please sign in to comment.