-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new CLI command (theia version) to display the Theia version from which the theia CLI was built. Signed-off-by: Yongming Ding <dyongming@vmware.com>
- Loading branch information
Yongming Ding
committed
Jan 30, 2023
1 parent
f0246ca
commit 4ddb501
Showing
4 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 Antrea Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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. | ||
|
||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"antrea.io/theia/pkg/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// versionCmd represents the version command | ||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Show Theia CLI version", | ||
Long: `Show Theia CLI version, which is the Theia version for which this | ||
CLI was built.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println(version.GetFullVersionWithRuntimeInfo()) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2023 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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. | ||
|
||
package version | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/blang/semver" | ||
) | ||
|
||
// These variables are set at build-time. | ||
var ( | ||
// Must follow the rules in https://semver.org/ | ||
// Does not include git / build information | ||
Version = "" | ||
// Empty if git not available | ||
GitSHA = "" | ||
// Can be "dirty", "clean" or empty (if git not available) | ||
GitTreeState = "" | ||
// Can be "unreleased" or "released"; if it is "unreleased" then we add build information to | ||
// the version in GetFullVersion | ||
ReleaseStatus = "unreleased" | ||
) | ||
|
||
func GetVersion() semver.Version { | ||
v, _ := semver.Parse(Version[1:]) | ||
return v | ||
} | ||
|
||
func GetGitSHA() string { | ||
return GitSHA | ||
} | ||
|
||
// GetFullVersion returns the version string to be displayed by executables. It will look like | ||
// "<major>.<minor>.<patch>" for released versions and "<major>.<minor>.<patch>-<SHA>[.dirty]" for | ||
// unreleased versions. | ||
func GetFullVersion() string { | ||
if Version == "" { | ||
return "UNKNOWN" | ||
} | ||
if ReleaseStatus == "released" { | ||
return Version | ||
} | ||
// add build information | ||
if GitSHA == "" { | ||
return fmt.Sprintf("%s-unknown", Version) | ||
} | ||
if GitTreeState == "dirty" { | ||
return fmt.Sprintf("%s-%s.dirty", Version, GitSHA) | ||
} | ||
return fmt.Sprintf("%s-%s", Version, GitSHA) | ||
} | ||
|
||
// GetFullVersionWithRuntimeInfo returns the same version string as GetFullVersion but appends | ||
// "<GOOS>/<GOARCH> <GOVERSION>", where GOOS is the running program's operating system target | ||
// (e.g. darwin, linux), GOARCH is the the running program's architecture target (e.g. amd64), and | ||
// GOVERSION is the Go version used to build the current binary. | ||
func GetFullVersionWithRuntimeInfo() string { | ||
return fmt.Sprintf("%s %s/%s %s", GetFullVersion(), runtime.GOOS, runtime.GOARCH, runtime.Version()) | ||
} |