Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove alias in commands and add version injection for cli #582

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewCliCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "obocli",
Short: "OceanBase Operator Cli",
Long: "OceanBase Operator Cli tool to manage OceanBase clusters, tenants, and backups.",
Long: "OceanBase Operator Cli tool to manage OceanBase clusters, tenants, and backup policies.",
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flags().Changed("version") {
versionCmd := version.NewCmd()
Expand Down
7 changes: 3 additions & 4 deletions internal/cli/cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
// NewCmd is command for cluster management
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cluster <subcommand>",
Aliases: []string{"c"},
Short: "Command for OBCluster's management",
Long: `Command for OBCluster's management, such as Create, UpGrade, Delete, Scale, Show.`,
Use: "cluster <subcommand>",
Short: "Command for OBCluster's management",
Long: `Command for OBCluster's management, such as Create, UpGrade, Delete, Scale, Show.`,
}
cmd.AddCommand(NewCreateCmd())
cmd.AddCommand(NewDeleteCmd())
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func NewCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create <cluster_name>",
Short: "Create an ob cluster",
Aliases: []string{"c"},
Args: cobra.ExactArgs(1),
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func NewDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <cluster_name>",
Short: "Delete an ob cluster",
Aliases: []string{"d"},
Args: cobra.ExactArgs(1),
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
Expand Down
7 changes: 3 additions & 4 deletions internal/cli/cmd/cluster/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ func NewListCmd() *cobra.Command {
tbw, tbLog := cmdUtil.GetTableLoggerInstance()
logger := cmdUtil.GetDefaultLoggerInstance()
cmd := &cobra.Command{
Use: "list",
Short: "List ob clusters",
Long: `List ob clusters.`,
Aliases: []string{"ls", "l"},
Use: "list",
Short: "List ob clusters",
Long: `List ob clusters.`,
Run: func(cmd *cobra.Command, args []string) {
obclusterList, err := clients.ListAllOBClusters(cmd.Context())
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/cluster/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func NewUpdateCmd() *cobra.Command {
Short: "Update an ob cluster",
Long: "Update an ob cluster, support cpu/memory/storage",
Args: cobra.ExactArgs(1),
Aliases: []string{"ud"},
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
if err := o.Validate(); err != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/cluster/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func NewUpgradeCmd() *cobra.Command {
Short: "Upgrade an ob cluster",
Long: "Upgrade an ob cluster, please specify the new image",
Args: cobra.ExactArgs(1),
Aliases: []string{"ug"},
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
if err := o.Validate(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ func NewCmd() *cobra.Command {
o := install.NewInstallOptions()
logger := cmdUtil.GetDefaultLoggerInstance()
cmd := &cobra.Command{
Use: "install <component>",
Aliases: []string{"i"},
Short: "Command for ob-operator and components installation",
Use: "install <component>",
Short: "Command for ob-operator and components installation",
Long: `Command for ob-operator and components installation.

Currently support:
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/tenant/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func NewCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create <tenant_name> --cluster=<cluster_name>",
Short: "Create an ob tenant",
Aliases: []string{"c"},
PreRunE: o.Parse,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/tenant/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func NewDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <tenant_name>",
Short: "Delete an ob tenant",
Aliases: []string{"d"},
Args: cobra.ExactArgs(1),
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
Expand Down
7 changes: 3 additions & 4 deletions internal/cli/cmd/tenant/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ func NewListCmd() *cobra.Command {
tbw, tbLog := cmdUtil.GetTableLoggerInstance()
logger := cmdUtil.GetDefaultLoggerInstance()
cmd := &cobra.Command{
Use: "list",
Short: "List ob tenants",
Long: `List ob tenants.`,
Aliases: []string{"ls", "l"},
Use: "list",
Short: "List ob tenants",
Long: `List ob tenants.`,
Run: func(cmd *cobra.Command, args []string) {
obtenantList, err := clients.ListAllOBTenants(cmd.Context(), o.Namespace, v1.ListOptions{})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/tenant/replaylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func NewReplayLogCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "replaylog <tenant_name>",
Short: "replay log of an ob tenant",
Aliases: []string{"r", "rl"},
Args: cobra.ExactArgs(1),
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
Expand Down
7 changes: 3 additions & 4 deletions internal/cli/cmd/tenant/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
// NewCmd is command for tenant management
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "tenant",
Aliases: []string{"t"},
Short: "Command for OBTenant's management",
Long: `Command for OBTenant's management, such as Create, Update, Delete, Switchover, Activate, Replaylog.`,
Use: "tenant",
Short: "Command for OBTenant's management",
Long: `Command for OBTenant's management, such as Create, Update, Delete, Switchover, Activate, Replaylog.`,
}
cmd.AddCommand(NewCreateCmd())
cmd.AddCommand(NewDeleteCmd())
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/tenant/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func NewUpdateCmd() *cobra.Command {
Short: "Update an ob tenant",
Long: "Update an ob tenant, support unitNumber/connectWhiteList/priority of zones",
Args: cobra.ExactArgs(1),
Aliases: []string{"ud"},
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
obtenant, err := clients.GetOBTenant(cmd.Context(), types.NamespacedName{
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cmd/tenant/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func NewUpgradeCmd() *cobra.Command {
Short: "Upgrade an ob tenant to compatible version to the ob cluster",
Long: `Upgrade an ob tenant to higher version, suitable for restoring low-version backup data to a high-version ob cluster.`,
Args: cobra.ExactArgs(1),
Aliases: []string{"ug"},
PreRunE: o.Parse,
Run: func(cmd *cobra.Command, args []string) {
if err := o.Validate(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ func NewCmd() *cobra.Command {
o := update.NewUpdateOptions()
logger := cmdUtil.GetDefaultLoggerInstance()
cmd := &cobra.Command{
Use: "update <components>",
Aliases: []string{"u"},
Short: "Command for ob-operator and components update",
Use: "update <components>",
Short: "Command for ob-operator and components update",
Long: `Command for ob-operator and components update.

Currently support:
Expand Down
72 changes: 68 additions & 4 deletions internal/cli/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,83 @@ See the Mulan PSL v2 for more details.
package version

import (
"os"
"runtime"
"text/tabwriter"
"text/template"
"time"

"github.com/spf13/cobra"
)

cmdUtil "github.com/oceanbase/ob-operator/internal/cli/cmd/util"
// Injected by build script
var (
OS = ""
Arch = ""
Version = ""
CommitHash = ""
BuildTime = ""
)

// defaultVersionTemplate is the default template for displaying version information.
const defaultVersionTemplate = `
OceanBase Operatore Cli:
Version: {{.Version}}
OS/Arch: {{.OS}}/{{.Arch}}
lizzy-0323 marked this conversation as resolved.
Show resolved Hide resolved
Go Version: {{.GoVersion}}
Git Commit: {{.CommitHash}}
Build Time: {{.BuildTime}}
`

// VersionInfo stores the version information.
type VersionInfo struct {
Version string
OS string
Arch string
GoVersion string
CommitHash string
BuildTime string
}

func NewCmd() *cobra.Command {
logger := cmdUtil.GetDefaultLoggerInstance()
cmd := &cobra.Command{
Use: "version",
Short: "Print the version number of OceanBase Cli",
Run: func(cmd *cobra.Command, args []string) {
logger.Println("OceanBase Cli Version: 0.0.1")
RunE: func(cmd *cobra.Command, args []string) error {
return printCliInfo()
},
}
return cmd
}

func reformatTime(buildTime string) string {
t, err := time.Parse("20060102150405", buildTime)
if err != nil {
return buildTime
}
return t.Format("2006-01-02 15:04:05")
}

// printCliInfo prints the version information of OceanBase Cli.
func printCliInfo() error {
t := tabwriter.NewWriter(os.Stdout, 10, 1, 1, ' ', 0)
tmpl, err := template.New("version").Parse(defaultVersionTemplate)
data := &VersionInfo{
Version: Version,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
GoVersion: runtime.Version(),
CommitHash: CommitHash,
BuildTime: reformatTime(BuildTime),
}
if err != nil {
return err
}
if err := tmpl.Execute(t, data); err != nil {
return err
}
if _, err := t.Write([]byte("\n")); err != nil {
return err
}
return t.Flush()
}
12 changes: 5 additions & 7 deletions internal/cli/generated/bindata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion make/cli.mk
lizzy-0323 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ BINARY_NAME ?= obocli
CLI_VERSION ?= 0.1.0
GOARCH ?=$(shell uname -m)
GOOS ?= $(shell uname -s | tr LD ld)
VERSION_INJECT_PACKAGE=github.com/oceanbase/ob-operator/internal/cli/cmd/version
COMMIT_HASH ?= $(shell git rev-parse --short HEAD)
BUILD_TIMESTAMP ?= $(shell date '+%Y%m%d%H%M%S')

# If GOARCH not specified, set GOARCH based on the detected architecture
ifeq ($(GOARCH),x86_64)
Expand All @@ -12,8 +15,9 @@ else ifeq ($(GOARCH),aarch64)
GOARCH = arm64
endif

CLI_BUILD_FLAGS = -p 4 -ldflags="-X '$(VERSION_INJECT_PACKAGE).Version=$(CLI_VERSION)' -X '$(VERSION_INJECT_PACKAGE).CommitHash=$(COMMIT_HASH)' -X '$(VERSION_INJECT_PACKAGE).BuildTime=$(BUILD_TIMESTAMP)' "
# build args
CLI_BUILD := GO11MODULE=ON CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build
CLI_BUILD := GO11MODULE=ON CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(CLI_BUILD_FLAGS)

# build dir for obocli
BUILD_DIR?=bin/
Expand Down
Loading