github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/names/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 namesPkg
    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/crud"
    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  // NamesOptions provides all command options for the chifra names command.
    31  type NamesOptions struct {
    32  	Terms     []string              `json:"terms,omitempty"`     // A space separated list of one or more search terms
    33  	Expand    bool                  `json:"expand,omitempty"`    // Expand search to include all fields (search name, address, and symbol otherwise)
    34  	MatchCase bool                  `json:"matchCase,omitempty"` // Do case-sensitive search
    35  	All       bool                  `json:"all,omitempty"`       // Include all (including custom) names in the search
    36  	Custom    bool                  `json:"custom,omitempty"`    // Include only custom named accounts in the search
    37  	Prefund   bool                  `json:"prefund,omitempty"`   // Include prefund accounts in the search
    38  	Addr      bool                  `json:"addr,omitempty"`      // Display only addresses in the results (useful for scripting, assumes --no_header)
    39  	Tags      bool                  `json:"tags,omitempty"`      // Export the list of tags and subtags only
    40  	Clean     bool                  `json:"clean,omitempty"`     // Clean the data (addrs to lower case, sort by addr)
    41  	Regular   bool                  `json:"regular,omitempty"`   // Only available with --clean, cleans regular names database
    42  	DryRun    bool                  `json:"dryRun,omitempty"`    // Only available with --clean or --autoname, outputs changes to stdout instead of updating databases
    43  	Autoname  string                `json:"autoname,omitempty"`  // An address assumed to be a token, added automatically to names database if true
    44  	Create    bool                  `json:"create,omitempty"`    // Create a new name record
    45  	Update    bool                  `json:"update,omitempty"`    // Edit an existing name
    46  	Delete    bool                  `json:"delete,omitempty"`    // Delete a name, but do not remove it
    47  	Undelete  bool                  `json:"undelete,omitempty"`  // Undelete a previously deleted name
    48  	Remove    bool                  `json:"remove,omitempty"`    // Remove a previously deleted name
    49  	Globals   globals.GlobalOptions `json:"globals,omitempty"`   // The global options
    50  	Conn      *rpc.Connection       `json:"conn,omitempty"`      // The connection to the RPC server
    51  	BadFlag   error                 `json:"badFlag,omitempty"`   // An error flag if needed
    52  	// EXISTING_CODE
    53  	crudData     *crud.NameCrud
    54  	AutonameAddr base.Address `json:"-"`
    55  	OrigTerms    []string     `json:"-"`
    56  	// EXISTING_CODE
    57  }
    58  
    59  var defaultNamesOptions = NamesOptions{}
    60  
    61  // testLog is used only during testing to export the options for this test case.
    62  func (opts *NamesOptions) testLog() {
    63  	logger.TestLog(len(opts.Terms) > 0, "Terms: ", opts.Terms)
    64  	logger.TestLog(opts.Expand, "Expand: ", opts.Expand)
    65  	logger.TestLog(opts.MatchCase, "MatchCase: ", opts.MatchCase)
    66  	logger.TestLog(opts.All, "All: ", opts.All)
    67  	logger.TestLog(opts.Custom, "Custom: ", opts.Custom)
    68  	logger.TestLog(opts.Prefund, "Prefund: ", opts.Prefund)
    69  	logger.TestLog(opts.Addr, "Addr: ", opts.Addr)
    70  	logger.TestLog(opts.Tags, "Tags: ", opts.Tags)
    71  	logger.TestLog(opts.Clean, "Clean: ", opts.Clean)
    72  	logger.TestLog(opts.Regular, "Regular: ", opts.Regular)
    73  	logger.TestLog(opts.DryRun, "DryRun: ", opts.DryRun)
    74  	logger.TestLog(len(opts.Autoname) > 0, "Autoname: ", opts.Autoname)
    75  	logger.TestLog(opts.Create, "Create: ", opts.Create)
    76  	logger.TestLog(opts.Update, "Update: ", opts.Update)
    77  	logger.TestLog(opts.Delete, "Delete: ", opts.Delete)
    78  	logger.TestLog(opts.Undelete, "Undelete: ", opts.Undelete)
    79  	logger.TestLog(opts.Remove, "Remove: ", opts.Remove)
    80  	opts.Conn.TestLog(opts.getCaches())
    81  	opts.Globals.TestLog()
    82  }
    83  
    84  // String implements the Stringer interface
    85  func (opts *NamesOptions) String() string {
    86  	b, _ := json.MarshalIndent(opts, "", "  ")
    87  	return string(b)
    88  }
    89  
    90  // namesFinishParseApi finishes the parsing for server invocations. Returns a new NamesOptions.
    91  func namesFinishParseApi(w http.ResponseWriter, r *http.Request) *NamesOptions {
    92  	values := r.URL.Query()
    93  	if r.Header.Get("User-Agent") == "testRunner" {
    94  		values.Set("testRunner", "true")
    95  	}
    96  	return NamesFinishParseInternal(w, values)
    97  }
    98  
    99  func NamesFinishParseInternal(w io.Writer, values url.Values) *NamesOptions {
   100  	copy := defaultNamesOptions
   101  	copy.Globals.Caps = getCaps()
   102  	opts := &copy
   103  	for key, value := range values {
   104  		switch key {
   105  		case "terms":
   106  			for _, val := range value {
   107  				s := strings.Split(val, " ") // may contain space separated items
   108  				opts.Terms = append(opts.Terms, s...)
   109  			}
   110  		case "expand":
   111  			opts.Expand = true
   112  		case "matchCase":
   113  			opts.MatchCase = true
   114  		case "all":
   115  			opts.All = true
   116  		case "custom":
   117  			opts.Custom = true
   118  		case "prefund":
   119  			opts.Prefund = true
   120  		case "addr":
   121  			opts.Addr = true
   122  		case "tags":
   123  			opts.Tags = true
   124  		case "clean":
   125  			opts.Clean = true
   126  		case "regular":
   127  			opts.Regular = true
   128  		case "dryRun":
   129  			opts.DryRun = true
   130  		case "autoname":
   131  			opts.Autoname = value[0]
   132  		case "create":
   133  			opts.Create = true
   134  		case "update":
   135  			opts.Update = true
   136  		case "delete":
   137  			opts.Delete = true
   138  		case "undelete":
   139  			opts.Undelete = true
   140  		case "remove":
   141  			opts.Remove = true
   142  		default:
   143  			if !copy.Globals.Caps.HasKey(key) {
   144  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "names")
   145  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
   146  					opts.BadFlag = err
   147  				}
   148  			}
   149  		}
   150  	}
   151  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
   152  	opts.Autoname, _ = opts.Conn.GetEnsAddress(opts.Autoname)
   153  	opts.AutonameAddr = base.HexToAddress(opts.Autoname)
   154  
   155  	// EXISTING_CODE
   156  	opts.OrigTerms = opts.Terms
   157  	opts.Terms, _ = opts.Conn.GetEnsAddresses(opts.Terms)
   158  	// EXISTING_CODE
   159  
   160  	return opts
   161  }
   162  
   163  // namesFinishParse finishes the parsing for command line invocations. Returns a new NamesOptions.
   164  func namesFinishParse(args []string) *NamesOptions {
   165  	// remove duplicates from args if any (not needed in api mode because the server does it).
   166  	dedup := map[string]int{}
   167  	if len(args) > 0 {
   168  		tmp := []string{}
   169  		for _, arg := range args {
   170  			if value := dedup[arg]; value == 0 {
   171  				tmp = append(tmp, arg)
   172  			}
   173  			dedup[arg]++
   174  		}
   175  		args = tmp
   176  	}
   177  
   178  	defFmt := "txt"
   179  	opts := GetOptions()
   180  	opts.Conn = opts.Globals.FinishParse(args, opts.getCaches())
   181  	opts.Autoname, _ = opts.Conn.GetEnsAddress(opts.Autoname)
   182  	opts.AutonameAddr = base.HexToAddress(opts.Autoname)
   183  
   184  	// EXISTING_CODE
   185  	opts.Terms = append(opts.Terms, args...)
   186  	opts.OrigTerms = opts.Terms
   187  	opts.Terms, _ = opts.Conn.GetEnsAddresses(opts.Terms)
   188  	// EXISTING_CODE
   189  	if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
   190  		opts.Globals.Format = defFmt
   191  	}
   192  
   193  	return opts
   194  }
   195  
   196  func GetOptions() *NamesOptions {
   197  	// EXISTING_CODE
   198  	// EXISTING_CODE
   199  	return &defaultNamesOptions
   200  }
   201  
   202  func getCaps() caps.Capability {
   203  	var capabilities caps.Capability // capabilities for chifra names
   204  	capabilities = capabilities.Add(caps.Default)
   205  	// EXISTING_CODE
   206  	// EXISTING_CODE
   207  	return capabilities
   208  }
   209  
   210  func ResetOptions(testMode bool) {
   211  	// We want to keep writer between command file calls
   212  	w := GetOptions().Globals.Writer
   213  	opts := NamesOptions{}
   214  	globals.SetDefaults(&opts.Globals)
   215  	opts.Globals.TestMode = testMode
   216  	opts.Globals.Writer = w
   217  	opts.Globals.Caps = getCaps()
   218  	defaultNamesOptions = opts
   219  }
   220  
   221  func (opts *NamesOptions) getCaches() (caches map[walk.CacheType]bool) {
   222  	// EXISTING_CODE
   223  	// EXISTING_CODE
   224  	return
   225  }
   226  
   227  // EXISTING_CODE
   228  // EXISTING_CODE