github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/cmd/init.go (about)

     1  // Copyright 2016, 2024 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  /*
     5   * Parts of this file were auto generated. Edit only those parts of
     6   * the code inside of 'EXISTING_CODE' tags.
     7   */
     8  
     9  package cmd
    10  
    11  // EXISTING_CODE
    12  import (
    13  	"os"
    14  
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals"
    16  	initPkg "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/init"
    17  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/caps"
    18  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
    19  	outputHelpers "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output/helpers"
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  // EXISTING_CODE
    24  
    25  // initCmd represents the init command
    26  var initCmd = &cobra.Command{
    27  	Use:     usageInit,
    28  	Long:    longInit,
    29  	Version: versionText,
    30  	PreRun: outputHelpers.PreRunWithJsonWriter("init", func() *globals.GlobalOptions {
    31  		return &initPkg.GetOptions().Globals
    32  	}),
    33  	RunE: file.RunWithFileSupport("init", initPkg.RunInit, initPkg.ResetOptions),
    34  	PostRun: outputHelpers.PostRunWithJsonWriter(func() *globals.GlobalOptions {
    35  		return &initPkg.GetOptions().Globals
    36  	}),
    37  }
    38  
    39  const usageInit = `init [flags]`
    40  
    41  const longInit = `Purpose:
    42    Initialize the TrueBlocks system by downloading the Unchained Index from IPFS.`
    43  
    44  const notesInit = `
    45  Notes:
    46    - If run with no options, this tool will download or freshen only the Bloom filters.
    47    - The --first_block option will fall back to the start of the containing chunk.
    48    - You may re-run the tool as often as you wish. It will repair or freshen the index.`
    49  
    50  func init() {
    51  	var capabilities caps.Capability // capabilities for chifra init
    52  	capabilities = capabilities.Add(caps.Verbose)
    53  	capabilities = capabilities.Add(caps.Version)
    54  	capabilities = capabilities.Add(caps.Noop)
    55  	capabilities = capabilities.Add(caps.NoColor)
    56  	capabilities = capabilities.Add(caps.Chain)
    57  
    58  	initCmd.Flags().SortFlags = false
    59  
    60  	initCmd.Flags().BoolVarP(&initPkg.GetOptions().All, "all", "a", false, `in addition to Bloom filters, download full index chunks (recommended)`)
    61  	initCmd.Flags().StringVarP(&initPkg.GetOptions().Example, "example", "e", "", `create an example for the SDK with the given name`)
    62  	initCmd.Flags().BoolVarP(&initPkg.GetOptions().DryRun, "dry_run", "d", false, `display the results of the download without actually downloading`)
    63  	initCmd.Flags().StringVarP(&initPkg.GetOptions().Publisher, "publisher", "P", "", `the publisher of the index to download (hidden)`)
    64  	initCmd.Flags().Uint64VarP((*uint64)(&initPkg.GetOptions().FirstBlock), "first_block", "F", 0, `do not download any chunks earlier than this block`)
    65  	initCmd.Flags().Float64VarP(&initPkg.GetOptions().Sleep, "sleep", "s", 0.0, `seconds to sleep between downloads`)
    66  	if os.Getenv("TEST_MODE") != "true" {
    67  		_ = initCmd.Flags().MarkHidden("publisher")
    68  	}
    69  	globals.InitGlobals("init", initCmd, &initPkg.GetOptions().Globals, capabilities)
    70  
    71  	initCmd.SetUsageTemplate(UsageWithNotes(notesInit))
    72  	initCmd.SetOut(os.Stderr)
    73  
    74  	// EXISTING_CODE
    75  	// EXISTING_CODE
    76  
    77  	chifraCmd.AddCommand(initCmd)
    78  }