github.com/oam-dev/kubevela@v1.9.11/hack/crd/dispatch/dispatch.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 main 18 19 import ( 20 "fmt" 21 "log" 22 "os" 23 "path/filepath" 24 "strings" 25 ) 26 27 var ( 28 oldCRD = map[string]bool{ 29 "workloaddefinitions": true, 30 } 31 ) 32 33 func main() { 34 var dir string 35 var newDir string 36 if len(os.Args) > 2 { 37 dir = os.Args[1] 38 newDir = os.Args[2] 39 } else { 40 log.Fatal(fmt.Errorf("not enough args")) 41 } 42 43 writeNew := func(fileName string, data []byte) { 44 pathNew := fmt.Sprintf("%s/%s", newDir, fileName) 45 /* #nosec */ 46 if err := os.WriteFile(pathNew, data, 0644); err != nil { 47 log.Fatal(err) 48 } 49 } 50 51 err := filepath.Walk(dir, func(path string, info os.FileInfo, _ error) error { 52 if info.IsDir() { 53 return nil 54 } 55 resourceName := extractMainInfo(info.Name()) 56 /* #nosec */ 57 data, err := os.ReadFile(path) 58 if err != nil { 59 fmt.Fprintln(os.Stderr, "failed to read file", err) 60 return err 61 } 62 if oldCRD[resourceName] { 63 return nil 64 } 65 writeNew(info.Name(), data) 66 return nil 67 }) 68 69 if err != nil { 70 log.Fatal(err) 71 } 72 log.Println("complete crd files dispatch") 73 } 74 75 func extractMainInfo(fileName string) string { 76 return strings.Split(strings.Split(fileName, "_")[1], ".")[0] 77 }