updated update check

Co-Authored-By: Naomi Kramer <naomiagoddard@gmail.com>
This commit is contained in:
Liza Tsibur
2024-06-28 00:07:51 -06:00
parent 86c60ce59c
commit 268ab78088
7 changed files with 67 additions and 18 deletions
+27
View File
@@ -1,8 +1,12 @@
package cmd
import (
"activecm/rita/config"
"activecm/rita/util"
"errors"
"fmt"
"github.com/google/go-github/github"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
)
@@ -33,3 +37,26 @@ func ConfigFlag(required bool) *cli.StringFlag {
},
}
}
func CheckForUpdate(cCtx *cli.Context, afs afero.Fs) error {
// get the current version
currentVersion := config.Version
// load config file
cfg, err := config.LoadConfig(afs, cCtx.String("config"))
if err != nil {
return fmt.Errorf("error loading config file: %w", err)
}
// check for update if version is set
if cfg.UpdateCheckEnabled && currentVersion != "" {
newer, latestVersion, err := util.CheckForNewerVersion(github.NewClient(nil), "v0.0.0")
if err != nil {
return fmt.Errorf("error checking for newer version of RITA: %w", err)
}
if newer {
fmt.Printf("\n\t✨ A newer version (%s) of RITA is available! https://github.com/activecm/rita/releases ✨\n\n", latestVersion)
}
}
return nil
}
+5
View File
@@ -68,6 +68,11 @@ var DeleteCommand = &cli.Command{
return err
}
// check for updates after running the command
if err := CheckForUpdate(cCtx, afero.NewOsFs()); err != nil {
return err
}
return nil
},
}
+12 -1
View File
@@ -93,6 +93,8 @@ var ImportCommand = &cli.Command{
if err != nil {
return err
}
// set the number of workers based on the number of CPUs
numParsers = int(math.Floor(math.Max(4, float64(runtime.NumCPU())/2)))
numDigesters = int(math.Floor(math.Max(4, float64(runtime.NumCPU())/2)))
numWriters = int(math.Floor(math.Max(4, float64(runtime.NumCPU())/2)))
@@ -102,7 +104,16 @@ var ImportCommand = &cli.Command{
// run import command
_, err = RunImportCmd(startTime, cfg, afs, cCtx.String("logs"), cCtx.String("database"), cCtx.Bool("rolling"), cCtx.Bool("rebuild"))
return err
if err != nil {
return err
}
// check for updates after running the command
if err := CheckForUpdate(cCtx, afero.NewOsFs()); err != nil {
return err
}
return nil
},
}
+5
View File
@@ -38,6 +38,11 @@ var ListCommand = &cli.Command{
return err
}
// check for updates after running the command
if err := CheckForUpdate(cCtx, afero.NewOsFs()); err != nil {
return err
}
return nil
},
}
+7 -1
View File
@@ -35,6 +35,12 @@ var ValidateConfigCommand = &cli.Command{
// validate config file
if err := RunValidateConfigCommand(afs, cCtx.String("config")); err != nil {
fmt.Printf("\n\t[!] Configuration file is not valid...")
return err
}
// check for updates after running the command
if err := CheckForUpdate(cCtx, afero.NewOsFs()); err != nil {
return err
}
@@ -54,7 +60,7 @@ func RunValidateConfigCommand(afs afero.Fs, configPath string) error {
return err
}
fmt.Println("configuration file is valid")
fmt.Printf("\n\t[✨] Configuration file is valid \n\n")
return nil
}
+10 -2
View File
@@ -77,8 +77,16 @@ var ViewCommand = &cli.Command{
}
}
err := runViewCmd(afs, cCtx.String("config"), cCtx.Args().First(), cCtx.Bool("stdout"), cCtx.String("search"), cCtx.Int("limit"))
return err
if err := runViewCmd(afs, cCtx.String("config"), cCtx.Args().First(), cCtx.Bool("stdout"), cCtx.String("search"), cCtx.Int("limit")); err != nil {
return err
}
// check for updates after running the command
if err := CheckForUpdate(cCtx, afero.NewOsFs()); err != nil {
return err
}
return nil
},
}
+1 -14
View File
@@ -4,13 +4,11 @@ import (
"activecm/rita/cmd"
"activecm/rita/config"
"activecm/rita/logger"
"activecm/rita/util"
"activecm/rita/viewer"
"fmt"
"log"
"os"
"github.com/google/go-github/github"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
)
@@ -61,17 +59,6 @@ func main() {
log.Fatal("Error loading .env file", err)
}
// check for update if version is set
if Version != "" {
newer, latestVersion, err := util.CheckForNewerVersion(github.NewClient(nil), "v0.0.0")
if err != nil {
log.Fatalf("Error checking for newer version: %v", err)
}
if newer {
fmt.Printf("\n\t✨ A newer version (%s) of RITA is available! https://github.com/activecm/rita/releases ✨\n", latestVersion)
}
}
return nil
},
}
@@ -88,7 +75,7 @@ func exitErrHandler(c *cli.Context, err error) {
if err == nil {
return
}
fmt.Fprintf(c.App.ErrWriter, "\n[!] %+v\n", err.Error())
fmt.Fprintf(c.App.ErrWriter, "\n\n\t[!] %+v\n\n", err.Error())
cli.OsExiter(1)
}