Skip to content

Commit 5d18d2c

Browse files
authored
chore: support to set the version (#14)
* chore: support to set the version * add cmd flag to print the version * update api-testing dep --------- Co-authored-by: Rick <[email protected]>
1 parent 62b8c3c commit 5d18d2c

File tree

7 files changed

+67
-5
lines changed

7 files changed

+67
-5
lines changed

.goreleaser.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ builds:
1818
ldflags:
1919
- -w
2020
- -s
21+
- -X github.com/linuxsuren/api-testing/pkg/version.version={{.Version}} -X github.com/linuxsuren/api-testing/pkg/version.commit={{.Commit}} -X github.com/linuxsuren/api-testing/pkg/version.date={{.Date}}
2122
archives:
2223
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
2324
builds:

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ WORKDIR /workspace
99
COPY . .
1010

1111
RUN GOPROXY=${GOPROXY} go mod download
12-
RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s" -o atest-store-orm .
12+
RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s -X github.com/linuxsuren/api-testing/pkg/version.version=${VERSION}\
13+
-X github.com/linuxsuren/api-testing/pkg/version.date=$(date +%Y-%m-%d)" -o atest-store-orm .
1314

1415
FROM ${BASE_IMAGE}
1516

cmd/root.go

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
ext "github.com/linuxsuren/api-testing/pkg/extension"
20+
"github.com/linuxsuren/api-testing/pkg/version"
2021
"github.com/linuxsuren/atest-ext-store-orm/pkg"
2122
"github.com/spf13/cobra"
2223
)
@@ -31,15 +32,22 @@ func NewRootCommand() (c *cobra.Command) {
3132
RunE: opt.runE,
3233
}
3334
opt.AddFlags(c.Flags())
35+
c.Flags().BoolVarP(&opt.version, "version", "", false, "Print the version then exit")
3436
return
3537
}
3638

3739
func (o *option) runE(c *cobra.Command, args []string) (err error) {
40+
if o.version {
41+
c.Println(version.GetVersion())
42+
c.Println(version.GetDate())
43+
return
44+
}
3845
remoteServer := pkg.NewRemoteServer()
3946
err = ext.CreateRunner(o.Extension, c, remoteServer)
4047
return
4148
}
4249

4350
type option struct {
4451
*ext.Extension
52+
version bool
4553
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.2
55
toolchain go1.22.4
66

77
require (
8-
github.com/linuxsuren/api-testing v0.0.18-0.20240712013124-33c89be15c83
8+
github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07
99
github.com/spf13/cobra v1.8.0
1010
github.com/stretchr/testify v1.9.0
1111
gorm.io/driver/mysql v1.5.2

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
9696
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
9797
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
9898
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
99-
github.com/linuxsuren/api-testing v0.0.18-0.20240712013124-33c89be15c83 h1:NUGWAWp6fMc9qFERsP0TdJE6lTItEDI6F6v7tDkHNx8=
100-
github.com/linuxsuren/api-testing v0.0.18-0.20240712013124-33c89be15c83/go.mod h1:8G3GZtQmSUHyqA/gZlSdFaDIveHQYbFInTibD7g3hMs=
99+
github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07 h1:NmjqkiR+4KXjWs9H6wGmgQo7r7FJE+RED+E3EwAGsp4=
100+
github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07/go.mod h1:8G3GZtQmSUHyqA/gZlSdFaDIveHQYbFInTibD7g3hMs=
101101
github.com/linuxsuren/go-fake-runtime v0.0.4 h1:y+tvBuw6MKTCav8Bo5HWwaXhBx1Z//VAvqI3gpOWqvw=
102102
github.com/linuxsuren/go-fake-runtime v0.0.4/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
103103
github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=

pkg/server.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import (
2929
"github.com/linuxsuren/api-testing/pkg/version"
3030
"gorm.io/driver/mysql"
3131
"gorm.io/driver/postgres"
32+
"gorm.io/driver/sqlite"
3233
"gorm.io/gorm"
3334
"gorm.io/gorm/logger"
34-
"gorm.io/driver/sqlite"
3535
)
3636

3737
type dbserver struct {
@@ -273,6 +273,16 @@ func (s *dbserver) Verify(ctx context.Context, in *server.Empty) (reply *server.
273273
}
274274
return
275275
}
276+
277+
func (s *dbserver) GetVersion(context.Context, *server.Empty) (ver *server.Version, err error) {
278+
ver = &server.Version{
279+
Version: version.GetVersion(),
280+
Commit: version.GetCommit(),
281+
Date: version.GetDate(),
282+
}
283+
return
284+
}
285+
276286
func (s *dbserver) PProf(ctx context.Context, in *server.PProfRequest) (data *server.PProfData, err error) {
277287
log.Println("pprof", in.Name)
278288

pkg/server_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,43 @@ func TestNewRemoteServer(t *testing.T) {
8686
assert.NoError(t, err)
8787
assert.False(t, reply.Ready)
8888
})
89+
90+
t.Run("invalid orm driver", func(t *testing.T) {
91+
remoteServer := NewRemoteServer()
92+
assert.NotNil(t, remoteServer)
93+
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
94+
Properties: map[string]string{
95+
"driver": "invalid",
96+
},
97+
})
98+
_, err := remoteServer.ListTestSuite(defaultCtx, &server.Empty{})
99+
assert.Error(t, err)
100+
})
101+
102+
t.Run("invalid mysql config", func(t *testing.T) {
103+
remoteServer := NewRemoteServer()
104+
assert.NotNil(t, remoteServer)
105+
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
106+
Properties: map[string]string{
107+
"driver": "mysql",
108+
},
109+
})
110+
_, err := remoteServer.ListTestSuite(defaultCtx, &server.Empty{})
111+
assert.Error(t, err)
112+
})
113+
114+
t.Run("invalid postgres config", func(t *testing.T) {
115+
remoteServer := NewRemoteServer()
116+
assert.NotNil(t, remoteServer)
117+
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
118+
Properties: map[string]string{
119+
"driver": "postgres",
120+
},
121+
URL: "0.0.0.0:-123",
122+
})
123+
_, err := remoteServer.ListTestSuite(defaultCtx, &server.Empty{})
124+
assert.Error(t, err)
125+
})
89126
}
90127

91128
func TestSQLite(t *testing.T) {
@@ -183,4 +220,9 @@ func TestSQLite(t *testing.T) {
183220
_, err := remoteServer.PProf(defaultCtx, &server.PProfRequest{})
184221
assert.NoError(t, err)
185222
})
223+
224+
t.Run("GetVersion", func(t *testing.T) {
225+
_, err := remoteServer.GetVersion(defaultCtx, &server.Empty{})
226+
assert.NoError(t, err)
227+
})
186228
}

0 commit comments

Comments
 (0)