Skip to content

Commit

Permalink
fixup! feat(cosmos)!: add required export-dir export cmd option
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman committed Aug 22, 2023
1 parent 5314a32 commit 15b00c0
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions golang/cosmos/daemon/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ func initRootCmd(sender Sender, rootCmd *cobra.Command, encodingConfig params.En
}
server.AddCommands(rootCmd, gaia.DefaultNodeHome, ac.newApp, ac.appExport, addModuleInitFlags)

hasVMController := sender != nil
for _, command := range rootCmd.Commands() {
if command.Name() != "export" {
continue
if command.Name() == "export" {
extendCosmosExportCommand(command, hasVMController)
break
}

extendCosmosExportCommand(command, sender != nil)
break
}

// add keybase, auxiliary RPC, query, and tx child commands
Expand Down Expand Up @@ -284,11 +283,17 @@ func (ac appCreator) newApp(
}

const (
FlagExportDir = "export-dir"
ExportedGenesisFileName = "genesis.json"
// FlagExportDir is the command-line flag for the "export" command specifying
// where the output of the export should be placed.
FlagExportDir = "export-dir"
// ExportedGenesisFileName is the file name used to save the genesis in the export-dir
ExportedGenesisFileName = "genesis.json"
ExportedSwingStoreDirectoryName = "swing-store"
)

// extendCosmosExportCommand monkey-patches the "export" command added by
// cosmos-sdk to add a required "export-dir" command-line flag, and create the
// genesis export in the specified directory.
func extendCosmosExportCommand(cmd *cobra.Command, hasVMController bool) {
cmd.Flags().String(FlagExportDir, "", "The directory where to create the genesis export")
err := cmd.MarkFlagRequired(FlagExportDir)
Expand Down Expand Up @@ -316,6 +321,7 @@ func extendCosmosExportCommand(cmd *cobra.Command, hasVMController bool) {
}
serverCtx.Viper.Set(gaia.FlagSwingStoreExportDir, swingStoreExportPath)

// This will fail is a genesis.json already exists in the export-dir
genesisFile, err := os.OpenFile(genesisPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
Expand All @@ -327,9 +333,10 @@ func extendCosmosExportCommand(cmd *cobra.Command, hasVMController bool) {
return originalRunE(cmd, args)
}

// Only do modify the command handler if we actually have a controller.
// Otherwise, there will be an exec of the VM program (OnExportHook), where
// this will handle all of the export logic.
// Only modify the command handler when we have a VM controller to handle
// the full export logic. Otherwise, appExport will just exec the VM program
// (OnExportHook), which will result in re-entering this flow with the VM
// controller set.
if hasVMController {
cmd.RunE = extendedRunE
}
Expand Down

0 comments on commit 15b00c0

Please sign in to comment.