github.com/openshift/installer@v1.4.17/pkg/explain/printer.go (about)

     1  package explain
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"sort"
     7  	"strings"
     8  
     9  	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    10  	"k8s.io/apimachinery/pkg/util/sets"
    11  )
    12  
    13  const (
    14  	fieldIndent     = 4
    15  	fieldDescIndent = 6
    16  )
    17  
    18  type printer struct {
    19  	Writer io.Writer
    20  }
    21  
    22  func (p printer) PrintKindAndVersion() {
    23  	io.WriteString(p.Writer, fmt.Sprintf("KIND:     %s\n", "InstallConfig"))
    24  	io.WriteString(p.Writer, fmt.Sprintf("VERSION:  %s\n\n", "v1"))
    25  }
    26  
    27  func (p printer) PrintResource(schema *apiextv1.JSONSchemaProps) {
    28  	resource := schema.Type
    29  	if schema.Items != nil && schema.Items.Schema != nil {
    30  		resource = fmt.Sprintf("[]%s", schema.Items.Schema.Type)
    31  	}
    32  	io.WriteString(p.Writer, fmt.Sprintf("RESOURCE: <%s>\n", resource))
    33  
    34  	p.printLabels(2, schema)
    35  
    36  	desc := schema.Description
    37  	if len(desc) == 0 {
    38  		desc = "<empty>"
    39  	}
    40  	write(2, p.Writer, desc)
    41  	io.WriteString(p.Writer, "\n")
    42  
    43  }
    44  
    45  func (p printer) PrintFields(schema *apiextv1.JSONSchemaProps) {
    46  	required := sets.NewString(schema.Required...)
    47  	properties := map[string]apiextv1.JSONSchemaProps{}
    48  	if schema.Items != nil && schema.Items.Schema != nil && len(schema.Items.Schema.Properties) > 0 {
    49  		properties = schema.Items.Schema.Properties
    50  		required.Insert(schema.Items.Schema.Required...)
    51  	}
    52  	if len(schema.Properties) > 0 {
    53  		properties = schema.Properties
    54  	}
    55  	if len(properties) == 0 {
    56  		return
    57  	}
    58  
    59  	var keys []string
    60  	for pname := range properties {
    61  		keys = append(keys, pname)
    62  	}
    63  	sort.Strings(keys)
    64  
    65  	io.WriteString(p.Writer, "FIELDS:\n")
    66  	for _, pname := range keys {
    67  		pschema := properties[pname]
    68  		p.printField(pname, required.Has(pname), &pschema)
    69  	}
    70  }
    71  
    72  func (p printer) printField(name string, required bool, schema *apiextv1.JSONSchemaProps) {
    73  	ftype := schema.Type
    74  	if schema.Items != nil && schema.Items.Schema != nil {
    75  		ftype = fmt.Sprintf("[]%s", schema.Items.Schema.Type)
    76  	}
    77  	title := fmt.Sprintf("%s <%s>", name, ftype)
    78  	if required {
    79  		title = fmt.Sprintf("%s -required-", title)
    80  	}
    81  	write(fieldIndent, p.Writer, title)
    82  
    83  	p.printLabels(fieldDescIndent, schema)
    84  
    85  	fdesc := schema.Description
    86  	if fdesc == "" {
    87  		fdesc = "<empty>"
    88  	}
    89  	write(6, p.Writer, fdesc)
    90  	if schema.Items != nil && schema.Items.Schema != nil && len(schema.Items.Schema.Description) > 0 {
    91  		write(6, p.Writer, schema.Items.Schema.Description)
    92  	}
    93  	io.WriteString(p.Writer, "\n")
    94  }
    95  
    96  func (p printer) printLabels(indentLevel int, schema *apiextv1.JSONSchemaProps) {
    97  	if schema.Default != nil {
    98  		write(indentLevel, p.Writer, fmt.Sprintf("Default: %s", defaultString(*schema.Default)))
    99  	}
   100  	if len(schema.Format) > 0 {
   101  		write(indentLevel, p.Writer, fmt.Sprintf("Format: %s", schema.Format))
   102  	}
   103  	if len(schema.Enum) > 0 {
   104  		write(indentLevel, p.Writer, fmt.Sprintf("Valid Values: %s", strings.Join(validValues(schema.Enum), ",")))
   105  	}
   106  }
   107  
   108  func write(indentLevel int, w io.Writer, s string) {
   109  	if strings.TrimSpace(s) == "" {
   110  		io.WriteString(w, "\n")
   111  	}
   112  	indent := ""
   113  	for i := 0; i < indentLevel; i++ {
   114  		indent = indent + " "
   115  	}
   116  	io.WriteString(w, indent+s+"\n")
   117  }
   118  
   119  func defaultString(obj apiextv1.JSON) string { return string(obj.Raw) }
   120  
   121  func validValues(objs []apiextv1.JSON) []string {
   122  	ret := make([]string, len(objs))
   123  	for idx, obj := range objs {
   124  		ret[idx] = string(obj.Raw)
   125  	}
   126  	return ret
   127  }