Skip to content

Commit

Permalink
feat(update): #23 add fossa update command
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhao authored and elldritch committed Feb 23, 2018
1 parent 89a8aa0 commit 2e9af5d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
5 changes: 2 additions & 3 deletions build/sbt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package build

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -144,10 +143,10 @@ func (builder *SBTBuilder) IsBuilt(m module.Module, allowUnresolved bool) (bool,

// IsModule is not implemented
func (builder *SBTBuilder) IsModule(target string) (bool, error) {
return false, errors.New("IsModule is not implemented for SBTBuilder")
return false, fmt.Errorf("IsModule is not implemented for SBTBuilder")
}

// InferModule is not implemented
func (builder *SBTBuilder) InferModule(target string) (module.Module, error) {
return module.Module{}, errors.New("InferModule is not implemented for SBTBuilder")
return module.Module{}, fmt.Errorf("InferModule is not implemented for SBTBuilder")
}
10 changes: 10 additions & 0 deletions cmd/fossa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func main() {
cli.BoolFlag{Name: "debug", Usage: debugUsage},
},
},
{
Name: "update",
Usage: "Updates `fossa` to the latest version",
Action: updateCmd,
Flags: []cli.Flag{},
},
}

app.Run(os.Args)
Expand Down Expand Up @@ -244,6 +250,10 @@ func defaultCmd(c *cli.Context) {
mainLogger.Fatalf("Could not load configuration: %s", err.Error())
}

if err := checkUpdate(); err != nil {
mainLogger.Infof("An update is available for this CLI; run `fossa update` to get the latest version.")
}

if len(config.modules) == 0 {
mainLogger.Fatal("No modules specified for analysis.")
}
Expand Down
49 changes: 49 additions & 0 deletions cmd/fossa/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"github.com/blang/semver"
logging "github.com/op/go-logging"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/urfave/cli"
)

var updateLogger = logging.MustGetLogger("update")
var updateEndpoint := "fossas/fossa-cli"

func checkUpdate() error {
latest, found, err := selfupdate.DetectLatest(updateEndpoint)
if err != nil {
return err // unable to update due to GH error
}

v := semver.MustParse(version)
if !found || latest.Version.Equals(v) {
return fmt.Errorf("current binary is latest version")
}
}

func doSelfUpdate() error {
v := semver.MustParse(version)
latest, err := selfupdate.UpdateSelf(v, updateEndpoint)
if err != nil {
return err
}
if latest.Version.Equals(v) {
// latest version is the same as current version. It means current binary is up to date.
return fmt.Errorf("no update required; currently on latest")
}
return nil
}

func updateCmd(c *cli.Context) {
if err := checkUpdate(); err != nil {
updateLogger.Fatalf("unable to update (%s)", err)
}

if err := doSelfUpdate(); err != nil {
updateLogger.Fatalf("unable failed (%s)", err)
}

updateLogger.Info("fossa has been updated; run `fossa -v` to view the current version")
}

0 comments on commit 2e9af5d

Please sign in to comment.