diff --git a/hack/actions/gen/main.go b/hack/actions/gen/main.go index 4930debaee..5ff0227aef 100644 --- a/hack/actions/gen/main.go +++ b/hack/actions/gen/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "errors" "io/fs" @@ -13,63 +14,33 @@ import ( "github.com/vdaas/vald/internal/log" "github.com/vdaas/vald/internal/safety" "github.com/vdaas/vald/internal/sync/errgroup" + "gopkg.in/yaml.v2" ) -type ( - name string - cron string - uses string - target string - secrets string - - branches []string - paths []string - platforms []string - - schedule struct { - cron cron - } - - push struct { - branches branches - } - - pull_request struct { - paths paths - } - - pull_request_target struct { - paths paths - } - - on struct { - schedule schedule - push push - pull_request pull_request - pull_request_target pull_request_target - } - - with struct { - target target - platforms platforms - } - - build struct { - uses uses - with with - secrets secrets - } - - jobs struct { - build build - } - - workflow struct { - name name - on on - jobs jobs - } -) +type Workflow struct { + Name string `yaml:"name"` + On struct { + Push struct { + Branches []string `yaml:"branches"` + Tags []string `yaml:"tags"` + } `yaml:"push"` + PullRequest struct { + Paths []string `yaml:"paths"` + } `yaml:"pull_request"` + PullRequestTarget struct { + Paths []string `yaml:"paths"` + } `yaml:"pull_request_target"` + } `yaml:"on"` + Jobs struct { + Build struct { + Uses string `yaml:"uses"` + With struct { + Target string `yaml:"target"` + } `yaml:"with"` + Secrets string `yaml:"secrets"` + } `yaml:"build"` + } `yaml:"jobs"` +} type Data struct { AliasImage bool @@ -286,6 +257,10 @@ var ( } ) +func generateWorkflowStruct(data Data) Workflow { + return Workflow{} +} + func main() { // この辺の処理は共通? log.Init() @@ -301,7 +276,7 @@ func main() { syscall.SIGKILL, syscall.SIGTERM) defer cancel() - log.Debug(tmpl) + log.Debug(tmpl) // workflow型を表示?ここどうしよう? maintainer := os.Getenv(maintainerKey) if maintainer == "" { @@ -349,18 +324,22 @@ func main() { data.Environments = appendM(data.Environments, defaultEnvironments) } - // ContainerTypeごとの処理 (workflow生成でも重要そう) + // TODO: ContainerTypeごとの処理 (workflow生成でも重要そう) // ↑ここまでdataの処理 (workflowの生成でもほとんど共通?) // ↓ここからファイルの生成 - // buf := bytes.NewBuffer(make([]byte, 0, len(tmpl))) log.Infof("Generating %s's workflow", name) - // workflow.Execute(buf, data) - tpl := buf.String() - buf.Reset() - // template.Must(template.New("workflow").Parse(tpl)).Execute(buf, data) - file.OverWriteFile(egctx, file.Join(os.Args[1], "actions", data.PackageDir, "TODO: ファイル名"), buf, fs.ModePerm) + workflowStruct := generateWorkflowStruct(data) + workflowYaml, err := yaml.Marshal(&workflowStruct) + if err != nil { + log.Fatalf("error marshaling workflowStruct to YAML: %v", err) + } + + // ここの書き方あってる?? + buf := bytes.NewBuffer(make([]byte, 0, len(workflowYaml))) + buf.Write(workflowYaml) + file.OverWriteFile(egctx, file.Join(os.Args[1], ".github/workflows", "TODO: ファイル名"), buf, fs.ModePerm) return nil })) }