Files

50 lines
1.2 KiB
Go

// Copyright (C) 2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package flags
import (
"github.com/luxfi/cli/v2/pkg/application"
"github.com/luxfi/cli/v2/pkg/prompts"
"github.com/spf13/cobra"
)
const (
rpcURLFLag = "rpc"
)
func AddRPCFlagToCmd(cmd *cobra.Command, app *application.Lux, rpc *string) {
cmd.Flags().StringVar(rpc, rpcURLFLag, "", "blockchain rpc endpoint")
rpcPreRun := func(cmd *cobra.Command, args []string) error {
if err := ValidateRPC(app, rpc, cmd, args); err != nil {
return err
}
return nil
}
existingPreRunE := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if existingPreRunE != nil {
if err := existingPreRunE(cmd, args); err != nil {
return err
}
}
return rpcPreRun(cmd, args)
}
}
func ValidateRPC(app *application.Lux, rpc *string, cmd *cobra.Command, args []string) error {
var err error
// TODO: modify check below to extend prompting for rpc to commands other than addValidator
if *rpc == "" {
if cmd.Name() == "addValidator" && len(args) == 0 {
*rpc, err = app.Prompt.CaptureURL("What is the RPC endpoint?", false)
if err != nil {
return err
}
}
return nil
}
return prompts.ValidateURLFormat(*rpc)
}