github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/logs/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 logsPkg
    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/identifiers"
    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  // LogsOptions provides all command options for the chifra logs command.
    30  type LogsOptions struct {
    31  	Transactions   []string                 `json:"transactions,omitempty"`   // A space-separated list of one or more transaction identifiers
    32  	TransactionIds []identifiers.Identifier `json:"transactionIds,omitempty"` // Transaction identifiers
    33  	Emitter        []string                 `json:"emitter,omitempty"`        // Filter logs to show only those logs emitted by the given address(es)
    34  	Topic          []string                 `json:"topic,omitempty"`          // Filter logs to show only those with this topic(s)
    35  	Articulate     bool                     `json:"articulate,omitempty"`     // Articulate the retrieved data if ABIs can be found
    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  	// EXISTING_CODE
    41  }
    42  
    43  var defaultLogsOptions = LogsOptions{}
    44  
    45  // testLog is used only during testing to export the options for this test case.
    46  func (opts *LogsOptions) testLog() {
    47  	logger.TestLog(len(opts.Transactions) > 0, "Transactions: ", opts.Transactions)
    48  	logger.TestLog(len(opts.Emitter) > 0, "Emitter: ", opts.Emitter)
    49  	logger.TestLog(len(opts.Topic) > 0, "Topic: ", opts.Topic)
    50  	logger.TestLog(opts.Articulate, "Articulate: ", opts.Articulate)
    51  	opts.Conn.TestLog(opts.getCaches())
    52  	opts.Globals.TestLog()
    53  }
    54  
    55  // String implements the Stringer interface
    56  func (opts *LogsOptions) String() string {
    57  	b, _ := json.MarshalIndent(opts, "", "  ")
    58  	return string(b)
    59  }
    60  
    61  // logsFinishParseApi finishes the parsing for server invocations. Returns a new LogsOptions.
    62  func logsFinishParseApi(w http.ResponseWriter, r *http.Request) *LogsOptions {
    63  	values := r.URL.Query()
    64  	if r.Header.Get("User-Agent") == "testRunner" {
    65  		values.Set("testRunner", "true")
    66  	}
    67  	return LogsFinishParseInternal(w, values)
    68  }
    69  
    70  func LogsFinishParseInternal(w io.Writer, values url.Values) *LogsOptions {
    71  	copy := defaultLogsOptions
    72  	copy.Globals.Caps = getCaps()
    73  	opts := &copy
    74  	for key, value := range values {
    75  		switch key {
    76  		case "transactions":
    77  			for _, val := range value {
    78  				s := strings.Split(val, " ") // may contain space separated items
    79  				opts.Transactions = append(opts.Transactions, s...)
    80  			}
    81  		case "emitter":
    82  			for _, val := range value {
    83  				s := strings.Split(val, " ") // may contain space separated items
    84  				opts.Emitter = append(opts.Emitter, s...)
    85  			}
    86  		case "topic":
    87  			for _, val := range value {
    88  				s := strings.Split(val, " ") // may contain space separated items
    89  				opts.Topic = append(opts.Topic, s...)
    90  			}
    91  		case "articulate":
    92  			opts.Articulate = true
    93  		default:
    94  			if !copy.Globals.Caps.HasKey(key) {
    95  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "logs")
    96  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
    97  					opts.BadFlag = err
    98  				}
    99  			}
   100  		}
   101  	}
   102  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
   103  
   104  	// EXISTING_CODE
   105  	// EXISTING_CODE
   106  	opts.Emitter, _ = opts.Conn.GetEnsAddresses(opts.Emitter)
   107  
   108  	return opts
   109  }
   110  
   111  // logsFinishParse finishes the parsing for command line invocations. Returns a new LogsOptions.
   112  func logsFinishParse(args []string) *LogsOptions {
   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  
   130  	// EXISTING_CODE
   131  	opts.Transactions = args
   132  	// EXISTING_CODE
   133  	opts.Emitter, _ = opts.Conn.GetEnsAddresses(opts.Emitter)
   134  	if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
   135  		opts.Globals.Format = defFmt
   136  	}
   137  
   138  	return opts
   139  }
   140  
   141  func GetOptions() *LogsOptions {
   142  	// EXISTING_CODE
   143  	// EXISTING_CODE
   144  	return &defaultLogsOptions
   145  }
   146  
   147  func getCaps() caps.Capability {
   148  	var capabilities caps.Capability // capabilities for chifra logs
   149  	capabilities = capabilities.Add(caps.Default)
   150  	capabilities = capabilities.Add(caps.Caching)
   151  	capabilities = capabilities.Add(caps.Names)
   152  	// EXISTING_CODE
   153  	// EXISTING_CODE
   154  	return capabilities
   155  }
   156  
   157  func ResetOptions(testMode bool) {
   158  	// We want to keep writer between command file calls
   159  	w := GetOptions().Globals.Writer
   160  	opts := LogsOptions{}
   161  	globals.SetDefaults(&opts.Globals)
   162  	opts.Globals.TestMode = testMode
   163  	opts.Globals.Writer = w
   164  	opts.Globals.Caps = getCaps()
   165  	defaultLogsOptions = opts
   166  }
   167  
   168  func (opts *LogsOptions) getCaches() (caches map[walk.CacheType]bool) {
   169  	// EXISTING_CODE
   170  	caches = map[walk.CacheType]bool{
   171  		walk.Cache_Transactions: true,
   172  	}
   173  	// EXISTING_CODE
   174  	return
   175  }
   176  
   177  // EXISTING_CODE
   178  // EXISTING_CODE