github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/config/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 configPkg
    10  
    11  import (
    12  	// EXISTING_CODE
    13  	"encoding/json"
    14  	"io"
    15  	"net/http"
    16  	"net/url"
    17  
    18  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals"
    19  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/caps"
    20  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    21  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
    22  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    23  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    24  	// EXISTING_CODE
    25  )
    26  
    27  // ConfigOptions provides all command options for the chifra config command.
    28  type ConfigOptions struct {
    29  	Mode    string                `json:"mode,omitempty"`    // Either show or edit the configuration
    30  	Paths   bool                  `json:"paths,omitempty"`   // Show the configuration paths for the system
    31  	Globals globals.GlobalOptions `json:"globals,omitempty"` // The global options
    32  	Conn    *rpc.Connection       `json:"conn,omitempty"`    // The connection to the RPC server
    33  	BadFlag error                 `json:"badFlag,omitempty"` // An error flag if needed
    34  	// EXISTING_CODE
    35  	// EXISTING_CODE
    36  }
    37  
    38  var defaultConfigOptions = ConfigOptions{}
    39  
    40  // testLog is used only during testing to export the options for this test case.
    41  func (opts *ConfigOptions) testLog() {
    42  	logger.TestLog(len(opts.Mode) > 0, "Mode: ", opts.Mode)
    43  	logger.TestLog(opts.Paths, "Paths: ", opts.Paths)
    44  	opts.Conn.TestLog(opts.getCaches())
    45  	opts.Globals.TestLog()
    46  }
    47  
    48  // String implements the Stringer interface
    49  func (opts *ConfigOptions) String() string {
    50  	b, _ := json.MarshalIndent(opts, "", "  ")
    51  	return string(b)
    52  }
    53  
    54  // configFinishParseApi finishes the parsing for server invocations. Returns a new ConfigOptions.
    55  func configFinishParseApi(w http.ResponseWriter, r *http.Request) *ConfigOptions {
    56  	values := r.URL.Query()
    57  	if r.Header.Get("User-Agent") == "testRunner" {
    58  		values.Set("testRunner", "true")
    59  	}
    60  	return ConfigFinishParseInternal(w, values)
    61  }
    62  
    63  func ConfigFinishParseInternal(w io.Writer, values url.Values) *ConfigOptions {
    64  	copy := defaultConfigOptions
    65  	copy.Globals.Caps = getCaps()
    66  	opts := &copy
    67  	for key, value := range values {
    68  		switch key {
    69  		case "mode":
    70  			opts.Mode = value[0]
    71  		case "paths":
    72  			opts.Paths = true
    73  		default:
    74  			if !copy.Globals.Caps.HasKey(key) {
    75  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "config")
    76  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
    77  					opts.BadFlag = err
    78  				}
    79  			}
    80  		}
    81  	}
    82  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
    83  
    84  	// EXISTING_CODE
    85  	// EXISTING_CODE
    86  
    87  	return opts
    88  }
    89  
    90  // configFinishParse finishes the parsing for command line invocations. Returns a new ConfigOptions.
    91  func configFinishParse(args []string) *ConfigOptions {
    92  	// remove duplicates from args if any (not needed in api mode because the server does it).
    93  	dedup := map[string]int{}
    94  	if len(args) > 0 {
    95  		tmp := []string{}
    96  		for _, arg := range args {
    97  			if value := dedup[arg]; value == 0 {
    98  				tmp = append(tmp, arg)
    99  			}
   100  			dedup[arg]++
   101  		}
   102  		args = tmp
   103  	}
   104  
   105  	defFmt := "txt"
   106  	opts := GetOptions()
   107  	opts.Conn = opts.Globals.FinishParse(args, opts.getCaches())
   108  
   109  	// EXISTING_CODE
   110  	defFmt = "txt"
   111  	for _, arg := range args {
   112  		opts.Mode = arg
   113  	}
   114  	if len(opts.Mode) == 0 {
   115  		opts.Mode = "<empty>"
   116  	}
   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() *ConfigOptions {
   126  	// EXISTING_CODE
   127  	// EXISTING_CODE
   128  	return &defaultConfigOptions
   129  }
   130  
   131  func getCaps() caps.Capability {
   132  	var capabilities caps.Capability // capabilities for chifra config
   133  	capabilities = capabilities.Add(caps.Default)
   134  	// EXISTING_CODE
   135  	// EXISTING_CODE
   136  	return capabilities
   137  }
   138  
   139  func ResetOptions(testMode bool) {
   140  	// We want to keep writer between command file calls
   141  	w := GetOptions().Globals.Writer
   142  	opts := ConfigOptions{}
   143  	globals.SetDefaults(&opts.Globals)
   144  	opts.Globals.TestMode = testMode
   145  	opts.Globals.Writer = w
   146  	opts.Globals.Caps = getCaps()
   147  	defaultConfigOptions = opts
   148  }
   149  
   150  func (opts *ConfigOptions) getCaches() (caches map[walk.CacheType]bool) {
   151  	// EXISTING_CODE
   152  	// EXISTING_CODE
   153  	return
   154  }
   155  
   156  // EXISTING_CODE
   157  // EXISTING_CODE