Skip to content

Commit

Permalink
feat!: unify cmd args (#194)
Browse files Browse the repository at this point in the history
Closes #90

This PR drops the config file flag and positional argument in all but
the `init` and `start` subcommands. It also adds a `--name`/`-n` flag
for specifying a name for the devnet (which defaults to `devnet`).
  • Loading branch information
MegaRedHand authored Jan 22, 2025
1 parent e790bbc commit 5786a92
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 62 deletions.
35 changes: 16 additions & 19 deletions cmd/avs-devnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,48 @@ func main() {
app.Name = "avs-devnet"
app.Usage = "start an AVS devnet"
app.Version = version
app.Flags = append(app.Flags, &flags.KurtosisPackageFlag)

app.Commands = append(app.Commands, &cli.Command{
Name: "init",
Usage: "Initialize a devnet configuration file",
Args: true,
ArgsUsage: "[<config-file>]",
Flags: []cli.Flag{},
ArgsUsage: "[<file-name>]",
Action: cmds.InitCmd,
})

app.Commands = append(app.Commands, &cli.Command{
Name: "start",
Usage: "Start devnet from configuration file",
Args: true,
ArgsUsage: "[<config-file>]",
Flags: []cli.Flag{},
Action: cmds.StartCmd,
ArgsUsage: "[<file-name>]",
Flags: []cli.Flag{
&flags.DevnetNameFlag,
&flags.KurtosisPackageFlag,
},
Action: cmds.StartCmd,
})

app.Commands = append(app.Commands, &cli.Command{
Name: "stop",
Usage: "Stop devnet from configuration file",
Args: true,
ArgsUsage: "[<config-file>]",
Flags: []cli.Flag{},
Action: cmds.StopCmd,
Name: "stop",
Usage: "Stop devnet from configuration file",
Flags: []cli.Flag{&flags.DevnetNameFlag},
Action: cmds.StopCmd,
})

app.Commands = append(app.Commands, &cli.Command{
Name: "get-address",
Usage: "Get a devnet contract or EOA address",
Args: true,
ArgsUsage: "<contract-name>...",
Flags: []cli.Flag{&flags.ConfigFilePathFlag},
Flags: []cli.Flag{&flags.DevnetNameFlag},
Action: cmds.GetAddress,
})

app.Commands = append(app.Commands, &cli.Command{
Name: "get-ports",
Usage: "Get the published ports on the devnet",
Args: true,
ArgsUsage: "[<config-file>]",
Flags: []cli.Flag{},
Action: cmds.GetPorts,
Name: "get-ports",
Usage: "Get the published ports on the devnet",
Flags: []cli.Flag{&flags.DevnetNameFlag},
Action: cmds.GetPorts,
})

if err := app.Run(os.Args); err != nil {
Expand Down
11 changes: 7 additions & 4 deletions src/cmds/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ var DefaultKurtosisPackage string = ""

//nolint:gochecknoglobals // these are constants
var (
ConfigFilePathFlag = cli.StringFlag{
Name: "config-file",
Usage: "Path to the devnet configuration file",
Value: "devnet.yaml",
DevnetNameFlag = cli.StringFlag{
Name: "name",
TakesFile: true,
Aliases: []string{"n"},
Usage: "Assign a name to the devnet",
Value: "devnet",
DefaultText: "devnet",
}

// NOTE: this flag is for internal use.
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/get_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (

func GetAddress(ctx *cli.Context) error {
args := ctx.Args()
configFileName := ctx.String(flags.ConfigFilePathFlag.Name)
devnetName, err := EnclaveNameFromFileName(configFileName)
if err != nil {
return cli.Exit(err, 1)
}
devnetName := flags.DevnetNameFlag.Get(ctx)

kurtosisCtx, err := kurtosis.InitKurtosisContext()
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions src/cmds/get_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package cmds
import (
"fmt"

"github.com/Layr-Labs/avs-devnet/src/cmds/flags"
"github.com/Layr-Labs/avs-devnet/src/kurtosis"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)

func GetPorts(ctx *cli.Context) error {
devnetName, _, err := parseArgs(ctx)
if err != nil {
return cli.Exit(err, 1)
}
devnetName := flags.DevnetNameFlag.Get(ctx)

kurtosisCtx, err := kurtosis.InitKurtosisContext()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Creates a new devnet configuration with the given context.
func InitCmd(ctx *cli.Context) error {
_, configFileName, err := parseArgs(ctx)
configFileName, err := parseConfigFileName(ctx)
if err != nil {
return cli.Exit(err, 1)
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmds/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
// Starts the devnet with the given context.
func StartCmd(ctx *cli.Context) error {
pkgName := flags.KurtosisPackageFlag.Get(ctx)
devnetName, configPath, err := parseArgs(ctx)
devnetName := flags.DevnetNameFlag.Get(ctx)
configPath, err := parseConfigFileName(ctx)
if err != nil {
return cli.Exit(err, 1)
}
Expand Down
8 changes: 3 additions & 5 deletions src/cmds/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/Layr-Labs/avs-devnet/src/cmds/flags"
"github.com/Layr-Labs/avs-devnet/src/kurtosis"
"github.com/urfave/cli/v2"
)
Expand All @@ -13,12 +14,9 @@ var ErrEnclaveNotExists = errors.New("enclave doesn't exist")

// Stops the devnet with the given context.
func StopCmd(ctx *cli.Context) error {
devnetName, _, err := parseArgs(ctx)
if err != nil {
return cli.Exit(err, 1)
}
devnetName := flags.DevnetNameFlag.Get(ctx)
fmt.Println("Stopping devnet...")
err = Stop(ctx.Context, devnetName)
err := Stop(ctx.Context, devnetName)
if errors.Is(err, ErrEnclaveNotExists) {
return cli.Exit("Failed to find '"+devnetName+"'. Maybe it's not running?", 1)
} else if err != nil {
Expand Down
30 changes: 7 additions & 23 deletions src/cmds/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,25 @@ package cmds
import (
"errors"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/urfave/cli/v2"
)

// Parses the main arguments from the given context.
// Returns the devnet name and the configuration file name.
func parseArgs(ctx *cli.Context) (string, string, error) {
// Parses the configuration file path from the positional args.
// Fails if more than one positional arg is provided.
func parseConfigFileName(ctx *cli.Context) (string, error) {
args := ctx.Args()
if args.Len() > 1 {
return "", "", errors.New("expected exactly 1 argument: <config-file>")
return "", errors.New("expected none or 1 argument: [<file-name>]")
}
fileName := args.First()
var devnetName string

// TODO: check file exists and support yml extension
if fileName == "" {
fileName = "devnet.yaml"
devnetName = "devnet"
} else {
name, err := EnclaveNameFromFileName(fileName)
if err != nil {
return "", "", err
}
devnetName = name
return "devnet.yaml", nil
}
return devnetName, fileName, nil
return fileName, nil
}

// Checks if a file exists at the given path.
Expand All @@ -39,13 +30,6 @@ func fileExists(filePath string) bool {
return !errors.Is(err, os.ErrNotExist)
}

// Extracts the devnet name from the given configuration file name.
func EnclaveNameFromFileName(fileName string) (string, error) {
name := filepath.Base(fileName)
name = strings.Split(name, ".")[0]
return ToValidEnclaveName(name)
}

func ToValidEnclaveName(name string) (string, error) {
for _, r := range []string{"_", "/", "."} {
name = strings.ReplaceAll(name, r, "-")
Expand Down

0 comments on commit 5786a92

Please sign in to comment.