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