Skip to content

Commit b08c51c

Browse files
authored
support default docker tag (#75)
1 parent f02c6e4 commit b08c51c

11 files changed

Lines changed: 186 additions & 16 deletions

File tree

.github/workflows/integration-tests.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,31 @@ jobs:
164164
run: python3 ./integration/non-integer-pk/test.py ./integration/non-integer-pk/golang/mysql.yml
165165
env:
166166
DB_ADDRESS: user:password@tcp(localhost:3306)/database?allowCleartextPasswords=true&allowNativePasswords=true&parseTime=true
167+
docker:
168+
needs: smoke_test
169+
runs-on: ubuntu-latest
170+
name: Run generating docker test
171+
steps:
172+
- name: Check out repository
173+
uses: actions/checkout@v2
174+
- uses: actions/setup-go@v2
175+
with:
176+
go-version: 1.14
177+
- name: Build
178+
run: make build
179+
- name: Run the test case
180+
run: python3 ./integration/docker/test.py
181+
cicd:
182+
needs: smoke_test
183+
runs-on: ubuntu-latest
184+
name: Run generating cicd test
185+
steps:
186+
- name: Check out repository
187+
uses: actions/checkout@v2
188+
- uses: actions/setup-go@v2
189+
with:
190+
go-version: 1.14
191+
- name: Build
192+
run: make build
193+
- name: Run the test case
194+
run: python3 ./integration/cicd/test.py

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
build: generate
2-
go build
2+
CGO_ENABLED=0 go build
33

44
generate:
55
go generate
@@ -12,9 +12,9 @@ clean:
1212
rm -rf ./autoAPI
1313

1414
build-release: generate
15-
GOOS=windows GOARCH=amd64 go build -o autoAPI.exe
15+
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o autoAPI.exe
1616
zip autoAPI-windows-amd64.zip autoAPI.exe
17-
GOOS=darwin GOARCH=amd64 go build -o autoAPI
17+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o autoAPI
1818
zip autoAPI-darwin-amd64.zip autoAPI
19-
GOOS=linux GOARCH=amd64 go build -o autoAPI
19+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o autoAPI
2020
zip autoAPI-linux-amd64.zip autoAPI

config/config.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ func FromCommandLine(c *cli.Context) (*Config, error) {
2828
return nil, err
2929
}
3030
result.Docker, err = docker.FromCommandLine(c)
31+
if err != nil {
32+
return nil, err
33+
}
34+
result.CICD, err = cicd.FromCommandLine(c)
3135
return &result, err
3236
}
3337

3438
func (c *Config) MergeWithEnv() error {
3539
if err := c.Database.MergeWithEnv(); err != nil {
3640
return err
3741
}
38-
return c.Docker.MergeWithEnv()
42+
if c.Docker != nil {
43+
return c.Docker.MergeWithEnv()
44+
}
45+
return nil
3946
}
4047

