summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/zed/internal/cmd/version.go
blob: b87ec66377279b4f7e541e47ba66e20dd4d8f62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cmd

import (
	"fmt"
	"os"

	"github.com/gookit/color"
	"github.com/jzelinskie/cobrautil/v2"
	"github.com/mattn/go-isatty"
	"github.com/spf13/cobra"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"

	"github.com/authzed/authzed-go/pkg/responsemeta"
	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"

	"github.com/authzed/zed/internal/client"
	"github.com/authzed/zed/internal/console"
)

func versionCmdFunc(cmd *cobra.Command, _ []string) error {
	if !isatty.IsTerminal(os.Stdout.Fd()) {
		color.Disable()
	}

	includeRemoteVersion := cobrautil.MustGetBool(cmd, "include-remote-version")
	hasContext := false
	if includeRemoteVersion {
		configStore, secretStore := client.DefaultStorage()
		_, err := client.GetCurrentTokenWithCLIOverride(cmd, configStore, secretStore)
		hasContext = err == nil
	}

	if hasContext && includeRemoteVersion {
		green := color.FgGreen.Render
		fmt.Print(green("client: "))
	}

	console.Println(cobrautil.UsageVersion("zed", cobrautil.MustGetBool(cmd, "include-deps")))

	if hasContext && includeRemoteVersion {
		client, err := client.NewClient(cmd)
		if err != nil {
			return err
		}

		// NOTE: we ignore the error here, as it may be due to a schema not existing, or
		// the client being unable to connect, etc. We just treat all such cases as an unknown
		// version.
		var headerMD metadata.MD
		_, _ = client.ReadSchema(cmd.Context(), &v1.ReadSchemaRequest{}, grpc.Header(&headerMD))
		version := headerMD.Get(string(responsemeta.ServerVersion))

		blue := color.FgLightBlue.Render
		fmt.Print(blue("service: "))
		if len(version) == 1 {
			console.Println(version[0])
		} else {
			console.Println("(unknown)")
		}
	}

	return nil
}