Skip to content

Commit

Permalink
feat: add berty update (dry-run)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 21, 2018
1 parent e1a992c commit a23a353
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/cmd/berty/root.go
Expand Up @@ -90,6 +90,7 @@ func newRootCommand() *cobra.Command {
newClientCommand(),
newSQLCommand(),
newIdentityCommand(),
newUpdateCommand(),
)

viper.AutomaticEnv()
Expand Down
83 changes: 83 additions & 0 deletions core/cmd/berty/update.go
@@ -0,0 +1,83 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"berty.tech/core"
)

type updateOptions struct {
}

func updateSetupFlags(flags *pflag.FlagSet, opts *updateOptions) {
_ = viper.BindPFlags(flags)
}

func newUpdateCommand() *cobra.Command {
opts := &updateOptions{}
cmd := &cobra.Command{
Use: "update",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.Unmarshal(opts); err != nil {
return err
}
return update(opts)
},
}

updateSetupFlags(cmd.Flags(), opts)
return cmd
}

func update(opts *updateOptions) error {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, "https://yolo.berty.io/release/ios.json", nil)
if err != nil {
return errors.Wrap(err, "failed to create http request")
}
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return errors.Wrap(err, "failed to fetch release file")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "failed to read body")
}
type release struct {
Branch string `json:"branch"`
StopTime time.Time `json:"stop-time"`
Author string `json:"author"`
GitSha string `json:"git-sha"`
BuildURL string `json:"build-url"`
Body string `json:"body"`
ManifestURL string `json:"manifest-url"`
}
ret := struct {
Master *release `json:"master"`
LatestPRs []*release `json:"latest-prs"`
}{}
if err := json.Unmarshal(body, &ret); err != nil {
return errors.Wrap(err, "invalid json body")
}

if core.CommitDate().Before(ret.Master.StopTime) {
out, _ := json.MarshalIndent(ret.Master, "", " ")
fmt.Printf("new master update available: %s\n", string(out))
} else {
fmt.Println("you are already up to date")
}

return nil
}
2 changes: 1 addition & 1 deletion core/manager/account/account.go
Expand Up @@ -210,7 +210,7 @@ func (a *Account) Open() error {
zap.String("git-sha", core.GitSha),
zap.String("git-branch", core.GitBranch),
zap.String("build-mode", core.BuildMode),
zap.String("commit-date", core.CommitDate()),
zap.String("commit-date", core.CommitDate().String()),
zap.String("db", a.dbPath()),
zap.String("name", a.Name),
)
Expand Down
9 changes: 4 additions & 5 deletions core/version.go
Expand Up @@ -18,15 +18,14 @@ var (
commitDate = "undefined"
)

func CommitDate() string {
func CommitDate() time.Time {
if commitDate == "undefined" {
return "undefined"
return time.Time{}
}

commitDateInt, err := strconv.ParseInt(commitDate, 10, 64)
if err != nil {
return "parse-error"
return time.Time{}
}
tm := time.Unix(commitDateInt, 0)
return tm.Format(time.RFC3339)
return time.Unix(commitDateInt, 0)
}

0 comments on commit a23a353

Please sign in to comment.