Skip to content

Commit

Permalink
upgrade firmware command
Browse files Browse the repository at this point in the history
  • Loading branch information
solipsis committed Jun 6, 2019
1 parent 32cc974 commit d99a398
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 49 deletions.
49 changes: 0 additions & 49 deletions cmd/github/main.go

This file was deleted.

83 changes: 83 additions & 0 deletions cmd/upgradeFirmware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/google/go-github/github"
"github.com/spf13/cobra"
)

func init() {
// TODO: This default needs to be removed as soon as the latest release points to an actual firmware release instead of a bootloader one
upgradeFirmwareCmd.Flags().StringVarP(&tag, "tag", "t", "v6.1.0", "Tag of release to fetch from github")
upgradeFirmwareCmd.Flags().StringVarP(&assetName, "asset", "a", "firmware.keepkey.bin", "name of asset to fetch from release")
rootCmd.AddCommand(upgradeFirmwareCmd)
}

var tag string
var assetName string

var upgradeFirmwareCmd = &cobra.Command{
Use: "upgradeFirmware",
Short: "Upgrade firmware to a specified tagged version or latest if none is specified",
Long: "Upgrades firmware to a specified tagged version or latest if none is specified",
Run: func(cmd *cobra.Command, args []string) {

client := github.NewClient(nil)

var (
release *github.RepositoryRelease
err error
)
// fetch tagged release from github
// if no tag was specified then fetch the latest release
if tag != "" {
release, _, err = client.Repositories.GetReleaseByTag(context.Background(), "keepkey", "keepkey-firmware", tag)
} else {
release, _, err = client.Repositories.GetLatestRelease(context.Background(), "keepkey", "keepkey-firmware")
}
if err != nil {
fmt.Printf("Unable to fetch release with tag: %s\n", tag)
os.Exit(1)
}

// find the specified asset and download it
var found bool
for _, asset := range release.Assets {
if *asset.Name != assetName {
continue
}
found = true

resp, err := http.Get(asset.GetBrowserDownloadURL())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

ioutil.WriteFile(*asset.Name, body, 0644)
_, err = kk.UploadFirmware(*asset.Name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

if !found {
fmt.Printf("No asset with name: %s, was found in release: %s\n", assetName, *release.TagName)
return
}

},
}

0 comments on commit d99a398

Please sign in to comment.