Skip to content

Instantly share code, notes, and snippets.

@phpgao
Last active October 20, 2024 04:18
Show Gist options
  • Save phpgao/9404c323fe2c74d2862701ac4c6e4dc8 to your computer and use it in GitHub Desktop.
Save phpgao/9404c323fe2c74d2862701ac4c6e4dc8 to your computer and use it in GitHub Desktop.
gen doc.go register.go
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
Domain string `yaml:"domain"`
Layout []string `yaml:"layout"`
Multigroup bool `yaml:"multigroup"`
ProjectName string `yaml:"projectName"`
Repo string `yaml:"repo"`
Resources []Resources `yaml:"resources"`
Version string `yaml:"version"`
}
type API struct {
CrdVersion string `yaml:"crdVersion"`
Namespaced bool `yaml:"namespaced"`
}
type Resources struct {
API API `yaml:"api"`
Domain string `yaml:"domain"`
Group string `yaml:"group"`
Kind string `yaml:"kind"`
Path string `yaml:"path"`
Version string `yaml:"version"`
}
func main() {
// 定义命令行参数
configFile := flag.String("config", "PROJECT", "Path to the configuration file")
flag.Parse()
// 获取配置文件的绝对路径
absConfigFile, err := filepath.Abs(*configFile)
if err != nil {
log.Fatalf("Error getting absolute path: %v", err)
}
// 读取文件
file, err := os.Open(absConfigFile)
if err != nil {
log.Fatalf("Error opening config file: %v", err)
}
defer file.Close()
// 读取文件内容
content, err := io.ReadAll(file)
if err != nil {
log.Fatalf("Error reading config file: %v", err)
}
// 解析 YAML
var config Config
err = yaml.Unmarshal(content, &config)
if err != nil {
log.Fatalf("Error parsing YAML: %v", err)
}
root := filepath.Dir(absConfigFile)
fmt.Println(root)
for _, resource := range config.Resources {
groupName := fmt.Sprintf("%s.%s", resource.Group, resource.Domain)
realDir := strings.Replace(resource.Path, fmt.Sprintf("%s/", config.Repo), "", 1)
realPath := filepath.Join(root, realDir)
// doc.go
doc := filepath.Join(realPath, "doc.go")
log.Printf("gen %s", doc)
err := writeDocFile(doc, groupName, resource.Version)
if err != nil {
log.Fatalf("Error writing to file %s: %v", doc, err)
}
//register.go
register := filepath.Join(realPath, "register.go")
log.Printf("gen %s", register)
err = writeRegisterFile(register, groupName, resource.Version)
if err != nil {
log.Fatalf("Error writing to file %s: %v", doc, err)
}
// 处理 _types.go 文件
err = processTypesFiles(realPath)
if err != nil {
log.Fatalf("Error processing _types.go files: %v", err)
}
}
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return err == nil
}
func writeDocFile(filename, groupName, version string) error {
// 创建文件
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
// 写入内容
content := fmt.Sprintf("// +groupName=%s\n\npackage %s\n", groupName, version)
_, err = file.WriteString(content)
return err
}
func writeRegisterFile(filename, groupName, version string) error {
// 创建文件
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
// 写入内容
content := fmt.Sprintf("package %s\n \nimport (\n \"k8s.io/apimachinery/pkg/runtime/schema\"\n)\n \n// SchemeGroupVersion is group version used to register these objects.\nvar SchemeGroupVersion = GroupVersion\n \nfunc Resource(resource string) schema.GroupResource {\n return SchemeGroupVersion.WithResource(resource).GroupResource()\n}", version)
_, err = file.WriteString(content)
return err
}
func processTypesFiles(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), "_types.go") {
filePath := filepath.Join(dir, file.Name())
content, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
contentStr := string(content)
if !strings.Contains(contentStr, "// +genclient") {
// 添加 // +genclient 注释
newContent := strings.Replace(contentStr,
"// +kubebuilder:subresource:status",
"// +kubebuilder:subresource:status\n// +genclient", 1)
err = os.WriteFile(filePath, []byte(newContent), 0644)
if err != nil {
return err
}
}
}
}
return nil
}
#!/usr/bin/env bash
# Modified from https://github.com/kubernetes-sigs/kueue/blob/065451d907fa27a0647436505b3cac38718ef136/hack/update-codegen.sh
# Apache-2.0, Copyright 2023 The Kubernetes Authors
set -o errexit
set -o nounset
set -o pipefail
PKG_ROOT=$(realpath)
OUTPUT_PKG=${1:-github.com/phpgao/crd/pkg}
CODEGEN_PKG=./code-generator
rm -rf $CODEGEN_PKG/examples
rm -rf test
source "${CODEGEN_PKG}/kube_codegen.sh"
kube::codegen::gen_client \
--output-pkg "$OUTPUT_PKG" \
--boilerplate "hack/boilerplate.go.txt" \
--output-dir "${PKG_ROOT}/pkg" \
--with-watch ./api
go mod tidy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment