This commit is contained in:
Madhan Raj Mookkandy 2020-09-03 23:37:55 -07:00
Родитель 8e71457b68
Коммит 439d8ee5a0
5 изменённых файлов: 264 добавлений и 0 удалений

Просмотреть файл

@ -31,6 +31,7 @@ test:
unittest:
$(GOTEST) ./pkg/marshal
$(GOTEST) ./pkg/config
generate: bootstrap
(cd rpc && ./gen_proto.sh)

1
go.mod
Просмотреть файл

@ -6,6 +6,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.3.2
github.com/google/go-cmp v0.5.0 // indirect
github.com/jmespath/go-jmespath v0.3.0
github.com/kr/pretty v0.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1

2
go.sum
Просмотреть файл

@ -13,6 +13,8 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

183
pkg/config/config.go Normal file
Просмотреть файл

@ -0,0 +1,183 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache v2.0 license.
package config
import (
"fmt"
"github.com/jmespath/go-jmespath"
"github.com/microsoft/moc/pkg/marshal"
"io/ioutil"
"reflect"
)
// Load the virtual machine configuration from the specified path
func LoadYAMLFile(path string, outData interface{}) (err error) {
err = marshal.FromYAMLFile(path, outData)
if err != nil {
return
}
return
}
// Load the virtual machine configuration from the specified path
func LoadYAMLConfig(data string, outData interface{}) (err error) {
err = marshal.FromYAMLString(data, outData)
if err != nil {
return
}
return
}
// PrintYAML
func PrintYAML(data interface{}) {
str, err := marshal.ToYAML(data)
if err != nil {
return
}
fmt.Printf("%s", str)
}
// PrintYAMLList
func PrintYAMLList(datasets interface{}) {
items := reflect.ValueOf(datasets)
if items.Kind() == reflect.Slice {
for i := 0; i < items.Len(); i++ {
PrintYAML(items.Index(i).Interface())
}
}
}
// PrintJSON
func PrintJSON(data interface{}) {
str, err := marshal.ToJSON(data)
if err != nil {
return
}
fmt.Printf("%s", str)
}
// PrintJSONList
func PrintJSONList(datasets interface{}) {
items := reflect.ValueOf(datasets)
if items.Kind() == reflect.Slice {
for i := 0; i < items.Len(); i++ {
PrintJSON(items.Index(i).Interface())
}
}
}
func printHeader(data reflect.Value) {
item := reflect.Indirect(data)
for i := 0; i < item.NumField(); i++ {
// For now, printing only string elements
if item.Field(i).Kind() == reflect.String {
fmt.Printf("%s ", item.Type().Field(i).Name)
}
}
fmt.Println()
}
func printElement(data reflect.Value) {
items := reflect.Indirect(data)
for i := 0; i < items.NumField(); i++ {
// For now, printing only string elements
if items.Field(i).Kind() == reflect.String {
fmt.Printf("%s ", items.Field(i).Interface())
}
}
fmt.Println()
}
// PrintTable
func PrintTable(datasets interface{}) {
items := reflect.ValueOf(datasets)
if items.Kind() == reflect.Slice && items.Len() > 0 {
printHeader(items.Index(0))
for i := 0; i < items.Len(); i++ {
printElement(items.Index(i))
}
}
}
// Load the secret configuration from the specified path
func LoadValueFile(path string) (*string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
value := string(contents)
return &value, nil
}
func ExportFormatList(datasets interface{}, path string, query string, outputType string) error {
var fileToWrite string
marshaledByte, err := marshalOutput(datasets, query, outputType)
if err != nil {
fmt.Printf("%v", err)
return err
}
fileToWrite += string(marshaledByte)
err = ioutil.WriteFile(
path,
[]byte(fileToWrite),
0644)
return err
}
func PrintFormat(data interface{}, query string, outputType string) {
marshaledByte, err := marshalOutput(data, query, outputType)
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Printf("%s\n", string(marshaledByte))
}
func PrintFormatList(datasets interface{}, query string, outputType string) {
PrintFormat(datasets, query, outputType)
}
func marshalOutput(data interface{}, query string, outputType string) ([]byte, error) {
var queryTarget interface{}
var result interface{}
var err error
jsonByte, err := marshal.ToJSONBytes(data)
if err != nil {
return nil, err
}
marshal.FromJSONBytes(jsonByte, &queryTarget)
if query != "" {
result, err = jmespath.Search(query, queryTarget)
if err != nil {
return nil, err
}
} else {
result = queryTarget
}
var marshaledByte []byte
if outputType == "json" {
marshaledByte, err = marshal.ToJSONBytes(result)
} else if outputType == "tsv" {
marshaledByte, err = marshal.ToTSVBytes(result)
} else if outputType == "csv" {
marshaledByte, err = marshal.ToCSVBytes(result)
} else {
marshaledByte, err = marshal.ToYAMLBytes(result)
}
if err != nil {
return nil, err
}
return marshaledByte, nil
}

77
pkg/config/config_test.go Normal file
Просмотреть файл

@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache v2.0 license.
package config
import "testing"
type SampleStruct struct {
TestString string `json:"testString,omitempty" yaml:testString,omitempty"`
TestString2 string `json:"testStrings,omitempty" yaml:testString,omitempty"`
TestInt int `json:"testInt,omitempty" yaml:"testInt,omitempty"`
TestArray []string `json:"testArray,omitempty" yaml:"testArray,omitempty"`
}
var tmp = SampleStruct{
TestString: "TestString",
TestString2: "TestString2",
TestInt: 1,
TestArray: []string{"TestArray"},
}
var tmpArray = []SampleStruct{
{
TestString: "test1String",
TestString2: "test1String2",
TestInt: 1,
TestArray: []string{"test1Array"},
},
{
TestString: "test2String",
TestString2: "test2String2",
TestInt: 2,
TestArray: []string{"test2Array"},
},
}
func Test_LoadYAMLConfig(t *testing.T) {
tmpString := `teststring: TestString
testInt: 1
testArray:
- TestArray`
tmpData := SampleStruct{}
err := LoadYAMLConfig(tmpString, &tmpData)
if err != nil {
t.Errorf("Failed to load Yaml Config" + err.Error())
}
}
func Test_PrintYAML(t *testing.T) {
tmp := SampleStruct{
TestString: "TestString",
TestInt: 1,
TestArray: []string{"TestArray"},
}
PrintYAML(tmp)
}
func Test_PrintJSON(t *testing.T) {
PrintJSON(tmp)
}
func Test_PrintYAMLList(t *testing.T) {
PrintYAMLList(tmpArray)
}
func Test_PrintJSONList(t *testing.T) {
PrintJSONList(tmpArray)
}
func Test_PrintTable(t *testing.T) {
PrintTable(tmpArray)
}
func Test_PrintFormat(t *testing.T) {
PrintFormat(tmp, "", "tsv")
PrintFormat(tmp, "", "csv")
}
func Test_PrintFormatList(t *testing.T) {
PrintFormatList(tmpArray, "", "tsv")
PrintFormatList(tmpArray, "", "csv")
}