github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/class/template.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package class 21 22 import ( 23 "fmt" 24 "os" 25 26 "github.com/spf13/cobra" 27 "k8s.io/cli-runtime/pkg/genericiooptions" 28 29 "github.com/1aal/kubeblocks/pkg/cli/util" 30 ) 31 32 const ComponentClassTemplate = ` 33 - # class template, you can declare variables and set default values here 34 template: | 35 cpu: "{{ or .cpu 1 }}" 36 memory: "{{ or .memory 4 }}Gi" 37 # template variables used to define classes 38 vars: [cpu, memory] 39 series: 40 - # class naming template, you can reference variables in class template 41 # it's also ok to define static class name in the following class definitions 42 namingTemplate: "custom-{{ .cpu }}c{{ .memory }}g" 43 44 # class definitions, we support two kinds of class definitions: 45 # 1. dynamically classes rendered with defined values & template variables 46 # 2. statically defined classes 47 classes: 48 - args: [1, 1] 49 ` 50 51 type TemplateOptions struct { 52 genericiooptions.IOStreams 53 54 outputFile string 55 } 56 57 func NewTemplateCmd(streams genericiooptions.IOStreams) *cobra.Command { 58 o := &TemplateOptions{IOStreams: streams} 59 cmd := &cobra.Command{ 60 Use: "template", 61 Short: "Generate class definition template", 62 Run: func(cmd *cobra.Command, args []string) { 63 util.CheckErr(o.run()) 64 }, 65 } 66 cmd.Flags().StringVarP(&o.outputFile, "output", "o", "", "Output class definition template to a file") 67 return cmd 68 } 69 70 func (o *TemplateOptions) run() error { 71 if o.outputFile != "" { 72 return os.WriteFile(o.outputFile, []byte(ComponentClassTemplate), 0644) 73 } 74 75 _, err := fmt.Fprint(o.Out, ComponentClassTemplate) 76 return err 77 }