Skip to content

Commit

Permalink
feat: write a half of initializeData func
Browse files Browse the repository at this point in the history
  • Loading branch information
smorihira committed Sep 4, 2024
1 parent a810929 commit e709f16
Showing 1 changed file with 23 additions and 49 deletions.
72 changes: 23 additions & 49 deletions hack/actions/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/vdaas/vald/internal/file"
"github.com/vdaas/vald/internal/log"
Expand All @@ -35,7 +34,8 @@ type Workflow struct {
Build struct {
Uses string `yaml:"uses"`
With struct {
Target string `yaml:"target"`
Target string `yaml:"target"`
Platforms string `yaml:"platforms"`
} `yaml:"with"`
Secrets string `yaml:"secrets"`
} `yaml:"build"`
Expand Down Expand Up @@ -78,6 +78,7 @@ type Data struct {
PullRequestTargetPaths []string
BuildUses string
BuildTarget string
BuildPlatforms string
BuildSecrets string
}

Expand Down Expand Up @@ -264,7 +265,7 @@ var (
}
)

func generateWorkflowStruct(data Data) Workflow {
func generateWorkflowStruct(data *Data) Workflow {
var workflow Workflow

workflow.Name = data.Name
Expand All @@ -274,13 +275,26 @@ func generateWorkflowStruct(data Data) Workflow {
workflow.On.PullRequestTarget.Paths = data.PullRequestTargetPaths
workflow.Jobs.Build.Uses = data.BuildUses
workflow.Jobs.Build.With.Target = data.BuildTarget
workflow.Jobs.Build.With.Platforms = data.BuildPlatforms
workflow.Jobs.Build.Secrets = data.BuildSecrets

return workflow
}

func initializeData(data *Data) {
var pullRequestPaths = make([]string, 0) // TODO: ContainerTypeごとなどいろいろ条件分岐

data.PushBranches = []string{"main", "release/v*.*", "!release/v*.*.*"}
data.PushTags = []string{"*.*.*", "v*.*.*", "*.*.*-*", "v*.*.*-*"}
data.PullRequestPaths = pullRequestPaths
data.PullRequestTargetPaths = pullRequestPaths
data.BuildUses = "./.github/workflows/_docker-image.yaml"
data.BuildTarget = data.Name
data.BuildPlatforms = "linux/amd64,linux/arm64" // TODO: ここも分岐必要??
data.BuildSecrets = "inherit"
}

func main() {
// TODO: この辺の処理は共通?
log.Init()
if len(os.Args) < minimumArgumentLength {
// skipcq: RVV-A0003
Expand All @@ -294,61 +308,21 @@ func main() {
syscall.SIGKILL,
syscall.SIGTERM)
defer cancel()
log.Debug(tmpl) // TODO: workflow型を表示?ここどうしよう?
// log.Debug(tmpl) TODO: workflow型を表示?ここどうしよう?

maintainer := os.Getenv(maintainerKey)
if maintainer == "" {
maintainer = defaultMaintainer
}
year := time.Now().Year()
eg, egctx := errgroup.New(ctx)
for n, d := range map[string]Data{
// TODO: mapを入れる
} {
name := n
data := d
data.Name = name[len("vald-"):]

eg.Go(safety.RecoverFunc(func() error {
data.Maintainer = maintainer
data.Year = year
if data.BinDir == "" {
data.BinDir = defaultBinaryDir
}
if data.RuntimeImage == "" {
data.RuntimeImage = defaultRuntimeImage
}
if data.RuntimeTag == "" {
data.RuntimeTag = defaultRuntimeTag
}
if data.BuilderImage == "" {
data.BuilderImage = defaultBuilderImage
}
if data.BuilderTag == "" {
data.BuilderTag = defaultBuilderTag
}
if data.RuntimeUser == "" {
data.RuntimeUser = defaultRuntimeUser
}
if data.BuildUser == "" {
data.BuildUser = defaultBuildUser
}
if data.BuildStageName == "" {
data.BuildStageName = defaultBuildStageName
}
if data.Environments != nil {
data.Environments = appendM(data.Environments, defaultEnvironments)
} else {
data.Environments = make(map[string]string, len(defaultEnvironments))
data.Environments = appendM(data.Environments, defaultEnvironments)
}

// TODO: ContainerTypeごとの処理 (workflow生成でも重要そう)

// ↑ここまでdataの処理 (TODO: workflowの生成でもほとんど共通?)
initializeData(&data)

// ↓ここからファイルの生成
log.Infof("Generating %s's workflow", name)
workflowStruct := generateWorkflowStruct(data)
workflowStruct := generateWorkflowStruct(&data)
workflowYaml, err := yaml.Marshal(&workflowStruct)
if err != nil {
log.Fatalf("error marshaling workflowStruct to YAML: %v", err)
Expand All @@ -357,7 +331,7 @@ func main() {
// TODO: ここの書き方あってる??
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)
file.OverWriteFile(egctx, file.Join(os.Args[1], ".github/workflows", "dockers-"+data.Name+"-image.yml"), buf, fs.ModePerm)
return nil
}))
}
Expand Down

0 comments on commit e709f16

Please sign in to comment.