github.com/uhthomas/helm@v3.0.0-beta.3+incompatible/pkg/action/output.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package action
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/gosuri/uitable"
    24  	"sigs.k8s.io/yaml"
    25  )
    26  
    27  // OutputFormat is a type for capturing supported output formats
    28  type OutputFormat string
    29  
    30  // TableFunc is a function that can be used to add rows to a table
    31  type TableFunc func(tbl *uitable.Table)
    32  
    33  const (
    34  	Table OutputFormat = "table"
    35  	JSON  OutputFormat = "json"
    36  	YAML  OutputFormat = "yaml"
    37  )
    38  
    39  // ErrInvalidFormatType is returned when an unsupported format type is used
    40  var ErrInvalidFormatType = fmt.Errorf("invalid format type")
    41  
    42  // String returns the string reprsentation of the OutputFormat
    43  func (o OutputFormat) String() string {
    44  	return string(o)
    45  }
    46  
    47  // Marshal uses the specified output format to marshal out the given data. It
    48  // does not support tabular output. For tabular output, use MarshalTable
    49  func (o OutputFormat) Marshal(data interface{}) (byt []byte, err error) {
    50  	switch o {
    51  	case YAML:
    52  		byt, err = yaml.Marshal(data)
    53  	case JSON:
    54  		byt, err = json.Marshal(data)
    55  	default:
    56  		err = ErrInvalidFormatType
    57  	}
    58  	return
    59  }
    60  
    61  // MarshalTable returns a formatted table using the given headers. Rows can be
    62  // added to the table using the given TableFunc
    63  func (o OutputFormat) MarshalTable(f TableFunc) ([]byte, error) {
    64  	if o != Table {
    65  		return nil, ErrInvalidFormatType
    66  	}
    67  	tbl := uitable.New()
    68  	if f == nil {
    69  		return []byte{}, nil
    70  	}
    71  	f(tbl)
    72  	return tbl.Bytes(), nil
    73  }
    74  
    75  // ParseOutputFormat takes a raw string and returns the matching OutputFormat.
    76  // If the format does not exists, ErrInvalidFormatType is returned
    77  func ParseOutputFormat(s string) (out OutputFormat, err error) {
    78  	switch s {
    79  	case Table.String():
    80  		out, err = Table, nil
    81  	case JSON.String():
    82  		out, err = JSON, nil
    83  	case YAML.String():
    84  		out, err = YAML, nil
    85  	default:
    86  		out, err = "", ErrInvalidFormatType
    87  	}
    88  	return
    89  }