Skip to content

Commit

Permalink
Merge branch 'main' into marshal-matters
Browse files Browse the repository at this point in the history
  • Loading branch information
archbear authored Jun 18, 2024
2 parents 443e67f + 1abdb40 commit f1367da
Show file tree
Hide file tree
Showing 38 changed files with 644 additions and 388 deletions.
58 changes: 42 additions & 16 deletions beacond/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"log/slog"
"os"

"github.com/berachain/beacon-kit/mod/cli/pkg/components"
clibuilder "github.com/berachain/beacon-kit/mod/cli/pkg/builder"
clicomponents "github.com/berachain/beacon-kit/mod/cli/pkg/components"
nodebuilder "github.com/berachain/beacon-kit/mod/node-core/pkg/builder"
nodecomponents "github.com/berachain/beacon-kit/mod/node-core/pkg/components"
beacon "github.com/berachain/beacon-kit/mod/node-core/pkg/components/module"
"github.com/berachain/beacon-kit/mod/node-core/pkg/types"
"github.com/cosmos/cosmos-sdk/server"
"go.uber.org/automaxprocs/maxprocs"
Expand All @@ -41,37 +43,61 @@ func run() error {

// Build the node using the node-core.
nb := nodebuilder.New(
// Set the Name to the Default.
nodebuilder.WithName[types.NodeI](
nodebuilder.DefaultAppName),
// Set the Description to the Default.
nodebuilder.WithDescription[types.NodeI](
nodebuilder.DefaultDescription),
// Set the DepInject Configuration to the Default.
nodebuilder.WithDepInjectConfig[types.NodeI](
nodebuilder.DefaultDepInjectConfig()),
// Set the Runtime Components to the Default.
nodebuilder.WithComponents[types.NodeI](
nodecomponents.DefaultComponentsWithStandardTypes(),
),
// Set the Client Components to the Default.
nodebuilder.WithClientComponents[types.NodeI](
components.DefaultClientComponents(),
)

// Build the root command using the builder
cb := clibuilder.New(
// Set the Name to the Default.
clibuilder.WithName[types.NodeI](nodebuilder.DefaultAppName),
// Set the Description to the Default.
clibuilder.WithDescription[types.NodeI](nodebuilder.DefaultDescription),
// Set the DepInject Configuration to the Default.
clibuilder.WithDepInjectConfig[types.NodeI](
nodebuilder.DefaultDepInjectConfig(),
),
// Set the Runtime Components to the Default.
clibuilder.WithComponents[types.NodeI](
append(
clicomponents.DefaultClientComponents(),
// TODO: remove these, and eventually pull cfg and chainspec
// from built node
nodecomponents.ProvideNoopTxConfig,
nodecomponents.ProvideConfig,
nodecomponents.ProvideChainSpec,
),
),
clibuilder.SupplyModuleDeps[types.NodeI](
beacon.SupplyModuleDependencies(),
),
// TODO: this is hood and needs to be refactored.
nodebuilder.WithTODORemoveRunHandler[types.NodeI](
// Set the Run Handler to the Default.
clibuilder.WithRunHandler[types.NodeI](
server.InterceptConfigsPreRunHandler,
),
// Set the AppCreator to the NodeBuilder AppCreator.
clibuilder.WithAppCreator[types.NodeI](nb.AppCreator),
)

// Assemble the node with all our components.
node, err := nb.Build()
// we never have to call nb.build() because this function is passed
// to the cli through the clibuilder.WithAppCreator option, eventually to be
// called by the cosmos-sdk

cmd, err := cb.Build()
if err != nil {
return err
}

// TODO: create a "runner" type harness that takes the node as a parameter.
return node.Run(components.DefaultNodeHome)
// eventually we want to decouple from cosmos cli, and just pass in a built
// Node and Cmd to a runner

// for now, running the cmd will start the node
return cmd.Run(clicomponents.DefaultNodeHome)
}

// main is the entry point.
Expand Down
4 changes: 2 additions & 2 deletions beacond/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ replace (
)

require (
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240614154006-a5defa6198f5
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240617040308-8f08818d38a4
github.com/berachain/beacon-kit/mod/node-core v0.0.0-20240614135647-9814c0c41144
github.com/cosmos/cosmos-sdk v0.51.0
go.uber.org/automaxprocs v1.5.3
Expand Down Expand Up @@ -48,7 +48,7 @@ require (
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/berachain/beacon-kit/mod/async v0.0.0-20240614154006-a5defa6198f5 // indirect
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8 // indirect
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240617161612-ab1257fcf5a1 // indirect
github.com/berachain/beacon-kit/mod/config v0.0.0-20240614154006-a5defa6198f5 // indirect
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240614154006-a5defa6198f5 // indirect
Expand Down
8 changes: 4 additions & 4 deletions beacond/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240614154006-a5defa6198f5 h1:laEFwO0v6zLXgh0LBULeTfqnMR9K36lbGp4Z2k9nWHM=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240614154006-a5defa6198f5/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8 h1:u7RuhmBWN5Z5fjEL9pYkRXg/AUbEj6xi5zCF47sXp2Q=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I=
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240617161612-ab1257fcf5a1 h1:oaPzJakH41f6Oprj0ldlR03aqsCffZYYTFmCC2ad3kg=
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240617161612-ab1257fcf5a1/go.mod h1:Bw0cs7rgkYv6DG0BNNYQ56n59XxPWY3ZXoJPyKoT2Ro=
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240614154006-a5defa6198f5 h1:WdcB+EL2WGWD6QySPdsf6ZW7RvACwkdIz5lpFR89THg=
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240614154006-a5defa6198f5/go.mod h1:KBIgO+aFAQkLpFJ+27DFRmrGPlpBjr/7gdQYSy9UnA4=
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240617040308-8f08818d38a4 h1:ZyIz86twB3Y11rFWgqNEJTJGfrTyqnvrOQNaw44PGlQ=
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240617040308-8f08818d38a4/go.mod h1:4LqfmvrvTCvXZo/6FcnvC089VaJJN5n9N2JIOiYJjnY=
github.com/berachain/beacon-kit/mod/config v0.0.0-20240614154006-a5defa6198f5 h1:KqQcNurpXlZYZ3r/+WR3j50+KjMWu2iZ1DDk1MdbrWk=
github.com/berachain/beacon-kit/mod/config v0.0.0-20240614154006-a5defa6198f5/go.mod h1:rBTpMivjYpzaQk/tKsp1pqj3jev4YJ5PPfUHhcpowCc=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240614154006-a5defa6198f5 h1:tF5zeC+OzfoVuE86LcE2Z9xyJSkBiMjEhejBKTRw6jE=
Expand Down
2 changes: 1 addition & 1 deletion contracts/lib/solady
6 changes: 2 additions & 4 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,13 @@ github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd3
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible h1:Ppm0npCCsmuR9oQaBtRuZcmILVE74aXE+AmrJj8L2ns=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240617040308-8f08818d38a4/go.mod h1:4LqfmvrvTCvXZo/6FcnvC089VaJJN5n9N2JIOiYJjnY=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240601211557-8654b92bbf10/go.mod h1:1ARLVWK4DisWnCwrYaRVphAb87JJfFBgkt98z7cq4I0=
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240508035017-2fb637ea5f0a/go.mod h1:0UT2gj5UW4QDKpIu7cc73akTKZUeCiroT7sp+oHEz20=
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240610174140-6c6ffa31d278/go.mod h1:bFkZmv0d7P9uoqJOw7ruG29M3oVwXslttLRuNwxzTOQ=
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240610174256-5d1223718dd6/go.mod h1:bFkZmv0d7P9uoqJOw7ruG29M3oVwXslttLRuNwxzTOQ=
github.com/berachain/beacon-kit/mod/execution v0.0.0-20240506203005-b920effebbe8/go.mod h1:vu3XA+lcu0pj08d4vfFT9Cp42+OhjoIpgC5AgH6ku+Q=
github.com/berachain/beacon-kit/mod/execution v0.0.0-20240617185735-42326b5546a8/go.mod h1:QiaIGIPh5htIO+CDatTHARMwKwAfoTL8eg2SBpyHbxQ=
github.com/berachain/beacon-kit/mod/log v0.0.0-20240506203005-b920effebbe8/go.mod h1:U5YBXSbq2odNSTLvtwKlA/2hO25C7Uk7gubbaBVDIn0=
github.com/berachain/beacon-kit/mod/log v0.0.0-20240508035017-2fb637ea5f0a/go.mod h1:U5YBXSbq2odNSTLvtwKlA/2hO25C7Uk7gubbaBVDIn0=
github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240429161625-c105cec3420c/go.mod h1:rqZvVlIpK+KXXfYD01+QILvxz4P7YTau5DGDBsEK3SU=
Expand Down Expand Up @@ -1775,8 +1777,6 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190327091125-710a502c58a2/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down Expand Up @@ -1970,8 +1970,6 @@ golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
Expand Down
4 changes: 1 addition & 3 deletions mod/beacon/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ module github.com/berachain/beacon-kit/mod/beacon

go 1.22.4

replace github.com/berachain/beacon-kit/mod/async => ../async

require (
github.com/berachain/beacon-kit/mod/async v0.0.0-00010101000000-000000000000
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240610210054-bfdc14c4013c
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240610210054-bfdc14c4013c
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240610210054-bfdc14c4013c
Expand Down
2 changes: 2 additions & 0 deletions mod/beacon/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjC
github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8 h1:u7RuhmBWN5Z5fjEL9pYkRXg/AUbEj6xi5zCF47sXp2Q=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240610210054-bfdc14c4013c h1:KD3RKYFpX0Irm9Al+Jgwo5Ja2jWXs2zd1qWVWtNJWao=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:eX2lsU4fU8yD++Aaw2Hw8xojn6OECDKx4shpr07CqnI=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240610210054-bfdc14c4013c h1:HiklyIM4rdXlO5evT0VCuiIofVDZxNC9x1lIIDyNcsM=
Expand Down
3 changes: 1 addition & 2 deletions mod/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22.4
replace (
// The following are required to build with the latest version of the cosmos-sdk main branch:
cosmossdk.io/api => cosmossdk.io/api v0.7.3-0.20240530104414-90cbb022d5f6
github.com/berachain/beacon-kit/mod/config => ../config
github.com/cosmos/cosmos-sdk => github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240529213909-58c32d695e1a
)

Expand Down Expand Up @@ -58,7 +57,7 @@ require (
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/berachain/beacon-kit/mod/async v0.0.0-20240614154006-a5defa6198f5 // indirect
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8 // indirect
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240617161612-ab1257fcf5a1 // indirect
github.com/berachain/beacon-kit/mod/da v0.0.0-20240614154006-a5defa6198f5 // indirect
github.com/berachain/beacon-kit/mod/execution v0.0.0-20240614154006-a5defa6198f5 // indirect
Expand Down
6 changes: 4 additions & 2 deletions mod/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240614154006-a5defa6198f5 h1:laEFwO0v6zLXgh0LBULeTfqnMR9K36lbGp4Z2k9nWHM=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240614154006-a5defa6198f5/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8 h1:u7RuhmBWN5Z5fjEL9pYkRXg/AUbEj6xi5zCF47sXp2Q=
github.com/berachain/beacon-kit/mod/async v0.0.0-20240617185735-42326b5546a8/go.mod h1:ycwqumRG49gb8qg87cc6kVgPeiUDaFMajjLko54Ey+I=
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240617161612-ab1257fcf5a1 h1:oaPzJakH41f6Oprj0ldlR03aqsCffZYYTFmCC2ad3kg=
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240617161612-ab1257fcf5a1/go.mod h1:Bw0cs7rgkYv6DG0BNNYQ56n59XxPWY3ZXoJPyKoT2Ro=
github.com/berachain/beacon-kit/mod/config v0.0.0-20240614154006-a5defa6198f5 h1:KqQcNurpXlZYZ3r/+WR3j50+KjMWu2iZ1DDk1MdbrWk=
github.com/berachain/beacon-kit/mod/config v0.0.0-20240614154006-a5defa6198f5/go.mod h1:rBTpMivjYpzaQk/tKsp1pqj3jev4YJ5PPfUHhcpowCc=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240614154006-a5defa6198f5 h1:tF5zeC+OzfoVuE86LcE2Z9xyJSkBiMjEhejBKTRw6jE=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240614154006-a5defa6198f5/go.mod h1:rreW3jRRBVZNjrqDRTBik3oSYrxyQ/gKmni/OaHnfxc=
github.com/berachain/beacon-kit/mod/da v0.0.0-20240614154006-a5defa6198f5 h1:rpEZNxSXhMh2d0Tlb1m6ioxxXeoPOkWir0algjJ7QKY=
Expand Down
129 changes: 129 additions & 0 deletions mod/cli/pkg/builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package builder

import (
"os"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
cmdlib "github.com/berachain/beacon-kit/mod/cli/pkg/commands"
"github.com/berachain/beacon-kit/mod/primitives"
"github.com/cosmos/cosmos-sdk/client"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// CLIBuilder is the builder for the commands.Root (root command).
type CLIBuilder[T servertypes.Application] struct {
depInjectCfg depinject.Config
name string
description string
// components is a list of component providers for depinject.
components []any
// supplies is a list of suppliers for depinject.
supplies []any
runHandler runHandler
// appCreator is a function that builds the Node, eventually called by the
// cosmos-sdk.
// TODO: CLI should not know about the AppCreator
appCreator servertypes.AppCreator[T]
// rootCmdSetup is a function that sets up the root command.
rootCmdSetup rootCmdSetup[T]
}

// New returns a new CLIBuilder with the given options.
func New[T servertypes.Application](opts ...Opt[T]) *CLIBuilder[T] {
cb := &CLIBuilder[T]{
supplies: []any{log.NewLogger(os.Stdout), viper.GetViper()},
}
for _, opt := range opts {
opt(cb)
}
return cb
}

// Build builds the CLI commands.
func (cb *CLIBuilder[T]) Build() (*cmdlib.Root, error) {
// allocate memory to hold the dependencies
var (
autoCliOpts autocli.AppOptions
mm *module.Manager
clientCtx client.Context
chainSpec primitives.ChainSpec
)
// build dependencies for the root command
if err := depinject.Inject(
depinject.Configs(
cb.depInjectCfg,
depinject.Supply(
cb.supplies...,
),
depinject.Provide(
cb.components...,
),
),
&autoCliOpts,
&mm,
&clientCtx,
&chainSpec,
); err != nil {
return nil, err
}

// pass in deps to build the root command
rootCmd := cmdlib.New(
cb.name,
cb.description,
defaultRunHandler(cb.runHandler),
clientCtx,
)

// enhance the root command with the autoCliOpts
if err := rootCmd.Enhance(autoCliOpts.EnhanceRootCommand); err != nil {
return nil, err
}

// apply default root command setup
cmdlib.DefaultRootCommandSetup(
rootCmd,
mm,
cb.appCreator,
chainSpec,
)

return rootCmd, nil
}

// defaultRunHandler returns a runHandler that uses the default configuration.
func defaultRunHandler(runHandler runHandler) func(cmd *cobra.Command) error {
return func(cmd *cobra.Command) error {
return runHandler(
cmd,
DefaultAppConfigTemplate(),
DefaultAppConfig(),
DefaultCometConfig(),
)
}
}
Loading

0 comments on commit f1367da

Please sign in to comment.