Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate command args and set log formatter on fatal error #122

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cli/commands/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ Copyright © 2023 Hugobyte AI Labs <[email protected]>
package bridge

import (
"fmt"
"os"

"github.com/hugobyte/dive/cli/commands/bridge/relyas"
"github.com/hugobyte/dive/cli/common"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

func NewBridgeCmd(diveContext *common.DiveContext) *cobra.Command {
Expand All @@ -18,7 +22,20 @@ func NewBridgeCmd(diveContext *common.DiveContext) *cobra.Command {
This will create an relay to connect two different chains and pass any messages between them.`,
Run: func(cmd *cobra.Command, args []string) {

cmd.Help()
validArgs := cmd.ValidArgs
for _, c := range cmd.Commands() {
validArgs = append(validArgs, c.Name())
}
cmd.ValidArgs = validArgs

if !slices.Contains(cmd.ValidArgs, args[0]) {

diveContext.Log.SetOutput(os.Stderr)
diveContext.Error(fmt.Sprintf("Invalid Subcommand: %v", args))

cmd.Usage()
os.Exit(1)
}
},
}

Expand Down
7 changes: 6 additions & 1 deletion cli/commands/bridge/relyas/btp.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func BtpRelayCmd(diveContext *common.DiveContext) *cobra.Command {
Short: "Starts BTP Bridge between ChainA and ChainB",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())

diveContext.InitKurtosisContext()
enclaveCtx, err := diveContext.GetEnclaveContext()
Expand All @@ -62,6 +62,11 @@ func BtpRelayCmd(diveContext *common.DiveContext) *cobra.Command {
bridge, _ := cmd.Flags().GetBool("bmvbridge") // To Specify Which Type of BMV to be used in btp setup(if true BMV bridge is used else BMV-BTP Block is used)

chains := initChains(chainA, chainB, serviceA, serviceB, bridge)

if err := chains.checkForBtpSupportedChains(); err != nil {
diveContext.FatalError(err.Error(), fmt.Sprintf("Supported Chains for BTP: %v", suppottedChainsForBtp))
}

diveContext.StartSpinner(fmt.Sprintf(" Starting BTP Bridge for %s,%s", chains.chainA, chains.chainB))

if chains.areChainsIcon() {
Expand Down
14 changes: 14 additions & 0 deletions cli/commands/bridge/relyas/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
"strconv"
"strings"

"slices"

"github.com/hugobyte/dive/cli/common"
)

var suppottedChainsForBtp = []string{"icon", "eth", "hardhat"}

type Chains struct {
chainA string
chainB string
Expand Down Expand Up @@ -67,3 +71,13 @@ func (chains *Chains) getServicesResponse() (string, string, error) {

return srcChainServiceResponse, dstChainServiceResponse, nil
}

func (chains *Chains) checkForBtpSupportedChains() error {
if !slices.Contains(suppottedChainsForBtp, chains.chainA) {
return fmt.Errorf("Invalid Chain %s", chains.chainA)
}
if !slices.Contains(suppottedChainsForBtp, chains.chainB) {
return fmt.Errorf("Invalid Chain %s", chains.chainB)
}
return nil
}
2 changes: 2 additions & 0 deletions cli/commands/bridge/relyas/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func IbcRelayCmd(diveContext *common.DiveContext) *cobra.Command {
Short: "Start connection between Cosmos based chainA and ChainB and initiate communication between them",
Long: "",
Run: func(cmd *cobra.Command, args []string) {

common.ValidateCmdArgs(diveContext, args, cmd.UsageString())
diveContext.InitKurtosisContext()

enclaveCtx, err := diveContext.GetEnclaveContext()
Expand Down
27 changes: 26 additions & 1 deletion cli/commands/chain/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Copyright © 2023 Hugobyte AI Labs<[email protected]>
package chain

import (
"fmt"
"os"
"slices"

"github.com/hugobyte/dive/cli/commands/chain/types"
"github.com/hugobyte/dive/cli/common"
"github.com/spf13/cobra"
Expand All @@ -21,7 +25,20 @@ By executing this command, the node is launched, enabling network participation,
maintenance within the specified blockchain ecosystem.`,

Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
validArgs := cmd.ValidArgs
for _, c := range cmd.Commands() {
validArgs = append(validArgs, c.Name())
}
cmd.ValidArgs = validArgs

if !slices.Contains(cmd.ValidArgs, args[0]) {

diveContext.Log.SetOutput(os.Stderr)
diveContext.Error(fmt.Sprintf("Invalid Subcommand: %v", args))

cmd.Usage()
os.Exit(1)
}

},
}
Expand All @@ -34,3 +51,11 @@ maintenance within the specified blockchain ecosystem.`,
return chainCmd

}

func addSubcommandsToValidArgs(cmd *cobra.Command) {
validArgs := cmd.ValidArgs
for _, c := range cmd.Commands() {
validArgs = append(validArgs, c.Name())
}
cmd.ValidArgs = validArgs
}
2 changes: 1 addition & 1 deletion cli/commands/chain/types/archway.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewArchwayCmd(diveContext *common.DiveContext) *cobra.Command {
Short: "Build, initialize and start a archway node",
Long: "The command starts the archway network and allows node in executing contracts",
Run: func(cmd *cobra.Command, args []string) {

common.ValidateCmdArgs(diveContext, args, cmd.UsageString())
runResponse := RunArchwayNode(diveContext)

common.WriteToServiceFile(runResponse.ServiceName, *runResponse)
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/chain/types/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewEthCmd(diveContext *common.DiveContext) *cobra.Command {
It establishes a connection to the Ethereum network and allows the node in executing smart contracts and maintaining the decentralized ledger.`,
Run: func(cmd *cobra.Command, args []string) {

common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())

data := RunEthNode(diveContext)

Expand Down
2 changes: 1 addition & 1 deletion cli/commands/chain/types/hardhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewHardhatCmd(diveContext *common.DiveContext) *cobra.Command {
It establishes a connection to the hardhat network and allows the node in executing smart contracts and maintaining the decentralized ledger.`,
Run: func(cmd *cobra.Command, args []string) {

common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())

data := RunHardhatNode(diveContext)

Expand Down
2 changes: 1 addition & 1 deletion cli/commands/chain/types/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewIconCmd(diveContext *common.DiveContext) *cobra.Command {
It establishes a connection to the Icon network and allows the node in executing smart contracts and maintaining the decentralized ledger.`,
Run: func(cmd *cobra.Command, args []string) {

common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())

decentralisation, _ := cmd.Flags().GetBool("decentralisation")

Expand Down
2 changes: 1 addition & 1 deletion cli/commands/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewCleanCmd(diveContext *common.DiveContext) *cobra.Command {
Short: "Cleans up Kurtosis leftover artifacts",
Long: `Destroys and removes any running encalves. If no enclaves running to remove it will throw an error`,
Run: func(cmd *cobra.Command, args []string) {
common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())

diveContext.InitKurtosisContext()
pwd, err := os.Getwd()
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ to access the dedicated DIVE community. It allows users to engage in discussions
collaborate with other members of the DIVE community within the Discord platform.`,
Run: func(cmd *cobra.Command, args []string) {
diveContext.Log.SetOutput(os.Stdout)
common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())
diveContext.Log.Info("Redirecting to DIVE discord channel...")
if err := common.OpenFile(diveURL); err != nil {
diveContext.Log.Errorf("Failed to open Dive discord channel with error %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/tutorial/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ directing users to a curated collection of tutorial videos specifically designed
offers step-by-step instructions, tips, and demonstrations to help users better understand and utilize the features and functionalities of DIVE.`,
Run: func(cmd *cobra.Command, args []string) {
diveContext.Log.SetOutput(os.Stdout)
common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())
diveContext.Log.Info("Redirecting to YouTube...")
if err := common.OpenFile(tutorialURL); err != nil {
diveContext.Log.Errorf("Failed to open Dive YouTube chanel with error %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/twitter/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ shared by the official HugoByte Twitter account. Users can stay informed about H
community, and follow our social media presence directly from the Twitter homepage.`,
Run: func(cmd *cobra.Command, args []string) {
diveContext.Log.SetOutput(os.Stdout)
common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())
diveContext.Log.Info("Redirecting to twitter...")
if err := common.OpenFile(twitterURL); err != nil {
diveContext.Log.Errorf("Failed to open HugoByte twitter with error %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewVersionCmd(diveContext *common.DiveContext) *cobra.Command {
Short: "Prints the CLI version",
Long: `Prints the current DIVE CLI version and warns if you are using an old version.`,
Run: func(cmd *cobra.Command, args []string) {
common.ValidateCmdArgs(args, cmd.UsageString())
common.ValidateCmdArgs(diveContext, args, cmd.UsageString())
diveContext.Log.SetOutput(os.Stdout)
// Checks for latest Version
latestVersion := common.GetLatestVersion()
Expand Down
6 changes: 4 additions & 2 deletions cli/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ func WriteToFile(data string) error {
return nil
}

func ValidateCmdArgs(args []string, cmd string) {
func ValidateCmdArgs(diveContext *DiveContext, args []string, cmd string) {
if len(args) != 0 {
logrus.Fatalf("Invalid Usage of command. Find cmd %s", cmd)

diveContext.FatalError("Invalid Usage of command", cmd)

}
}
Expand All @@ -144,6 +145,7 @@ func setupLogger() *logrus.Logger {
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
ForceColors: true,
PadLevelText: true,
})

ditFilePath := pwd + DiveLogDirectory + DiveDitLogFile
Expand Down
1 change: 1 addition & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
)

require (
Expand Down