From 412259c9cc789e4f11a107f67cdf2416ce486838 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Tue, 6 Jun 2023 14:40:29 +0800 Subject: [PATCH] feat: add version subcommand (#26) --- src/core-api/Makefile | 2 +- src/core-api/cmd/root.go | 2 +- src/core-api/cmd/version.go | 48 +++++++++++++++++++++++++++++ src/core-api/pkg/version/version.go | 26 ++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/core-api/cmd/version.go create mode 100644 src/core-api/pkg/version/version.go diff --git a/src/core-api/Makefile b/src/core-api/Makefile index daaca01ac..b81ed1614 100644 --- a/src/core-api/Makefile +++ b/src/core-api/Makefile @@ -51,7 +51,7 @@ cov: .PHONY: build build: - go build -o bk-apigateway-core-api + go build -ldflags "-X core/pkg/version.Version=${VERSION} -X core/pkg/version.Commit=`git rev-parse HEAD` -X core/pkg/version.BuildTime=`date +%Y-%m-%d_%I:%M:%S` -X 'core/pkg/version.GoVersion=`go version`'" -o bk-apigateway-core-api .PHONY: serve serve: build diff --git a/src/core-api/cmd/root.go b/src/core-api/cmd/root.go index 8da6787c6..0aa94e4b7 100644 --- a/src/core-api/cmd/root.go +++ b/src/core-api/cmd/root.go @@ -33,7 +33,7 @@ import ( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "apigateway-core", + Use: "core-api", Short: "The core service for bk-apigateway", Long: `The core service for bk-apigateway, it provide a lower-level API for apisix plugin and outer service`, // Uncomment the following line if your bare application diff --git a/src/core-api/cmd/version.go b/src/core-api/cmd/version.go new file mode 100644 index 000000000..e92deead4 --- /dev/null +++ b/src/core-api/cmd/version.go @@ -0,0 +1,48 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - API 网关(BlueKing - APIGateway) available. + * Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * to the current version of the project delivered to anyone in the future. + */ + +package cmd + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" + + "core/pkg/version" +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of core-api", + Long: `All software has versions. This is core-api's`, + Run: func(cmd *cobra.Command, args []string) { + info := []string{ + "Version: " + version.Version, + "Commit: " + version.Commit, + "Build Time: " + version.BuildTime, + "Go Version: " + version.GoVersion, + } + fmt.Println(strings.Join(info, "\n")) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/src/core-api/pkg/version/version.go b/src/core-api/pkg/version/version.go new file mode 100644 index 000000000..5c09cc7ce --- /dev/null +++ b/src/core-api/pkg/version/version.go @@ -0,0 +1,26 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - API 网关(BlueKing - APIGateway) available. + * Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * to the current version of the project delivered to anyone in the future. + */ + +package version + +var ( + Version = "0.0.0" + Commit = "none" + BuildTime = "unknown" + GoVersion = "1.20.X" +)