Skip to content

Commit

Permalink
feat: marshaling and overwriting of workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
smorihira committed Sep 4, 2024
1 parent fb28978 commit a064c2f
Showing 1 changed file with 42 additions and 63 deletions.
105 changes: 42 additions & 63 deletions hack/actions/gen/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"errors"
"io/fs"
Expand All @@ -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
Expand Down Expand Up @@ -286,6 +257,10 @@ var (
}
)

func generateWorkflowStruct(data Data) Workflow {
return Workflow{}
}

func main() {
// この辺の処理は共通?
log.Init()
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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
}))
}
Expand Down

0 comments on commit a064c2f

Please sign in to comment.