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

Show Server Version in Yorkie CLI #938

15 changes: 15 additions & 0 deletions admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,21 @@ func (c *Client) ListChangeSummaries(
return summaries, nil
}

// GetServerVersion gets the server version.
func (c *Client) GetServerVersion(ctx context.Context,
) (*types.VersionDetail, error) {
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
response, err := c.client.GetServerVersion(ctx, connect.NewRequest(&api.GetServerVersionRequest{}))
if err != nil {
return nil, err
}

return &types.VersionDetail{
YorkieVersion: response.Msg.YorkieVersion,
GoVersion: response.Msg.GoVersion,
BuildDate: response.Msg.BuildDate,
}, nil
}

/**
* withShardKey returns a context with the given shard key in metadata.
*/
Expand Down
56 changes: 56 additions & 0 deletions api/docs/yorkie/v1/admin.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ paths:
$ref: '#/components/responses/connect.error'
tags:
- yorkie.v1.AdminService
/yorkie.v1.AdminService/GetServerVersion:
post:
description: ""
requestBody:
$ref: '#/components/requestBodies/yorkie.v1.AdminService.GetServerVersion.yorkie.v1.GetServerVersionRequest'
responses:
"200":
$ref: '#/components/responses/yorkie.v1.AdminService.GetServerVersion.yorkie.v1.GetServerVersionResponse'
default:
$ref: '#/components/responses/connect.error'
tags:
- yorkie.v1.AdminService
/yorkie.v1.AdminService/GetSnapshotMeta:
post:
description: ""
Expand Down Expand Up @@ -246,6 +258,15 @@ components:
schema:
$ref: '#/components/schemas/yorkie.v1.GetProjectRequest'
required: true
yorkie.v1.AdminService.GetServerVersion.yorkie.v1.GetServerVersionRequest:
content:
application/json:
schema:
$ref: '#/components/schemas/yorkie.v1.GetServerVersionRequest'
application/proto:
schema:
$ref: '#/components/schemas/yorkie.v1.GetServerVersionRequest'
required: true
yorkie.v1.AdminService.GetSnapshotMeta.yorkie.v1.GetSnapshotMetaRequest:
content:
application/json:
Expand Down Expand Up @@ -391,6 +412,15 @@ components:
schema:
$ref: '#/components/schemas/yorkie.v1.GetProjectResponse'
description: ""
yorkie.v1.AdminService.GetServerVersion.yorkie.v1.GetServerVersionResponse:
content:
application/json:
schema:
$ref: '#/components/schemas/yorkie.v1.GetServerVersionResponse'
application/proto:
schema:
$ref: '#/components/schemas/yorkie.v1.GetServerVersionResponse'
description: ""
yorkie.v1.AdminService.GetSnapshotMeta.yorkie.v1.GetSnapshotMetaResponse:
content:
application/json:
Expand Down Expand Up @@ -868,6 +898,32 @@ components:
type: object
title: GetProjectResponse
type: object
yorkie.v1.GetServerVersionRequest:
additionalProperties: false
description: ""
title: GetServerVersionRequest
type: object
yorkie.v1.GetServerVersionResponse:
additionalProperties: false
description: ""
properties:
buildDate:
additionalProperties: false
description: ""
title: build_date
type: string
goVersion:
additionalProperties: false
description: ""
title: go_version
type: string
yorkieVersion:
additionalProperties: false
description: ""
title: yorkie_version
type: string
title: GetServerVersionResponse
type: object
yorkie.v1.GetSnapshotMetaRequest:
additionalProperties: false
description: ""
Expand Down
22 changes: 22 additions & 0 deletions api/types/version_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package types

