-
-
Notifications
You must be signed in to change notification settings - Fork 530
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
21ee8ed
commit 94c327c
Showing
3 changed files
with
43 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,16 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/claudiodangelis/qrcp/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type githubRelease struct { | ||
Name string `json:"name"` | ||
PublishedAt time.Time `json:"published_at"` | ||
} | ||
|
||
func getLatest() (*githubRelease, error) { | ||
var result []githubRelease | ||
resp, err := http.Get("https://api.github.com/repos/claudiodangelis/qrcp/releases?per_page=1") | ||
if err != nil { | ||
return nil, err | ||
} | ||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := json.Unmarshal(b, &result); err != nil { | ||
return nil, err | ||
} | ||
return &result[0], nil | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print version number and build information.", | ||
Run: func(c *cobra.Command, args []string) { | ||
fmt.Println(version.String()) | ||
fmt.Println("Current version:", version.String()) | ||
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) | ||
s.Suffix = " checking the latest available version" | ||
s.Start() | ||
latest, err := getLatest() | ||
s.Stop() | ||
if err != nil { | ||
fmt.Println("Unable to get latest available version from Github") | ||
} else { | ||
fmt.Printf("Latest available version: %s [date: %s]\n", latest.Name, latest.PublishedAt) | ||
} | ||
}, | ||
} |
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