Skip to content

Commit adeb2de

Browse files
committed
feat(config): 优化获取 goctl 版本
1 parent 03a5de0 commit adeb2de

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

config/config.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77
"strings"
8+
"sync"
89

910
"github.com/hashicorp/go-version"
1011
"github.com/spf13/pflag"
@@ -262,23 +263,27 @@ func (c *Config) SwaggerDir() string {
262263
return filepath.Join("desc", "swagger")
263264
}
264265

265-
func (c *Config) GoctlVersion() *version.Version {
266-
goctlVersionResp, err := execx.Run("goctl -v", "")
267-
if err != nil {
268-
panic(err)
269-
}
266+
var (
267+
once sync.Once
268+
goctlVersion *version.Version
269+
)
270270

271-
logx.Debugf("goctl version: %s", goctlVersionResp)
272-
versionInfo := strings.Split(goctlVersionResp, " ")
273-
var goctlVersion *version.Version
274-
if len(versionInfo) >= 3 {
275-
goctlVersion, err = version.NewVersion(versionInfo[2])
271+
func (c *Config) GoctlVersion() *version.Version {
272+
once.Do(func() {
273+
goctlVersionResp, err := execx.Run("goctl -v", "")
276274
if err != nil {
277275
panic(err)
278276
}
279-
} else {
280-
panic("unknown goctl version")
281-
}
277+
278+
logx.Debugf("goctl version: %s", goctlVersionResp)
279+
versionInfo := strings.Split(goctlVersionResp, " ")
280+
if len(versionInfo) >= 3 {
281+
goctlVersion, err = version.NewVersion(versionInfo[2])
282+
if err != nil {
283+
panic(err)
284+
}
285+
}
286+
})
282287

283288
return goctlVersion
284289
}

0 commit comments

Comments
 (0)