Add check for app update (#88)

* add check for app update; fix ETUI error handling

* validate user args
This commit is contained in:
Alex Goodman
2020-07-21 12:02:03 -04:00
committed by GitHub
parent 380cd39514
commit 7ebb9f4e0b
19 changed files with 438 additions and 66 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ func init() {
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Errorf("could not start application: %w", err)
log.Errorf(err.Error())
os.Exit(1)
}
}
+26 -6
View File
@@ -9,7 +9,9 @@ import (
"github.com/anchore/imgbom/imgbom/presenter"
"github.com/anchore/imgbom/internal"
"github.com/anchore/imgbom/internal/bus"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/imgbom/internal/ui"
"github.com/anchore/imgbom/internal/version"
"github.com/spf13/cobra"
"github.com/wagoodman/go-partybus"
)
@@ -26,9 +28,13 @@ Supports the following image sources:
`, map[string]interface{}{
"appName": internal.ApplicationName,
}),
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
os.Exit(doRunCmd(cmd, args))
err := doRunCmd(cmd, args)
if err != nil {
log.Errorf(err.Error())
os.Exit(1)
}
},
}
@@ -37,6 +43,21 @@ func startWorker(userInput string) <-chan error {
go func() {
defer close(errs)
if appConfig.CheckForAppUpdate {
isAvailable, newVersion, err := version.IsUpdateAvailable()
if err != nil {
log.Errorf(err.Error())
}
if isAvailable {
log.Infof("New version of %s is available: %s", internal.ApplicationName, newVersion)
bus.Publish(partybus.Event{
Type: event.AppUpdateAvailable,
Value: newVersion,
})
}
}
catalog, scope, _, err := imgbom.Catalog(userInput, appConfig.ScopeOpt)
if err != nil {
errs <- fmt.Errorf("failed to catalog input: %+v", err)
@@ -51,10 +72,9 @@ func startWorker(userInput string) <-chan error {
return errs
}
func doRunCmd(_ *cobra.Command, args []string) int {
errs := startWorker(args[0])
func doRunCmd(_ *cobra.Command, args []string) error {
userInput := args[0]
errs := startWorker(userInput)
ux := ui.Select(appConfig.CliOptions.Verbosity > 0, appConfig.Quiet)
return ux(errs, eventSubscription)
}
+18 -13
View File
@@ -4,16 +4,11 @@ import (
"fmt"
"github.com/anchore/imgbom/internal"
"github.com/anchore/imgbom/internal/version"
"github.com/spf13/cobra"
)
type Version struct {
Version string
Commit string
BuildTime string
}
var version *Version
var showVerboseVersionInfo bool
var versionCmd = &cobra.Command{
Use: "version",
@@ -22,13 +17,23 @@ var versionCmd = &cobra.Command{
}
func init() {
versionCmd.Flags().BoolVarP(&showVerboseVersionInfo, "verbose", "v", false, "show additional version information")
rootCmd.AddCommand(versionCmd)
}
func SetVersion(v *Version) {
version = v
}
func printVersion(cmd *cobra.Command, args []string) {
fmt.Printf("%s %s\n", internal.ApplicationName, version.Version)
func printVersion(_ *cobra.Command, _ []string) {
versionInfo := version.FromBuild()
if showVerboseVersionInfo {
fmt.Println("Application: ", internal.ApplicationName)
fmt.Println("Version: ", versionInfo.Version)
fmt.Println("BuildDate: ", versionInfo.BuildDate)
fmt.Println("GitCommit: ", versionInfo.GitCommit)
fmt.Println("GitTreeState: ", versionInfo.GitTreeState)
fmt.Println("Platform: ", versionInfo.Platform)
fmt.Println("GoVersion: ", versionInfo.GoVersion)
fmt.Println("Compiler: ", versionInfo.Compiler)
} else {
fmt.Printf("%s %s\n", internal.ApplicationName, versionInfo.Version)
}
}