-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ability to check DefraDB CLI version. (#339)
Adds the ability to check DefraDB's latest release version. Resolves #259 ``` Usage: defradb version [flags] Flags: -f, --format string The version's format can be one of: 'short', 'json' ``` Example [default]: >> `defradb version` ``` DefraDB's Version Information: * version tag = 0.2.1 * build commit = e4328e0 * release date = 2022-03-07T00:12:07Z ``` Example [short flag]: ``` >> $ defradb version -f short 0.2.1 ``` Example [json flag]: ``` >> $ defradb version --format json {"tag":"0.2.1","commit":"e4328e0","date":"2022-03-07T00:12:07Z"} ```
- Loading branch information
1 parent
726d0d3
commit 6a8f990
Showing
3 changed files
with
104 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type versionInfo struct { | ||
Tag string `json:"tag"` | ||
Commit string `json:"commit"` | ||
Date string `json:"date"` | ||
} | ||
|
||
func (v versionInfo) FullVersion() string { | ||
return fmt.Sprintf(`DefraDB's Version Information: | ||
* version tag : %s | ||
* build commit : %s | ||
* release date : %s`, | ||
color.BlueString(DefraVersion.Tag), | ||
color.GreenString(DefraVersion.Commit), | ||
color.YellowString(DefraVersion.Date), | ||
) | ||
|
||
} | ||
|
||
func (v versionInfo) JsonVersion() (string, error) { | ||
data, err := json.Marshal(v) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} | ||
|
||
var DefraVersion = versionInfo{ | ||
Tag: "0.2.1", | ||
Commit: "e4328e0", | ||
Date: "2022-03-07T00:12:07Z", | ||
} | ||
|
||
var format string | ||
|
||
// versionCmd represents the command that will output the cli version | ||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Version", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
switch strings.ToLower(format) { | ||
|
||
case "short": | ||
fmt.Println(DefraVersion.Tag) // nolint:forbidigo | ||
|
||
case "json": | ||
jVersion, err := DefraVersion.JsonVersion() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(jVersion) // nolint:forbidigo | ||
|
||
default: | ||
fmt.Println(DefraVersion.FullVersion()) // nolint:forbidigo | ||
|
||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func initVersionFormatFlag(cmd *cobra.Command) { | ||
fs := cmd.Flags() | ||
fs.SortFlags = false | ||
fs.StringVarP(&format, "format", "f", "", "The version's format can be one of: 'short', 'json'") | ||
} | ||
|
||
func init() { | ||
initVersionFormatFlag(versionCmd) | ||
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
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