4148
func (c *Config) MergeWithConfig(path string) error {
@@ -119,7 +126,7 @@ func (c *Config) MergeWith(other *Config) {
119126
}
120127

121128
func (c *Config) Validate() error {
122-
if c.CICD != nil && c.CICD.K8s == nil && c.CICD.GithubAction == nil {
129+
if (c.CICD != nil && c.CICD.K8s == nil && c.CICD.GithubAction == nil) || (c.Docker == nil || c.Docker.Username == nil && c.Docker.Tag == nil) {
123130
c.CICD = nil
124131
}
125132
err := c.Database.Validate()

config/fields/cicd/cicd.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cicd
22

3+
import "github.com/urfave/cli/v2"
4+
35
type CICD struct {
46
GithubAction *bool `yaml:"GitHubAction" json:"GitHubAction"`
57
K8s *bool `yaml:"k8s" json:"k8s"`
@@ -27,3 +29,12 @@ func (cicd *CICD) MergeWith(other *CICD) {
2729
cicd.K8s = other.K8s
2830
}
2931
}
32+
33+
func FromCommandLine(c *cli.Context) (*CICD, error) {
34+
t := true
35+
f := false
36+
return &CICD{
37+
GithubAction: &t,
38+
K8s: &f,
39+
}, nil
40+
}

config/fields/docker/docker.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ func (docker *Docker) MergeWith(other *Docker) {
2323
}
2424

2525
func (docker *Docker) MergeWithEnv() error {
26-
if os.Getenv("DOCKER_USERNAME") != "" {
26+
if docker.Username == nil && os.Getenv("DOCKER_USERNAME") != "" {
2727
username := os.Getenv("DOCKER_USERNAME")
2828
docker.Username = &username
2929
}
30-
if os.Getenv("DOCKER_TAG") != "" {
30+
if docker.Tag == nil && os.Getenv("DOCKER_TAG") != "" {
3131
tag := os.Getenv("DOCKER_TAG")
3232
docker.Tag = &tag
3333
}
34+
if docker.Tag == nil && os.Getenv("GITHUB_RUN_NUMBER") != "" {
35+
tag := "ci-v" + os.Getenv("GITHUB_RUN_NUMBER")
36+
docker.Tag = &tag
37+
}
3438
return nil
3539
}
3640

@@ -42,7 +46,7 @@ func FromCommandLine(c *cli.Context) (*Docker, error) {
4246
if tag := c.String("dockertag"); tag != "" {
4347
result.Tag = &tag
4448
}
45-
if result.Username == nil && result.Tag == nil {
49+
if c.Bool("nodocker") {
4650
return nil, nil
4751
}
4852
return &result, nil

integration/cicd/default.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
database:
2+
tablename: student
3+
fields:
4+
- name: name
5+
type: varchar(24)
6+
- name: birthday
7+
type: date
8+
- name: school_id
9+
type: int

integration/cicd/test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
import subprocess
3+
import unittest
4+
5+
6+
class CICDGeneratingTest(unittest.TestCase):
7+
def test_default(self):
8+
subprocess.run("./autoAPI --force -f ./integration/docker/default.yml --force -o ./api".split(' '))
9+
self.assertFalse(os.path.exists("./api/.github/workflows/dockerimage.yml"))
10+
11+
def test_command_line(self):
12+
# force noDocker
13+
subprocess.run(
14+
"./autoAPI --force -f ./integration/docker/default.yml --force --nodocker -o ./api".split(' '))
15+
self.assertTrue(os.path.exists("./api"))
16+
self.assertFalse(os.path.exists("./api/.github/workflows/dockerimage.yml"))
17+
18+
subprocess.run(
19+
"./autoAPI --force -f ./integration/docker/default.yml --force -nd -o ./api".split(' '))
20+
self.assertTrue(os.path.exists("./api"))
21+
self.assertFalse(os.path.exists("./api/.github/workflows/dockerimage.yml"))
22+
23+
def test_docker_image_name(self):
24+
# set docker info by command
25+
subprocess.run(
26+
"./autoAPI --force -f ./integration/docker/default.yml --force -du testuser -dt v0 -o ./api".split(
27+
' '))
28+
self.assertTrue(os.path.exists("./api/.github/workflows/dockerimage.yml"))
29+
with open("./api/.github/workflows/dockerimage.yml") as f:
30+
content = f.read()
31+
self.assertEqual(content.count("testuser/student:v0"), 2)
32+
self.assertIn('--username "testuser"', content)
33+
# set docker info by env
34+
subprocess.run(
35+
"./autoAPI --force -f ./integration/docker/default.yml --force -o ./api".split(' '),
36+
env={'DOCKER_USERNAME': 'testuser', 'DOCKER_TAG': 'v0'})
37+
with open("./api/.github/workflows/dockerimage.yml") as f:
38+
content = f.read()
39+
self.assertEqual(content.count("testuser/student:v0"), 2)
40+
self.assertIn('--username "testuser"', content)
41+
# simulate GitHub Actions env
42+
subprocess.run(
43+
"./autoAPI --force -f ./integration/docker/default.yml --force -o ./api".split(' '),
44+
env={'DOCKER_USERNAME': 'testuser', 'GITHUB_RUN_NUMBER': '7'})
45+
with open("./api/.github/workflows/dockerimage.yml") as f:
46+
content = f.read()
47+
self.assertEqual(content.count("testuser/student:ci-v7"), 2)
48+
self.assertIn('--username "testuser"', content)
49+
50+
51+
if __name__ == '__main__':
52+
loader = unittest.TestLoader()
53+
loader.sortTestMethodsUsing = None
54+
unittest.main(testLoader=loader)

integration/docker/default.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
database:
2+
tablename: student
3+
fields:
4+
- name: name
5+
type: varchar(24)
6+
- name: birthday
7+
type: date
8+
- name: school_id
9+
type: int

integration/docker/test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import subprocess
3+
import unittest
4+
5+
6+
# todo: check whether the dockerfile generated is right, ie. can build and work
7+
class DockerfileGeneratingTest(unittest.TestCase):
8+
def test_default(self):
9+
subprocess.run("./autoAPI --force -f ./integration/docker/default.yml --force -o ./api".split(' '))
10+
self.assertTrue(os.path.exists("./api/Dockerfile"))
11+
12+
def test_command_line(self):
13+
# force noDocker
14+
subprocess.run(
15+
"./autoAPI --force -f ./integration/docker/default.yml --force --nodocker -o ./api".split(' '))
16+
self.assertTrue(os.path.exists("./api"))
17+
self.assertFalse(os.path.exists("./api/Dockerfile"))
18+
19+
20+
if __name__ == '__main__':
21+
loader = unittest.TestLoader()
22+
loader.sortTestMethodsUsing = None
23+
unittest.main(testLoader=loader)

main.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import (
88
"autoAPI/generator/apiGenerator/golang"
99
"autoAPI/generator/cicdGenerator"
1010
"errors"
11+
"fmt"
1112
"github.com/urfave/cli/v2"
12-
"log"
1313
"os"
1414
)
1515

1616
func main() {
1717
err := (&cli.App{
1818
Flags: []cli.Flag{
19-
&cli.StringFlag{
19+
&cli.PathFlag{
2020
Name: "file",
2121
Aliases: []string{"f"},
2222
Usage: "Load configuration from `FILE`",
2323
},
24-
&cli.StringFlag{
24+
&cli.PathFlag{
2525
Name: "output",
2626
Aliases: []string{"o"},
2727
Usage: "Put the output code in `PATH`",
@@ -32,6 +32,31 @@ func main() {
3232
Value: false,
3333
Usage: "If the output PATH dir exists, use '--force' to overwrite it",
3434
},
35+
&cli.StringFlag{
36+
Name: "dockerusername",
37+
Aliases: []string{"du"},
38+
Usage: "Your DockerHub's username",
39+
Required: false,
40+
},
41+
&cli.StringFlag{
42+
Name: "dockertag",
43+
Aliases: []string{"dt"},
44+
Usage: "Tag of the generated docker image",
45+
Required: false,
46+
},
47+
&cli.BoolFlag{
48+
Name: "nodocker",
49+
Aliases: []string{"nd"},
50+
Usage: "Force not generating docker",
51+
Required: false,
52+
Value: false,
53+
},
54+
&cli.StringFlag{
55+
Name: "dbengine",
56+
Aliases: []string{"dbms"},
57+
Usage: "The dbengine you want use",
58+
Required: false,
59+
},
3560
},
3661
Name: "autoAPI",
3762
Usage: "Generate an CRUD api endpoint program automatically!",
@@ -89,6 +114,6 @@ func main() {
89114
},
90115
}).Run(os.Args)
91116
if err != nil {
92-
log.Fatal(err)
117+
_ = fmt.Errorf("%s", err.Error())
93118
}
94119
}

0 commit comments

Comments
 (0)