From 5d09cd0e7c95349f3c4c2ff75c274f219d9f7955 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 14 May 2022 16:00:20 +0800 Subject: [PATCH] use goproxy properly, remove files (#1903) --- tools/goctl/docker/docker.go | 6 ++---- tools/goctl/quickstart/micro.go | 2 +- tools/goctl/quickstart/mono.go | 4 ++-- tools/goctl/quickstart/run.go | 17 +++++++++++++---- tools/goctl/util/env/env.go | 8 ++++++++ 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index a2ab50dd..0dbc42f4 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -7,11 +7,11 @@ import ( "path/filepath" "strings" "text/template" - "time" "github.com/logrusorgru/aurora" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/tools/goctl/util" + "github.com/zeromicro/go-zero/tools/goctl/util/env" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) @@ -19,7 +19,6 @@ const ( dockerfileName = "Dockerfile" etcDir = "etc" yamlEtx = ".yaml" - cstOffset = 60 * 60 * 8 // 8 hours offset for Chinese Standard Time ) // Docker describes a dockerfile @@ -152,10 +151,9 @@ func generateDockerfile(goFile, base string, port int, version, timezone string, builder.WriteString(`, "` + arg + `"`) } - _, offset := time.Now().Zone() t := template.Must(template.New("dockerfile").Parse(text)) return t.Execute(out, Docker{ - Chinese: offset == cstOffset, + Chinese: env.InChina(), GoRelPath: projPath, GoFile: goFile, ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)), diff --git a/tools/goctl/quickstart/micro.go b/tools/goctl/quickstart/micro.go index 6122c2e3..1b8d0203 100644 --- a/tools/goctl/quickstart/micro.go +++ b/tools/goctl/quickstart/micro.go @@ -67,7 +67,7 @@ func (m micro) start() { goModTidy(projectDir) sg := service.NewServiceGroup() sg.Add(serviceImpl{func() { - log.Debug(">> Ready to start an zRPC server...") + log.Debug(">> Ready to start a zRPC server...") goStart(zRPCWorkDir) }}) sg.Add(serviceImpl{func() { diff --git a/tools/goctl/quickstart/mono.go b/tools/goctl/quickstart/mono.go index 7a14227f..7f91c655 100644 --- a/tools/goctl/quickstart/mono.go +++ b/tools/goctl/quickstart/mono.go @@ -73,7 +73,7 @@ func (m mono) start() { if !m.callRPC { goModTidy(projectDir) } - log.Debug(">> Ready to start an api server...") - log.Debug(">> Try to execute 'curl --request POST http://127.0.0.1:8888/ping' after service startup...") + log.Debug(">> Ready to start an API server...") + log.Debug(">> Run 'curl -X POST http://127.0.0.1:8888/ping' after service startup...") goStart(apiWorkDir) } diff --git a/tools/goctl/quickstart/run.go b/tools/goctl/quickstart/run.go index 998ac1ba..662b2e7d 100644 --- a/tools/goctl/quickstart/run.go +++ b/tools/goctl/quickstart/run.go @@ -5,18 +5,19 @@ import ( "os/exec" "runtime" + "github.com/zeromicro/go-zero/tools/goctl/util/env" "github.com/zeromicro/go-zero/tools/goctl/vars" ) +const goproxy = "GOPROXY=https://goproxy.cn,direct" + func goStart(dir string) { - goproxy := "GOPROXY=https://goproxy.cn" - execCommand(dir, "go run .", goproxy) + execCommand(dir, "go run .", prepareGoProxyEnv()...) } func goModTidy(dir string) int { - goproxy := "GOPROXY=https://goproxy.cn" log.Debug(">> go mod tidy") - return execCommand(dir, "go mod tidy", goproxy) + return execCommand(dir, "go mod tidy", prepareGoProxyEnv()...) } func execCommand(dir string, arg string, envArgs ...string) int { @@ -33,3 +34,11 @@ func execCommand(dir string, arg string, envArgs ...string) int { _ = cmd.Run() return cmd.ProcessState.ExitCode() } + +func prepareGoProxyEnv(envArgs ...string) []string { + if env.InChina() { + return append(envArgs, goproxy) + } + + return envArgs +} diff --git a/tools/goctl/util/env/env.go b/tools/goctl/util/env/env.go index 93c95314..c87e7cf0 100644 --- a/tools/goctl/util/env/env.go +++ b/tools/goctl/util/env/env.go @@ -6,6 +6,7 @@ import ( "path/filepath" "runtime" "strings" + "time" "github.com/zeromicro/go-zero/tools/goctl/vars" ) @@ -16,8 +17,15 @@ const ( binProtoc = "protoc" binProtocGenGo = "protoc-gen-go" binProtocGenGrpcGo = "protoc-gen-go-grpc" + cstOffset = 60 * 60 * 8 // 8 hours offset for Chinese Standard Time ) +// InChina returns whether the current time is in China Standard Time. +func InChina() bool { + _, offset := time.Now().Zone() + return offset == cstOffset +} + // LookUpGo searches an executable go in the directories // named by the GOROOT/bin or PATH environment variable. func LookUpGo() (string, error) {