github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/when/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 whenPkg
    10  
    11  import (
    12  	// EXISTING_CODE
    13  	"encoding/json"
    14  	"io"
    15  	"net/http"
    16  	"net/url"
    17  	"strings"
    18  
    19  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals"
    20  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    21  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/caps"
    22  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/identifiers"
    23  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    24  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
    25  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    26  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    27  	// EXISTING_CODE
    28  )
    29  
    30  // WhenOptions provides all command options for the chifra when command.
    31  type WhenOptions struct {
    32  	Blocks     []string                 `json:"blocks,omitempty"`     // One or more dates, block numbers, hashes, or special named blocks (see notes)
    33  	BlockIds   []identifiers.Identifier `json:"blockIds,omitempty"`   // Block identifiers
    34  	List       bool                     `json:"list,omitempty"`       // Export a list of the 'special' blocks
    35  	Timestamps bool                     `json:"timestamps,omitempty"` // Display or process timestamps
    36  	Count      bool                     `json:"count,omitempty"`      // With --timestamps only, returns the number of timestamps in the cache
    37  	Truncate   base.Blknum              `json:"truncate,omitempty"`   // With --timestamps only, truncates the timestamp file at this block
    38  	Repair     bool                     `json:"repair,omitempty"`     // With --timestamps only, repairs block(s) in the block range by re-querying from the chain
    39  	Check      bool                     `json:"check,omitempty"`      // With --timestamps only, checks the validity of the timestamp data
    40  	Update     bool                     `json:"update,omitempty"`     // With --timestamps only, bring the timestamp database forward to the latest block
    41  	Deep       bool                     `json:"deep,omitempty"`       // With --timestamps --check only, verifies timestamps from on chain (slow)
    42  	Globals    globals.GlobalOptions    `json:"globals,omitempty"`    // The global options
    43  	Conn       *rpc.Connection          `json:"conn,omitempty"`       // The connection to the RPC server
    44  	BadFlag    error                    `json:"badFlag,omitempty"`    // An error flag if needed
    45  	// EXISTING_CODE
    46  	// EXISTING_CODE
    47  }
    48  
    49  var defaultWhenOptions = WhenOptions{
    50  	Truncate: base.NOPOSN,
    51  }
    52  
    53  // testLog is used only during testing to export the options for this test case.
    54  func (opts *WhenOptions) testLog() {
    55  	logger.TestLog(len(opts.Blocks) > 0, "Blocks: ", opts.Blocks)
    56  	logger.TestLog(opts.List, "List: ", opts.List)
    57  	logger.TestLog(opts.Timestamps, "Timestamps: ", opts.Timestamps)
    58  	logger.TestLog(opts.Count, "Count: ", opts.Count)
    59  	logger.TestLog(opts.Truncate != base.NOPOSN, "Truncate: ", opts.Truncate)
    60  	logger.TestLog(opts.Repair, "Repair: ", opts.Repair)
    61  	logger.TestLog(opts.Check, "Check: ", opts.Check)
    62  	logger.TestLog(opts.Update, "Update: ", opts.Update)
    63  	logger.TestLog(opts.Deep, "Deep: ", opts.Deep)
    64  	opts.Conn.TestLog(opts.getCaches())
    65  	opts.Globals.TestLog()
    66  }
    67  
    68  // String implements the Stringer interface
    69  func (opts *WhenOptions) String() string {
    70  	b, _ := json.MarshalIndent(opts, "", "  ")
    71  	return string(b)
    72  }
    73  
    74  // whenFinishParseApi finishes the parsing for server invocations. Returns a new WhenOptions.
    75  func whenFinishParseApi(w http.ResponseWriter, r *http.Request) *WhenOptions {
    76  	values := r.URL.Query()
    77  	if r.Header.Get("User-Agent") == "testRunner" {
    78  		values.Set("testRunner", "true")
    79  	}
    80  	return WhenFinishParseInternal(w, values)
    81  }
    82  
    83  func WhenFinishParseInternal(w io.Writer, values url.Values) *WhenOptions {
    84  	copy := defaultWhenOptions
    85  	copy.Globals.Caps = getCaps()
    86  	opts := &copy
    87  	opts.Truncate = base.NOPOSN
    88  	for key, value := range values {
    89  		switch key {
    90  		case "blocks":
    91  			for _, val := range value {
    92  				s := strings.Split(val, " ") // may contain space separated items
    93  				opts.Blocks = append(opts.Blocks, s...)
    94  			}
    95  		case "list":
    96  			opts.List = true
    97  		case "timestamps":
    98  			opts.Timestamps = true
    99  		case "count":
   100  			opts.Count = true
   101  		case "truncate":
   102  			opts.Truncate = base.MustParseBlknum(value[0])
   103  		case "repair":
   104  			opts.Repair = true
   105  		case "check":
   106  			opts.Check = true
   107  		case "update":
   108  			opts.Update = true
   109  		case "deep":
   110  			opts.Deep = true
   111  		default:
   112  			if !copy.Globals.Caps.HasKey(key) {
   113  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "when")
   114  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
   115  					opts.BadFlag = err
   116  				}
   117  			}
   118  		}
   119  	}
   120  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
   121  
   122  	// EXISTING_CODE
   123  	// EXISTING_CODE
   124  
   125  	return opts
   126  }
   127  
   128  // whenFinishParse finishes the parsing for command line invocations. Returns a new WhenOptions.
   129  func whenFinishParse(args []string) *WhenOptions {
   130  	// remove duplicates from args if any (not needed in api mode because the server does it).
   131  	dedup := map[string]int{}
   132  	if len(args) > 0 {
   133  		tmp := []string{}
   134  		for _, arg := range args {
   135  			if value := dedup[arg]; value == 0 {
   136  				tmp = append(tmp, arg)
   137  			}
   138  			dedup[arg]++
   139  		}
   140  		args = tmp
   141  	}
   142  
   143  	defFmt := "txt"
   144  	opts := GetOptions()
   145  	opts.Conn = opts.Globals.FinishParse(args, opts.getCaches())
   146  
   147  	// EXISTING_CODE
   148  	opts.Blocks = args
   149  	if opts.Truncate == 0 {
   150  		opts.Truncate = base.NOPOSN
   151  	}
   152  	// EXISTING_CODE
   153  	if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
   154  		opts.Globals.Format = defFmt
   155  	}
   156  
   157  	return opts
   158  }
   159  
   160  func GetOptions() *WhenOptions {
   161  	// EXISTING_CODE
   162  	// EXISTING_CODE
   163  	return &defaultWhenOptions
   164  }
   165  
   166  func getCaps() caps.Capability {
   167  	var capabilities caps.Capability // capabilities for chifra when
   168  	capabilities = capabilities.Add(caps.Default)
   169  	capabilities = capabilities.Add(caps.Caching)
   170  	// EXISTING_CODE
   171  	// EXISTING_CODE
   172  	return capabilities
   173  }
   174  
   175  func ResetOptions(testMode bool) {
   176  	// We want to keep writer between command file calls
   177  	w := GetOptions().Globals.Writer
   178  	opts := WhenOptions{}
   179  	globals.SetDefaults(&opts.Globals)
   180  	opts.Globals.TestMode = testMode
   181  	opts.Globals.Writer = w
   182  	opts.Globals.Caps = getCaps()
   183  	opts.Truncate = base.NOPOSN
   184  	defaultWhenOptions = opts
   185  }
   186  
   187  func (opts *WhenOptions) getCaches() (caches map[walk.CacheType]bool) {
   188  	// EXISTING_CODE
   189  	caches = map[walk.CacheType]bool{
   190  		walk.Cache_Blocks: !opts.Timestamps,
   191  	}
   192  	// EXISTING_CODE
   193  	return
   194  }
   195  
   196  // EXISTING_CODE
   197  // EXISTING_CODE