Skip to content

Commit d33ac9c

Browse files
committed
feat: complete the basic feature of converting
1 parent 2aa9a86 commit d33ac9c

15 files changed

Lines changed: 431 additions & 1 deletion
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Pull Request Build
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
name: Build
14+
runs-on: ubuntu-20.04
15+
steps:
16+
- name: Set up Go 1.19
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: 1.19
20+
id: go
21+
- name: Check out code into the Go module directory
22+
uses: actions/checkout@v3.0.0
23+
- name: Test
24+
run: |
25+
go test ./... -coverprofile coverage.out
26+
- name: Run GoReleaser
27+
uses: goreleaser/goreleaser-action@v2.9.1
28+
with:
29+
version: latest
30+
args: release --skip-publish --rm-dist

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/
2+
.idea/

.goreleaser.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
builds:
2+
- env:
3+
- CGO_ENABLED=0
4+
goos:
5+
- linux
6+
- windows
7+
- darwin
8+
archives:
9+
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
10+
format_overrides:
11+
- goos: windows
12+
format: zip
13+
files:
14+
- README.md
15+
checksum:
16+
name_template: 'checksums.txt'
17+
snapshot:
18+
name_template: "{{ incpatch .Version }}-next"
19+
changelog:
20+
sort: asc
21+
filters:
22+
exclude:
23+
- '^docs:'
24+
- '^test:'

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build:
2+
go build -o bin/gaw .
3+
copy: build
4+
cp bin/gaw /usr/local/bin

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# github-action-workflow
2-
GitHub Actions compitable workflows
2+
GitHub Actions compatible workflows

cmd/convert.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import (
4+
"github.com/linuxsuren/github-action-workflow/pkg"
5+
"github.com/spf13/cobra"
6+
"golang.org/x/exp/maps"
7+
"gopkg.in/yaml.v2"
8+
"os"
9+
"strings"
10+
)
11+
12+
func newConvertCmd() (c *cobra.Command) {
13+
opt := &convertOption{}
14+
c = &cobra.Command{
15+
Use: "convert",
16+
Example: "gaw convert .github/workflows/build.yaml",
17+
Short: "Convert GitHub Actions workflow file to Argo Workflows",
18+
Args: cobra.MinimumNArgs(1),
19+
RunE: opt.runE,
20+
}
21+
22+
flags := c.Flags()
23+
flags.StringToStringVarP(&opt.env, "env", "e", nil,
24+
"Environment variables for all steps")
25+
return
26+
}
27+
28+
func (o *convertOption) runE(cmd *cobra.Command, args []string) (err error) {
29+
gh := &pkg.Workflow{}
30+
var data []byte
31+
if data, err = os.ReadFile(args[0]); err == nil {
32+
if err = yaml.Unmarshal(data, gh); err != nil {
33+
return
34+
}
35+
}
36+
37+
for i, job := range gh.Jobs {
38+
for j, step := range job.Steps {
39+
if step.Env == nil {
40+
gh.Jobs[i].Steps[j].Env = o.env
41+
} else {
42+
maps.Copy(gh.Jobs[i].Steps[j].Env, o.env)
43+
}
44+
}
45+
}
46+
47+
var result string
48+
if result, err = gh.ConvertToArgoWorkflow(); err == nil {
49+
cmd.Println(strings.TrimSpace(result))
50+
}
51+
return
52+
}
53+
54+
type convertOption struct {
55+
env map[string]string
56+
}

cmd/root.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"os"
6+
)
7+
8+
func NewRoot() (c *cobra.Command) {
9+
c = &cobra.Command{
10+
Use: "gaw",
11+
Short: "GitHub Actions workflow compatible tool",
12+
}
13+
14+
c.SetOut(os.Stdout)
15+
c.AddCommand(newConvertCmd())
16+
return
17+
}

go.mod

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module github.com/linuxsuren/github-action-workflow
2+
3+
go 1.19
4+
5+
require (
6+
github.com/spf13/cobra v1.6.1
7+
github.com/stretchr/testify v1.8.1
8+
golang.org/x/exp v0.0.0-20221211140036-ad323defaf05
9+
gopkg.in/yaml.v2 v2.4.0
10+
)
11+
12+
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
github.com/spf13/pflag v1.0.5 // indirect
17+
gopkg.in/yaml.v3 v3.0.1 // indirect
18+
)

go.sum

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
6+
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
10+
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
11+
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
12+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
13+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
14+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
15+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
16+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
17+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
18+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
19+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
20+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
21+
golang.org/x/exp v0.0.0-20221211140036-ad323defaf05 h1:T8EldfGCcveFMewH5xAYxxoX3PSQMrsechlUGVFlQBU=
22+
golang.org/x/exp v0.0.0-20221211140036-ad323defaf05/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
23+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
26+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
27+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
28+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
29+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"github.com/linuxsuren/github-action-workflow/cmd"
5+
"os"
6+
)
7+
8+
func main() {
9+
if err := cmd.NewRoot().Execute(); err != nil {
10+
os.Exit(1)
11+
}
12+
}

0 commit comments

Comments
 (0)