// VersionInfo represents information of version.
type VersionInfo struct {
// ClientVersion is the yorkie cli version.
ClientVersion *VersionDetail `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`

// ServerVersion is the yorkie server version.
ServerVersion *VersionDetail `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
}

// VersionDetail represents detail information of version.
type VersionDetail struct {
// YorkieVersion
YorkieVersion string `json:"yorkieVersion" yaml:"yorkieVersion"`

// GoVersion
GoVersion string `json:"goVersion" yaml:"goVersion"`

// BuildDate
BuildDate string `json:"buildDate" yaml:"buildDate"`
}
386 changes: 265 additions & 121 deletions api/yorkie/v1/admin.pb.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions api/yorkie/v1/admin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ service AdminService {
rpc SearchDocuments (SearchDocumentsRequest) returns (SearchDocumentsResponse) {}

rpc ListChanges (ListChangesRequest) returns (ListChangesResponse) {}

rpc GetServerVersion (GetServerVersionRequest) returns (GetServerVersionResponse) {}
}

message SignUpRequest {
Expand Down Expand Up @@ -184,3 +186,11 @@ message ListChangesRequest {
message ListChangesResponse {
repeated Change changes = 1;
}

message GetServerVersionRequest {}

message GetServerVersionResponse {
string yorkie_version = 1;
string go_version = 2;
string build_date = 3;
}
27 changes: 27 additions & 0 deletions api/yorkie/v1/v1connect/admin.connect.go

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

137 changes: 131 additions & 6 deletions cmd/yorkie/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,152 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"

"github.com/yorkie-team/yorkie/admin"
"github.com/yorkie-team/yorkie/api/types"
"github.com/yorkie-team/yorkie/cmd/yorkie/config"
"github.com/yorkie-team/yorkie/internal/version"
)

var (
clientOnly bool
output string
)

func newVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number of Yorkie",
Use: "version",
Short: "Print the version number of Yorkie",
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Yorkie: %s\n", version.Version)
fmt.Printf("Go: %s\n", runtime.Version())
fmt.Printf("Build date: %s\n", version.BuildDate)
if err := Validate(); err != nil {
return err
}

var versionInfo types.VersionInfo
versionInfo.ClientVersion = getYorkieClientVersion()
hackerwins marked this conversation as resolved.
Show resolved Hide resolved

serverVersionChan := make(chan *types.VersionDetail)
errorChan := make(chan error)

if !clientOnly {
go func() {
rpcAddr := viper.GetString("rpcAddr")
auth, err := config.LoadAuth(rpcAddr)
if err != nil {
errorChan <- err
return
}
hackerwins marked this conversation as resolved.
Show resolved Hide resolved

cli, err := admin.Dial(rpcAddr, admin.WithToken(auth.Token), admin.WithInsecure(auth.Insecure))
if err != nil {
errorChan <- err
return
}
defer cli.Close()

ctx := context.Background()
sv, err := cli.GetServerVersion(ctx)
if err != nil {
errorChan <- err
return
}

serverVersionChan <- sv
}()
}

var serverErr error

if !clientOnly {
select {
case sv := <-serverVersionChan:
versionInfo.ServerVersion = sv
case err := <-errorChan:
serverErr = err
}
}

switch output {
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
case "":
cmd.Printf("Yorkie Client: %s\n", versionInfo.ClientVersion.YorkieVersion)
cmd.Printf("Go: %s\n", versionInfo.ClientVersion.GoVersion)
cmd.Printf("Build Date: %s\n", versionInfo.ClientVersion.BuildDate)
if versionInfo.ServerVersion != nil {
cmd.Printf("Yorkie Server: %s\n", versionInfo.ServerVersion.YorkieVersion)
cmd.Printf("Go: %s\n", versionInfo.ServerVersion.GoVersion)
cmd.Printf("Build Date: %s\n", versionInfo.ServerVersion.BuildDate)
}
case "yaml":
marshalled, err := yaml.Marshal(&versionInfo)
if err != nil {
return errors.New("failed to marshal YAML")
}
fmt.Println(string(marshalled))
case "json":
marshalled, err := json.MarshalIndent(&versionInfo, "", " ")
if err != nil {
return errors.New("failed to marshal JSON")
}
fmt.Println(string(marshalled))
}

if serverErr != nil {
cmd.Printf("Error fetching server version: ")
if strings.Contains(serverErr.Error(), "unimplemented") {
cmd.Printf("The server does not support this operation. You might need to check your server version.\n")
} else {
cmd.Print(serverErr)
}
}

krapie marked this conversation as resolved.
Show resolved Hide resolved
return nil
},
}
}

// Validate validates the provided options.
func Validate() error {
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
if output != "" && output != "yaml" && output != "json" {
return errors.New(`--output must be 'yaml' or 'json'`)
}

return nil
}

func getYorkieClientVersion() *types.VersionDetail {
return &types.VersionDetail{
YorkieVersion: version.Version,
GoVersion: runtime.Version(),
BuildDate: version.BuildDate,
}
}

func init() {
rootCmd.AddCommand(newVersionCmd())
cmd := newVersionCmd()
cmd.Flags().BoolVar(
&clientOnly,
"client",
clientOnly,
"Shows client version only. (no server required)",
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
)
cmd.Flags().StringVarP(
&output,
"output",
"o",
output,
"One of 'yaml' or 'json'.",
)

rootCmd.AddCommand(cmd)
}
Loading
Loading