github.com/oam-dev/kubevela@v1.9.11/references/cli/cuex.go (about) 1 /* 2 Copyright 2023 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 cli 18 19 import ( 20 "fmt" 21 "os" 22 23 "github.com/kubevela/pkg/cue/cuex" 24 "github.com/kubevela/pkg/cue/util" 25 "github.com/spf13/cobra" 26 "k8s.io/kubectl/pkg/util/i18n" 27 "k8s.io/kubectl/pkg/util/templates" 28 29 "github.com/oam-dev/kubevela/apis/types" 30 velacmd "github.com/oam-dev/kubevela/pkg/cmd" 31 ) 32 33 // CueXCommandGroup commands for cuex management 34 func CueXCommandGroup(f velacmd.Factory, order string) *cobra.Command { 35 cmd := &cobra.Command{ 36 Use: "cuex", 37 Short: i18n.T("Manage CueX engine for compile."), 38 Annotations: map[string]string{ 39 types.TagCommandType: types.TypeAuxiliary, 40 types.TagCommandOrder: order, 41 }, 42 } 43 cmd.AddCommand(NewCueXEvalCommand(f)) 44 return cmd 45 } 46 47 // CueXEvalOption option for compile cuex 48 type CueXEvalOption struct { 49 Format string 50 Path string 51 File string 52 } 53 54 // Run compile cuex 55 func (in *CueXEvalOption) Run(cmd *cobra.Command) error { 56 if in.File == "" { 57 return fmt.Errorf("file must be provided for compile") 58 } 59 bs, err := os.ReadFile(in.File) 60 if err != nil { 61 return err 62 } 63 val, err := cuex.CompileString(cmd.Context(), string(bs)) 64 if err != nil { 65 return err 66 } 67 bs, err = util.Print(val, util.WithFormat(in.Format), util.WithPath(in.Path)) 68 if err != nil { 69 return err 70 } 71 cmd.Println(string(bs)) 72 return nil 73 } 74 75 var ( 76 cuexEvalLong = templates.LongDesc(i18n.T(` 77 Eval cue file with CueX engine. 78 79 Evaluate your cue file with the CueX engine. When your cue file does not 80 use KubeVela's extension, it will work similarly to the native CUE CLI. 81 When using KubeVela's extensions, this command will execute the extension 82 functions and resolve values, in addition to the native CUE compile process. 83 `)) 84 85 cuexEvalExample = templates.Examples(i18n.T(` 86 # Evaluate a cue file 87 vela cuex eval -f my.cue 88 89 # Evaluate a cue file into json format 90 vela cuex eval -f my.cue -o json 91 92 # Evaluate a cue file and output the target path 93 vela cuex eval -f my.cue -p key.path 94 `)) 95 ) 96 97 // NewCueXEvalCommand `vela cuex eval` command 98 func NewCueXEvalCommand(f velacmd.Factory) *cobra.Command { 99 opt := &CueXEvalOption{ 100 Format: string(util.PrintFormatCue), 101 } 102 cmd := &cobra.Command{ 103 Use: "eval", 104 Short: i18n.T("Eval cue file with CueX engine."), 105 Long: cuexEvalLong, 106 Example: cuexEvalExample, 107 RunE: func(cmd *cobra.Command, args []string) error { 108 return opt.Run(cmd) 109 }, 110 } 111 cmd.Flags().StringVarP(&opt.Format, "format", "o", opt.Format, "format of the output") 112 cmd.Flags().StringVarP(&opt.File, "file", "f", opt.File, "file for eval") 113 cmd.Flags().StringVarP(&opt.Path, "path", "p", opt.Path, "path for eval") 114 return velacmd.NewCommandBuilder(f, cmd). 115 WithResponsiveWriter(). 116 Build() 117 }