github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/explore/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 explorePkg
    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/caps"
    21  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    22  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
    23  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    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  // ExploreOptions provides all command options for the chifra explore command.
    30  type ExploreOptions struct {
    31  	Terms   []string              `json:"terms,omitempty"`   // One or more address, name, block, or transaction identifier
    32  	NoOpen  bool                  `json:"noOpen,omitempty"`  // Return the URL without opening it
    33  	Local   bool                  `json:"local,omitempty"`   // Open the local TrueBlocks explorer
    34  	Google  bool                  `json:"google,omitempty"`  // Search google excluding popular blockchain explorers
    35  	Dalle   bool                  `json:"dalle,omitempty"`   // Open the address to the DalleDress explorer
    36  	Globals globals.GlobalOptions `json:"globals,omitempty"` // The global options
    37  	Conn    *rpc.Connection       `json:"conn,omitempty"`    // The connection to the RPC server
    38  	BadFlag error                 `json:"badFlag,omitempty"` // An error flag if needed
    39  	// EXISTING_CODE
    40  	Destinations []types.Destination
    41  	// EXISTING_CODE
    42  }
    43  
    44  var defaultExploreOptions = ExploreOptions{}
    45  
    46  // testLog is used only during testing to export the options for this test case.
    47  func (opts *ExploreOptions) testLog() {
    48  	logger.TestLog(len(opts.Terms) > 0, "Terms: ", opts.Terms)
    49  	logger.TestLog(opts.NoOpen, "NoOpen: ", opts.NoOpen)
    50  	logger.TestLog(opts.Local, "Local: ", opts.Local)
    51  	logger.TestLog(opts.Google, "Google: ", opts.Google)
    52  	logger.TestLog(opts.Dalle, "Dalle: ", opts.Dalle)
    53  	opts.Conn.TestLog(opts.getCaches())
    54  	opts.Globals.TestLog()
    55  }
    56  
    57  // String implements the Stringer interface
    58  func (opts *ExploreOptions) String() string {
    59  	b, _ := json.MarshalIndent(opts, "", "  ")
    60  	return string(b)
    61  }
    62  
    63  // exploreFinishParseApi finishes the parsing for server invocations. Returns a new ExploreOptions.
    64  func exploreFinishParseApi(w http.ResponseWriter, r *http.Request) *ExploreOptions {
    65  	values := r.URL.Query()
    66  	if r.Header.Get("User-Agent") == "testRunner" {
    67  		values.Set("testRunner", "true")
    68  	}
    69  	return ExploreFinishParseInternal(w, values)
    70  }
    71  
    72  func ExploreFinishParseInternal(w io.Writer, values url.Values) *ExploreOptions {
    73  	copy := defaultExploreOptions
    74  	copy.Globals.Caps = getCaps()
    75  	opts := &copy
    76  	for key, value := range values {
    77  		switch key {
    78  		case "terms":
    79  			for _, val := range value {
    80  				s := strings.Split(val, " ") // may contain space separated items
    81  				opts.Terms = append(opts.Terms, s...)
    82  			}
    83  		case "noOpen":
    84  			opts.NoOpen = true
    85  		case "local":
    86  			opts.Local = true
    87  		case "google":
    88  			opts.Google = true
    89  		case "dalle":
    90  			opts.Dalle = true
    91  		default:
    92  			if !copy.Globals.Caps.HasKey(key) {
    93  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "explore")
    94  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
    95  					opts.BadFlag = err
    96  				}
    97  			}
    98  		}
    99  	}
   100  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
   101  
   102  	// EXISTING_CODE
   103  	opts.Terms, _ = opts.Conn.GetEnsAddresses(opts.Terms)
   104  	// EXISTING_CODE
   105  
   106  	return opts
   107  }
   108  
   109  // exploreFinishParse finishes the parsing for command line invocations. Returns a new ExploreOptions.
   110  func exploreFinishParse(args []string) *ExploreOptions {
   111  	// remove duplicates from args if any (not needed in api mode because the server does it).
   112  	dedup := map[string]int{}
   113  	if len(args) > 0 {
   114  		tmp := []string{}
   115  		for _, arg := range args {
   116  			if value := dedup[arg]; value == 0 {
   117  				tmp = append(tmp, arg)
   118  			}
   119  			dedup[arg]++
   120  		}
   121  		args = tmp
   122  	}
   123  
   124  	defFmt := "txt"
   125  	opts := GetOptions()
   126  	opts.Conn = opts.Globals.FinishParse(args, opts.getCaches())
   127  
   128  	// EXISTING_CODE
   129  	opts.Terms = append(opts.Terms, args...)
   130  	opts.Terms, _ = opts.Conn.GetEnsAddresses(opts.Terms)
   131  	// EXISTING_CODE
   132  	if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
   133  		opts.Globals.Format = defFmt
   134  	}
   135  
   136  	return opts
   137  }
   138  
   139  func GetOptions() *ExploreOptions {
   140  	// EXISTING_CODE
   141  	// EXISTING_CODE
   142  	return &defaultExploreOptions
   143  }
   144  
   145  func getCaps() caps.Capability {
   146  	var capabilities caps.Capability // capabilities for chifra explore
   147  	capabilities = capabilities.Add(caps.Default)
   148  	// EXISTING_CODE
   149  	// EXISTING_CODE
   150  	return capabilities
   151  }
   152  
   153  func ResetOptions(testMode bool) {
   154  	// We want to keep writer between command file calls
   155  	w := GetOptions().Globals.Writer
   156  	opts := ExploreOptions{}
   157  	globals.SetDefaults(&opts.Globals)
   158  	opts.Globals.TestMode = testMode
   159  	opts.Globals.Writer = w
   160  	opts.Globals.Caps = getCaps()
   161  	defaultExploreOptions = opts
   162  }
   163  
   164  func (opts *ExploreOptions) getCaches() (caches map[walk.CacheType]bool) {
   165  	// EXISTING_CODE
   166  	// EXISTING_CODE
   167  	return
   168  }
   169  
   170  // EXISTING_CODE
   171  // EXISTING_CODE