github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/abis/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 abisPkg
    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/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  // AbisOptions provides all command options for the chifra abis command.
    30  type AbisOptions struct {
    31  	Addrs    []string              `json:"addrs,omitempty"`    // A list of one or more smart contracts whose ABIs to display
    32  	Known    bool                  `json:"known,omitempty"`    // Load common 'known' ABIs from cache
    33  	ProxyFor string                `json:"proxyFor,omitempty"` // Redirects the query to this implementation
    34  	List     bool                  `json:"list,omitempty"`     // A list of downloaded abi files
    35  	Count    bool                  `json:"count,omitempty"`    // Show the number of abis downloaded
    36  	Find     []string              `json:"find,omitempty"`     // Search for function or event declarations given a four- or 32-byte code(s)
    37  	Hint     []string              `json:"hint,omitempty"`     // For the --find option only, provide hints to speed up the search
    38  	Encode   string                `json:"encode,omitempty"`   // Generate the 32-byte encoding for a given cannonical function or event signature
    39  	Globals  globals.GlobalOptions `json:"globals,omitempty"`  // The global options
    40  	Conn     *rpc.Connection       `json:"conn,omitempty"`     // The connection to the RPC server
    41  	BadFlag  error                 `json:"badFlag,omitempty"`  // An error flag if needed
    42  	// EXISTING_CODE
    43  	// EXISTING_CODE
    44  }
    45  
    46  var defaultAbisOptions = AbisOptions{}
    47  
    48  // testLog is used only during testing to export the options for this test case.
    49  func (opts *AbisOptions) testLog() {
    50  	logger.TestLog(len(opts.Addrs) > 0, "Addrs: ", opts.Addrs)
    51  	logger.TestLog(opts.Known, "Known: ", opts.Known)
    52  	logger.TestLog(len(opts.ProxyFor) > 0, "ProxyFor: ", opts.ProxyFor)
    53  	logger.TestLog(opts.List, "List: ", opts.List)
    54  	logger.TestLog(opts.Count, "Count: ", opts.Count)
    55  	logger.TestLog(len(opts.Find) > 0, "Find: ", opts.Find)
    56  	logger.TestLog(len(opts.Hint) > 0, "Hint: ", opts.Hint)
    57  	logger.TestLog(len(opts.Encode) > 0, "Encode: ", opts.Encode)
    58  	opts.Conn.TestLog(opts.getCaches())
    59  	opts.Globals.TestLog()
    60  }
    61  
    62  // String implements the Stringer interface
    63  func (opts *AbisOptions) String() string {
    64  	b, _ := json.MarshalIndent(opts, "", "  ")
    65  	return string(b)
    66  }
    67  
    68  // abisFinishParseApi finishes the parsing for server invocations. Returns a new AbisOptions.
    69  func abisFinishParseApi(w http.ResponseWriter, r *http.Request) *AbisOptions {
    70  	values := r.URL.Query()
    71  	if r.Header.Get("User-Agent") == "testRunner" {
    72  		values.Set("testRunner", "true")
    73  	}
    74  	return AbisFinishParseInternal(w, values)
    75  }
    76  
    77  func AbisFinishParseInternal(w io.Writer, values url.Values) *AbisOptions {
    78  	copy := defaultAbisOptions
    79  	copy.Globals.Caps = getCaps()
    80  	opts := &copy
    81  	for key, value := range values {
    82  		switch key {
    83  		case "addrs":
    84  			for _, val := range value {
    85  				s := strings.Split(val, " ") // may contain space separated items
    86  				opts.Addrs = append(opts.Addrs, s...)
    87  			}
    88  		case "known":
    89  			opts.Known = true
    90  		case "proxyFor":
    91  			opts.ProxyFor = value[0]
    92  		case "list":
    93  			opts.List = true
    94  		case "count":
    95  			opts.Count = true
    96  		case "find":
    97  			for _, val := range value {
    98  				s := strings.Split(val, " ") // may contain space separated items
    99  				opts.Find = append(opts.Find, s...)
   100  			}
   101  		case "hint":
   102  			for _, val := range value {
   103  				s := strings.Split(val, " ") // may contain space separated items
   104  				opts.Hint = append(opts.Hint, s...)
   105  			}
   106  		case "encode":
   107  			opts.Encode = value[0]
   108  		default:
   109  			if !copy.Globals.Caps.HasKey(key) {
   110  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "abis")
   111  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
   112  					opts.BadFlag = err
   113  				}
   114  			}
   115  		}
   116  	}
   117  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
   118  
   119  	// EXISTING_CODE
   120  	// EXISTING_CODE
   121  	opts.Addrs, _ = opts.Conn.GetEnsAddresses(opts.Addrs)
   122  	opts.ProxyFor, _ = opts.Conn.GetEnsAddress(opts.ProxyFor)
   123  
   124  	return opts
   125  }
   126  
   127  // abisFinishParse finishes the parsing for command line invocations. Returns a new AbisOptions.
   128  func abisFinishParse(args []string) *AbisOptions {
   129  	// remove duplicates from args if any (not needed in api mode because the server does it).
   130  	dedup := map[string]int{}
   131  	if len(args) > 0 {
   132  		tmp := []string{}
   133  		for _, arg := range args {
   134  			if value := dedup[arg]; value == 0 {
   135  				tmp = append(tmp, arg)
   136  			}
   137  			dedup[arg]++
   138  		}
   139  		args = tmp
   140  	}
   141  
   142  	defFmt := "txt"
   143  	opts := GetOptions()
   144  	opts.Conn = opts.Globals.FinishParse(args, opts.getCaches())
   145  
   146  	// EXISTING_CODE
   147  	for _, arg := range args {
   148  		if base.IsValidAddress(arg) {
   149  			opts.Addrs = append(opts.Addrs, arg)
   150  		}
   151  	}
   152  	// EXISTING_CODE
   153  	opts.Addrs, _ = opts.Conn.GetEnsAddresses(opts.Addrs)
   154  	opts.ProxyFor, _ = opts.Conn.GetEnsAddress(opts.ProxyFor)
   155  	if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
   156  		opts.Globals.Format = defFmt
   157  	}
   158  
   159  	return opts
   160  }
   161  
   162  func GetOptions() *AbisOptions {
   163  	// EXISTING_CODE
   164  	// EXISTING_CODE
   165  	return &defaultAbisOptions
   166  }
   167  
   168  func getCaps() caps.Capability {
   169  	var capabilities caps.Capability // capabilities for chifra abis
   170  	capabilities = capabilities.Add(caps.Default)
   171  	capabilities = capabilities.Add(caps.Caching)
   172  	capabilities = capabilities.Add(caps.Names)
   173  	// EXISTING_CODE
   174  	// EXISTING_CODE
   175  	return capabilities
   176  }
   177  
   178  func ResetOptions(testMode bool) {
   179  	// We want to keep writer between command file calls
   180  	w := GetOptions().Globals.Writer
   181  	opts := AbisOptions{}
   182  	globals.SetDefaults(&opts.Globals)
   183  	opts.Globals.TestMode = testMode
   184  	opts.Globals.Writer = w
   185  	opts.Globals.Caps = getCaps()
   186  	defaultAbisOptions = opts
   187  }
   188  
   189  func (opts *AbisOptions) getCaches() (caches map[walk.CacheType]bool) {
   190  	// EXISTING_CODE
   191  	// EXISTING_CODE
   192  	return
   193  }
   194  
   195  // EXISTING_CODE
   196  // EXISTING_CODE