github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/init/options.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 initPkg
    10  
    11  import (
    12  	// EXISTING_CODE
    13  	"encoding/json"
    14  	"io"
    15  	"net/http"
    16  	"net/url"
    17  
    18  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals"
    19  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    20  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/caps"
    21  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    22  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    23  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
    24  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    25  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    26  	// EXISTING_CODE
    27  )
    28  
    29  // InitOptions provides all command options for the chifra init command.
    30  type InitOptions struct {
    31  	All        bool                  `json:"all,omitempty"`        // In addition to Bloom filters, download full index chunks (recommended)
    32  	Example    string                `json:"example,omitempty"`    // Create an example for the SDK with the given name
    33  	DryRun     bool                  `json:"dryRun,omitempty"`     // Display the results of the download without actually downloading
    34  	Publisher  string                `json:"publisher,omitempty"`  // The publisher of the index to download
    35  	FirstBlock base.Blknum           `json:"firstBlock,omitempty"` // Do not download any chunks earlier than this block
    36  	Sleep      float64               `json:"sleep,omitempty"`      // Seconds to sleep between downloads
    37  	Globals    globals.GlobalOptions `json:"globals,omitempty"`    // The global options
    38  	Conn       *rpc.Connection       `json:"conn,omitempty"`       // The connection to the RPC server
    39  	BadFlag    error                 `json:"badFlag,omitempty"`    // An error flag if needed
    40  	// EXISTING_CODE
    41  	PublisherAddr base.Address `json:"-"`
    42  	// EXISTING_CODE
    43  }
    44  
    45  var defaultInitOptions = InitOptions{}
    46  
    47  // testLog is used only during testing to export the options for this test case.
    48  func (opts *InitOptions) testLog() {
    49  	logger.TestLog(opts.All, "All: ", opts.All)
    50  	logger.TestLog(len(opts.Example) > 0, "Example: ", opts.Example)
    51  	logger.TestLog(opts.DryRun, "DryRun: ", opts.DryRun)
    52  	logger.TestLog(len(opts.Publisher) > 0, "Publisher: ", opts.Publisher)
    53  	logger.TestLog(opts.FirstBlock != 0, "FirstBlock: ", opts.FirstBlock)
    54  	logger.TestLog(opts.Sleep != float64(0.0), "Sleep: ", opts.Sleep)
    55  	opts.Conn.TestLog(opts.getCaches())
    56  	opts.Globals.TestLog()
    57  }
    58  
    59  // String implements the Stringer interface
    60  func (opts *InitOptions) String() string {
    61  	b, _ := json.MarshalIndent(opts, "", "  ")
    62  	return string(b)
    63  }
    64  
    65  // initFinishParseApi finishes the parsing for server invocations. Returns a new InitOptions.
    66  func initFinishParseApi(w http.ResponseWriter, r *http.Request) *InitOptions {
    67  	values := r.URL.Query()
    68  	if r.Header.Get("User-Agent") == "testRunner" {
    69  		values.Set("testRunner", "true")
    70  	}
    71  	return InitFinishParseInternal(w, values)
    72  }
    73  
    74  func InitFinishParseInternal(w io.Writer, values url.Values) *InitOptions {
    75  	copy := defaultInitOptions
    76  	copy.Globals.Caps = getCaps()
    77  	opts := &copy
    78  	for key, value := range values {
    79  		switch key {
    80  		case "all":
    81  			opts.All = true
    82  		case "example":
    83  			opts.Example = value[0]
    84  		case "dryRun":
    85  			opts.DryRun = true
    86  		case "publisher":
    87  			opts.Publisher = value[0]
    88  		case "firstBlock":
    89  			opts.FirstBlock = base.MustParseBlknum(value[0])
    90  		case "sleep":
    91  			opts.Sleep = base.MustParseFloat64(value[0])
    92  		default:
    93  			if !copy.Globals.Caps.HasKey(key) {
    94  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "init")
    95  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
    96  					opts.BadFlag = err
    97  				}
    98  			}
    99  		}
   100  	}
   101  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
   102  	opts.Publisher, _ = opts.Conn.GetEnsAddress(config.GetPublisher(opts.Publisher))
   103  	opts.PublisherAddr = base.HexToAddress(opts.Publisher)
   104  
   105  	// EXISTING_CODE
   106  	// EXISTING_CODE
   107  
   108  	return opts
   109  }
   110  
   111  // initFinishParse finishes the parsing for command line invocations. Returns a new InitOptions.
   112  func initFinishParse(args []string) *InitOptions {
   113  	// remove duplicates from args if any (not needed in api mode because the server does it).
   114  	dedup := map[string]int{}
   115  	if len(args) > 0 {
   116  		tmp := []string{}
   117  		for _, arg := range args {
   118  			if value := dedup[arg]; value == 0 {
   119  				tmp = append(tmp, arg)
   120  			}
   121  			dedup[arg]++
   122  		}
   123  		args = tmp
   124  	}
   125  
   126  	defFmt := "txt"
   127  	opts := GetOptions()
   128  	opts.Conn = opts.Globals.FinishParse(args, opts.getCaches())
   129  	opts.Publisher, _ = opts.Conn.GetEnsAddress(config.GetPublisher(opts.Publisher))
   130  	opts.PublisherAddr = base.HexToAddress(opts.Publisher)
   131  
   132  	// EXISTING_CODE
   133  	if len(args) > 0 {
   134  		opts.BadFlag = validate.Usage("Invalid argument ({0}).", args[0])
   135  	}
   136  	// EXISTING_CODE
   137  	if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
   138  		opts.Globals.Format = defFmt
   139  	}
   140  
   141  	return opts
   142  }
   143  
   144  func GetOptions() *InitOptions {
   145  	// EXISTING_CODE
   146  	// EXISTING_CODE
   147  	return &defaultInitOptions
   148  }
   149  
   150  func getCaps() caps.Capability {
   151  	var capabilities caps.Capability // capabilities for chifra init
   152  	capabilities = capabilities.Add(caps.Verbose)
   153  	capabilities = capabilities.Add(caps.Version)
   154  	capabilities = capabilities.Add(caps.Noop)
   155  	capabilities = capabilities.Add(caps.NoColor)
   156  	capabilities = capabilities.Add(caps.Chain)
   157  	// EXISTING_CODE
   158  	// EXISTING_CODE
   159  	return capabilities
   160  }
   161  
   162  func ResetOptions(testMode bool) {
   163  	// We want to keep writer between command file calls
   164  	w := GetOptions().Globals.Writer
   165  	opts := InitOptions{}
   166  	globals.SetDefaults(&opts.Globals)
   167  	opts.Globals.TestMode = testMode
   168  	opts.Globals.Writer = w
   169  	opts.Globals.Caps = getCaps()
   170  	defaultInitOptions = opts
   171  }
   172  
   173  func (opts *InitOptions) getCaches() (caches map[walk.CacheType]bool) {
   174  	// EXISTING_CODE
   175  	// EXISTING_CODE
   176  	return
   177  }
   178  
   179  // EXISTING_CODE
   180  // EXISTING_CODE