Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
48 changes: 44 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,59 @@ import (
"github.com/spf13/cobra"

"github.com/openfga/cli/internal/build"
"github.com/openfga/cli/internal/cmdutils"
)

var versionStr = fmt.Sprintf("v`%s` (commit: `%s`, date: `%s`)", build.Version, build.Commit, build.Date)

// versionCmd is the entrypoint for the `fga version“ command.
var forceServerVersion bool

// versionCmd is the entrypoint for the `fga version` command.
var versionCmd *cobra.Command = &cobra.Command{
Use: "version",
Short: "Reports the FGA CLI version",
Long: "Reports the FGA CLI version.",
RunE: func(_ *cobra.Command, _ []string) error {
fmt.Printf("fga version %s\n", versionStr)
Long: "Reports the FGA CLI version and OpenFGA server version if configured.",
RunE: func(cmd *cobra.Command, _ []string) error {
fmt.Printf("fga CLI version %s\n", versionStr)

// Try to get server version if configured or forced
clientConfig := cmdutils.GetClientConfig(cmd)
if clientConfig.ApiUrl != "" || forceServerVersion {
if clientConfig.ApiUrl == "" {
fmt.Println("Warning: No API URL configured. Use --force to check server version anyway.")

return nil
}

fgaClient, err := clientConfig.GetFgaClient()
if err != nil {
fmt.Printf("Warning: Could not connect to OpenFGA server: %v\n", err)

return nil
}

serverVersion, err := clientConfig.GetServerVersion(fgaClient)
if err != nil {
fmt.Printf("Warning: Could not get OpenFGA server version: %v\n", err)
fmt.Printf("openfga version v`unknown`\n")

return nil
}

fmt.Printf("openfga version v`%s`\n", serverVersion)
}

return nil
},
Args: cobra.NoArgs,
}

func init() {
versionCmd.Flags().BoolVar(
&forceServerVersion,
"force",
false,
"Force checking server version even if API URL is not configured",
)
rootCmd.AddCommand(versionCmd)
}
12 changes: 12 additions & 0 deletions internal/fga/fga.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package fga

import (
"context"
"fmt"
"strings"

openfga "github.com/openfga/go-sdk"
Expand Down Expand Up @@ -47,7 +49,7 @@
Debug bool `json:"debug,omitempty"`
}

func (c ClientConfig) getCredentials() *credentials.Credentials {

Check failure on line 52 in internal/fga/fga.go

View workflow job for this annotation

GitHub Actions / Lints

unexported method "getCredentials" for struct "ClientConfig" should be placed after the exported method "GetServerVersion" (funcorder)
if c.APIToken != "" {
return &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Expand Down Expand Up @@ -75,7 +77,7 @@
}
}

func (c ClientConfig) getClientConfig() *client.ClientConfiguration {

Check failure on line 80 in internal/fga/fga.go

View workflow job for this annotation

GitHub Actions / Lints

unexported method "getClientConfig" for struct "ClientConfig" should be placed after the exported method "GetServerVersion" (funcorder)
return &client.ClientConfiguration{
ApiUrl: c.ApiUrl,
StoreId: c.StoreID,
Expand All @@ -98,3 +100,13 @@

return fgaClient, nil
}

// GetServerVersion returns the version of the OpenFGA server.
func (c ClientConfig) GetServerVersion(fgaClient *client.OpenFgaClient) (string, error) {
store, err := fgaClient.GetStore(context.Background()).Execute()
if err != nil {
return "unknown", fmt.Errorf("failed to get store: %w", err)
}

return store.GetId(), nil
}
Loading