Skip to content

Commit

Permalink
feat: implement hardhat command with cli context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Dec 5, 2023
1 parent 477dcaf commit 687b529
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
37 changes: 36 additions & 1 deletion cli/cmd/chains/chain/hardhat/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package hardhat

import (
"strings"

"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)
Expand All @@ -13,4 +15,37 @@ It establishes a connection to the hardhat network and allows the node in execut
SetRun(hardhat).
Build()

func hardhat(cmd *cobra.Command, args []string) {}
func hardhat(cmd *cobra.Command, args []string) {
cliContext := common.GetCliWithKurtosisContext()

err := common.ValidateArgs(args)

if err != nil {
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

cliContext.Spinner().StartWithMessage("Starting Hardhat Node", "green")

responseData, err := RunHardhat(cliContext)
if err != nil {
if strings.Contains(err.Error(), "already running") {
cliContext.Spinner().StopWithMessage("Hardhat Node Already Running")
cliContext.Logger().Error(common.CodeOf(err), err.Error())
cliContext.Context().Exit(0)
} else {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatalf(common.CodeOf(err), err.Error())
}
}

err = common.WriteServiceResponseData(responseData.ServiceName, *responseData, cliContext)

if err != nil {
cliContext.Spinner().Stop()
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

cliContext.Spinner().StopWithMessage("Hardhat Node Started. Please find service details in current working directory(services.json)")

}
48 changes: 48 additions & 0 deletions cli/cmd/chains/chain/hardhat/run.go
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
package hardhat

import "github.com/hugobyte/dive-core/cli/common"

func RunHardhat(cli *common.Cli) (*common.DiveServiceResponse, error) {

enclaveContext, err := cli.Context().GetEnclaveContext(common.DiveEnclave)

if err != nil {
return nil, err
}

runConfig := common.GetStarlarkRunConfig(`{}`, common.DiveEthHardhatNodeScript, "start_hardhat_node")

response, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, runConfig)

if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

responseData, services, skippedInstructions, err := common.GetSerializedData(cli, response)
if err != nil {
err = cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

return nil, common.Errorc(common.KurtosisContextError, err.Error())

}
if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return nil, common.Errorc(common.KurtosisContextError, "Already Running")
}

hardhatResponseData := &common.DiveServiceResponse{}

result, err := hardhatResponseData.Decode([]byte(responseData))

if err != nil {
err = cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

return nil, common.Errorc(common.KurtosisContextError, err.Error())
}

return result, nil
}

0 comments on commit 687b529

Please sign in to comment.