Skip to content

Commit

Permalink
updated update check
Browse files Browse the repository at this point in the history
Co-Authored-By: Naomi Kramer <[email protected]>
  • Loading branch information
lisaSW and caffeinatedpixel committed Jun 28, 2024
1 parent 86c60ce commit 268ab78
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
27 changes: 27 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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 changes: 5 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
13 changes: 12 additions & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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
},
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
12 changes: 10 additions & 2 deletions cmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}

Expand Down
15 changes: 1 addition & 14 deletions rita.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
},
}
Expand All @@ -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)

}

0 comments on commit 268ab78

Please sign in to comment.