github.com/oam-dev/kubevela@v1.9.11/references/docgen/console.go (about) 1 /* 2 Copyright 2021 The KubeVela 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 docgen 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/olekukonko/tablewriter" 24 25 "github.com/kubevela/workflow/pkg/cue/packages" 26 27 "github.com/oam-dev/kubevela/apis/types" 28 "github.com/oam-dev/kubevela/pkg/utils/common" 29 cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" 30 ) 31 32 // Int64Type is int64 type 33 type Int64Type = int64 34 35 // StringType is string type 36 type StringType = string 37 38 // BoolType is bool type 39 type BoolType = bool 40 41 // Reference is the struct for capability information 42 type Reference interface { 43 prepareParameter(tableName string, parameterList []ReferenceParameter) string 44 } 45 46 // FromCluster is the struct for input Namespace 47 type FromCluster struct { 48 Namespace string `json:"namespace"` 49 Rev int64 `json:"revision"` 50 PD *packages.PackageDiscover 51 } 52 53 // FromLocal is the struct for input Definition Path 54 type FromLocal struct { 55 Paths []string `json:"paths"` 56 } 57 58 // ConsoleReference is the struct for capability information in console 59 type ConsoleReference struct { 60 ParseReference 61 TableName string `json:"tableName"` 62 TableObject *tablewriter.Table `json:"tableObject"` 63 } 64 65 // BaseOpenAPIV3Template is Standard OpenAPIV3 Template 66 var BaseOpenAPIV3Template = `{ 67 "openapi": "3.0.0", 68 "info": { 69 "title": "definition-parameter", 70 "version": "1.0" 71 }, 72 "paths": {}, 73 "components": { 74 "schemas": { 75 "parameter": %s 76 } 77 } 78 }` 79 80 // ReferenceParameter is the parameter section of CUE template 81 type ReferenceParameter struct { 82 types.Parameter `json:",inline,omitempty"` 83 // PrintableType is same to `parameter.Type` which could be printable 84 PrintableType string `json:"printableType"` 85 } 86 87 // ReferenceParameterTable stores the information of a bunch of ReferenceParameter in a table style 88 type ReferenceParameterTable struct { 89 Name string 90 Parameters []ReferenceParameter 91 Depth *int 92 } 93 94 var commonRefs []CommonReference 95 96 // GenerateCUETemplateProperties get all properties of a capability 97 func (ref *ConsoleReference) GenerateCUETemplateProperties(capability *types.Capability, pd *packages.PackageDiscover) (string, []ConsoleReference, error) { 98 ref.DisplayFormat = "console" 99 capName := capability.Name 100 101 cueValue, err := common.GetCUEParameterValue(capability.CueTemplate, pd) 102 if err != nil { 103 return "", nil, fmt.Errorf("failed to retrieve `parameters` value from %s with err: %w", capName, err) 104 } 105 var defaultDepth = 0 106 doc, console, err := ref.parseParameters(capName, cueValue, Specification, defaultDepth, false) 107 if err != nil { 108 return "", nil, err 109 } 110 return doc, console, nil 111 } 112 113 // GenerateTerraformCapabilityProperties generates Capability properties for Terraform ComponentDefinition in Cli console 114 func (ref *ConsoleReference) GenerateTerraformCapabilityProperties(capability types.Capability) ([]ConsoleReference, error) { 115 var references []ConsoleReference 116 117 variableTables, _, err := ref.parseTerraformCapabilityParameters(capability) 118 if err != nil { 119 return nil, err 120 } 121 for _, t := range variableTables { 122 references = append(references, ref.prepareConsoleParameter(t.Name, t.Parameters, types.TerraformCategory)) 123 } 124 return references, nil 125 } 126 127 // Show will show capability reference in console 128 func (ref *ConsoleReference) Show(ctx context.Context, c common.Args, ioStreams cmdutil.IOStreams, capabilityName string, ns string, _ int64) error { 129 caps, err := ref.getCapabilities(ctx, c) 130 if err != nil { 131 return err 132 } 133 if len(caps) < 1 { 134 return fmt.Errorf("no capability found with name %s namespace %s", capabilityName, ns) 135 } 136 capability := &caps[0] 137 var propertyConsole []ConsoleReference 138 switch capability.Category { 139 case types.CUECategory: 140 var pd *packages.PackageDiscover 141 if ref.Remote != nil { 142 pd = ref.Remote.PD 143 } 144 _, propertyConsole, err = ref.GenerateCUETemplateProperties(capability, pd) 145 if err != nil { 146 return err 147 } 148 case types.TerraformCategory: 149 propertyConsole, err = ref.GenerateTerraformCapabilityProperties(*capability) 150 if err != nil { 151 return err 152 } 153 default: 154 return fmt.Errorf("unsupport capability category %s", capability.Category) 155 } 156 157 for _, p := range propertyConsole { 158 ioStreams.Info(p.TableName) 159 p.TableObject.Render() 160 ioStreams.Info("\n") 161 } 162 return nil 163 }