github.com/microsoft/moc@v0.17.1/pkg/config/config.go (about)

     1  // Copyright (c) Microsoft Corporation. All rights reserved.
     2  // Licensed under the Apache v2.0 license.
     3  
     4  package config
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  	"reflect"
    10  
    11  	"github.com/jmespath/go-jmespath"
    12  	"github.com/microsoft/moc/pkg/marshal"
    13  )
    14  
    15  // Load the virtual machine configuration from the specified path
    16  func LoadYAMLFile(path string, outData interface{}) (err error) {
    17  	err = marshal.FromYAMLFile(path, outData)
    18  	if err != nil {
    19  		return
    20  	}
    21  
    22  	return
    23  }
    24  
    25  // Load the virtual machine configuration from the specified path
    26  func LoadYAMLConfig(data string, outData interface{}) (err error) {
    27  	err = marshal.FromYAMLString(data, outData)
    28  	if err != nil {
    29  		return
    30  	}
    31  
    32  	return
    33  }
    34  
    35  // PrintYAML
    36  func PrintYAML(data interface{}) {
    37  	str, err := marshal.ToYAML(data)
    38  	if err != nil {
    39  		return
    40  	}
    41  	fmt.Printf("%s", str)
    42  }
    43  
    44  // PrintYAMLList
    45  func PrintYAMLList(datasets interface{}) {
    46  	items := reflect.ValueOf(datasets)
    47  	if items.Kind() == reflect.Slice {
    48  		for i := 0; i < items.Len(); i++ {
    49  			PrintYAML(items.Index(i).Interface())
    50  		}
    51  	}
    52  }
    53  
    54  // PrintJSON
    55  func PrintJSON(data interface{}) {
    56  	str, err := marshal.ToJSON(data)
    57  	if err != nil {
    58  		return
    59  	}
    60  	fmt.Printf("%s", str)
    61  }
    62  
    63  // PrintJSONList
    64  func PrintJSONList(datasets interface{}) {
    65  	items := reflect.ValueOf(datasets)
    66  	if items.Kind() == reflect.Slice {
    67  		for i := 0; i < items.Len(); i++ {
    68  			PrintJSON(items.Index(i).Interface())
    69  		}
    70  	}
    71  }
    72  
    73  func printHeader(data reflect.Value) {
    74  	item := reflect.Indirect(data)
    75  	for i := 0; i < item.NumField(); i++ {
    76  		// For now, printing only string elements
    77  		if item.Field(i).Kind() == reflect.String {
    78  			fmt.Printf("%s ", item.Type().Field(i).Name)
    79  		}
    80  	}
    81  	fmt.Println()
    82  }
    83  
    84  func printElement(data reflect.Value) {
    85  	items := reflect.Indirect(data)
    86  	for i := 0; i < items.NumField(); i++ {
    87  		// For now, printing only string elements
    88  		if items.Field(i).Kind() == reflect.String {
    89  			fmt.Printf("%s ", items.Field(i).Interface())
    90  		}
    91  	}
    92  	fmt.Println()
    93  }
    94  
    95  // PrintTable
    96  func PrintTable(datasets interface{}) {
    97  	items := reflect.ValueOf(datasets)
    98  	if items.Kind() == reflect.Slice && items.Len() > 0 {
    99  		printHeader(items.Index(0))
   100  		for i := 0; i < items.Len(); i++ {
   101  			printElement(items.Index(i))
   102  		}
   103  	}
   104  
   105  }
   106  
   107  // Load the secret configuration from the specified path
   108  func LoadValueFile(path string) (*string, error) {
   109  	contents, err := ioutil.ReadFile(path)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	value := string(contents)
   114  
   115  	return &value, nil
   116  }
   117  
   118  func ExportFormatList(datasets interface{}, path string, query string, outputType string) error {
   119  	var fileToWrite string
   120  
   121  	marshaledByte, err := MarshalOutput(datasets, query, outputType)
   122  	if err != nil {
   123  		fmt.Printf("%v", err)
   124  		return err
   125  	}
   126  	fileToWrite += string(marshaledByte)
   127  
   128  	err = ioutil.WriteFile(
   129  		path,
   130  		[]byte(fileToWrite),
   131  		0644)
   132  	return err
   133  }
   134  
   135  func PrintFormat(data interface{}, query string, outputType string) {
   136  	marshaledByte, err := MarshalOutput(data, query, outputType)
   137  	if err != nil {
   138  		fmt.Printf("%v", err)
   139  		return
   140  	}
   141  	fmt.Printf("%s\n", string(marshaledByte))
   142  }
   143  
   144  func PrintFormatList(datasets interface{}, query string, outputType string) {
   145  	PrintFormat(datasets, query, outputType)
   146  }
   147  
   148  func MarshalOutput(data interface{}, query string, outputType string) ([]byte, error) {
   149  	var queryTarget interface{}
   150  	var result interface{}
   151  	var err error
   152  
   153  	jsonByte, err := marshal.ToJSONBytes(data)
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  	marshal.FromJSONBytes(jsonByte, &queryTarget)
   158  	if query != "" {
   159  		result, err = jmespath.Search(query, queryTarget)
   160  		if err != nil {
   161  			return nil, err
   162  		}
   163  	} else {
   164  		result = queryTarget
   165  	}
   166  
   167  	var marshaledByte []byte
   168  	if outputType == "json" {
   169  		marshaledByte, err = marshal.ToJSONBytes(result)
   170  	} else if outputType == "ppjson" {
   171  		marshaledByte, err = marshal.ToPrettyPrintedJSONBytes(result)
   172  	} else if outputType == "tsv" {
   173  		marshaledByte, err = marshal.ToTSVBytes(result)
   174  	} else if outputType == "csv" {
   175  		marshaledByte, err = marshal.ToCSVBytes(result)
   176  	} else {
   177  		marshaledByte, err = marshal.ToYAMLBytes(result)
   178  	}
   179  
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	return marshaledByte, nil
   185  }