forked from bruin-data/bruin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2f940d
commit 8f853d4
Showing
3 changed files
with
63 additions
and
20 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
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,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func VersionCmd(commit string) *cli.Command { | ||
return &cli.Command{ | ||
Name: "version", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "output", | ||
Aliases: []string{"o"}, | ||
Usage: "the output type, possible values are: plain, json", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
outputFormat := c.String("output") | ||
version := c.App.Version | ||
|
||
res, err := http.Get("https://github.com/bruin-data/bruin/releases/latest") //nolint | ||
defer res.Body.Close() //nolint | ||
if err != nil { | ||
return errors.Wrap(err, "failed to check the latest version") | ||
} | ||
latest := strings.TrimPrefix(res.Request.URL.String(), "https://github.com/bruin-data/bruin/releases/tag/") | ||
|
||
if outputFormat == "json" { | ||
output := struct { | ||
Version string `json:"version"` | ||
Commit string `json:"commit"` | ||
Latest string `json:"latest"` | ||
}{ | ||
Version: version, | ||
Commit: commit, | ||
Latest: latest, | ||
} | ||
|
||
outputString, err := json.Marshal(output) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to marshal the output") | ||
} | ||
fmt.Println(string(outputString)) | ||
|
||
return nil | ||
} | ||
|
||
fmt.Printf("Current version: %s (%s)\n", c.App.Version, commit) | ||
fmt.Println("Latest version: " + latest) | ||
return nil | ||
}, | ||
} | ||
} |
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