Skip to content

Commit

Permalink
feat: check for minimum anvil version
Browse files Browse the repository at this point in the history
  • Loading branch information
tremarkley committed Jul 11, 2024
1 parent 3faa12f commit 6e3e0bc
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"

"github.com/ethereum-optimism/supersim"

Expand All @@ -23,6 +28,10 @@ var (
EnvVarPrefix = "SUPERSIM"
)

const (
minAnvilVersion = "0.2.0"
)

func main() {
oplog.SetupDefaults()

Expand All @@ -45,6 +54,11 @@ func main() {

func SupersimMain(ctx *cli.Context, closeApp context.CancelCauseFunc) (cliapp.Lifecycle, error) {
log := oplog.NewLogger(oplog.AppOut(ctx), oplog.ReadCLIConfig(ctx))
ok, minAnvilErr := IsMinAnvilInstalled(log)

if !ok || minAnvilErr != nil {
return nil, fmt.Errorf("Anvil version %s or higher is required.", minAnvilVersion)
}

_, err := supersim.ReadCLIConfig(ctx)
if err != nil {
Expand All @@ -59,3 +73,53 @@ func SupersimMain(ctx *cli.Context, closeApp context.CancelCauseFunc) (cliapp.Li

return s, nil
}

func IsMinAnvilInstalled(log log.Logger) (bool, error) {
cmd := exec.Command("anvil", "--version")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return false, err
}

output := out.String()

versionRegex := regexp.MustCompile(`\d+\.\d+\.\d+`)
version := versionRegex.FindString(output)

if version == "" {
return false, fmt.Errorf("Failed to parse anvil version from output")
}

log.Info(fmt.Sprintf("Anvil version installed: %s", version))

if isVersionGreaterOrEqual(version, minAnvilVersion) {
return true, nil
}

return false, nil
}

// Compares two semantic versions.
func isVersionGreaterOrEqual(version, minVersion string) bool {
v1 := strings.Split(version, ".")
v2 := strings.Split(minVersion, ".")

for i := 0; i < 3; i++ {
num1, err1 := strconv.Atoi(v1[i])
num2, err2 := strconv.Atoi(v2[i])
if err1 != nil || err2 != nil {
return false
}

if num1 > num2 {
return true
} else if num1 < num2 {
return false
}
}

// versions are equal
return true
}

0 comments on commit 6e3e0bc

Please sign in to comment.