github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/daemon/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 daemonPkg
    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  // DaemonOptions provides all command options for the chifra daemon command.
    28  type DaemonOptions struct {
    29  	Url     string                `json:"url,omitempty"`     // Specify the API server's url and optionally its port
    30  	Silent  bool                  `json:"silent,omitempty"`  // Disable logging (for use in SDK for example)
    31  	Port    string                `json:"port,omitempty"`    // Deprecated, use --url instead
    32  	Grpc    bool                  `json:"grpc,omitempty"`    // Deprecated, there is no replacement
    33  	Api     string                `json:"api,omitempty"`     // Deprecated, there is no replacement
    34  	Scrape  string                `json:"scrape,omitempty"`  // Deprecated, use chifra scrape instead
    35  	Monitor bool                  `json:"monitor,omitempty"` // Deprecated, use chifra monitors --watch instead
    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 defaultDaemonOptions = DaemonOptions{
    44  	Url:  "localhost:8080",
    45  	Port: ":8080",
    46  	Api:  "on",
    47  }
    48  
    49  // testLog is used only during testing to export the options for this test case.
    50  func (opts *DaemonOptions) testLog() {
    51  	logger.TestLog(len(opts.Url) > 0 && opts.Url != "localhost:8080", "Url: ", opts.Url)
    52  	logger.TestLog(opts.Silent, "Silent: ", opts.Silent)
    53  	opts.Conn.TestLog(opts.getCaches())
    54  	opts.Globals.TestLog()
    55  }
    56  
    57  // String implements the Stringer interface
    58  func (opts *DaemonOptions) String() string {
    59  	b, _ := json.MarshalIndent(opts, "", "  ")
    60  	return string(b)
    61  }
    62  
    63  // daemonFinishParseApi finishes the parsing for server invocations. Returns a new DaemonOptions.
    64  func daemonFinishParseApi(w http.ResponseWriter, r *http.Request) *DaemonOptions {
    65  	values := r.URL.Query()
    66  	if r.Header.Get("User-Agent") == "testRunner" {
    67  		values.Set("testRunner", "true")
    68  	}
    69  	return DaemonFinishParseInternal(w, values)
    70  }
    71  
    72  func DaemonFinishParseInternal(w io.Writer, values url.Values) *DaemonOptions {
    73  	copy := defaultDaemonOptions
    74  	copy.Globals.Caps = getCaps()
    75  	opts := &copy
    76  	opts.Url = "localhost:8080"
    77  	opts.Port = ":8080"
    78  	opts.Api = "on"
    79  	for key, value := range values {
    80  		switch key {
    81  		case "url":
    82  			opts.Url = value[0]
    83  		case "silent":
    84  			opts.Silent = true
    85  		case "port":
    86  			opts.Port = value[0]
    87  		case "grpc":
    88  			opts.Grpc = true
    89  		case "api":
    90  			opts.Api = value[0]
    91  		case "scrape":
    92  			opts.Scrape = value[0]
    93  		case "monitor":
    94  			opts.Monitor = true
    95  		default:
    96  			if !copy.Globals.Caps.HasKey(key) {
    97  				err := validate.Usage("Invalid key ({0}) in {1} route.", key, "daemon")
    98  				if opts.BadFlag == nil || opts.BadFlag.Error() > err.Error() {
    99  					opts.BadFlag = err
   100  				}
   101  			}
   102  		}
   103  	}
   104  	opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())
   105  
   106  	// Deprecated...
   107  	if opts.Port != ":8080" && opts.Url == "localhost:8080" {
   108  		logger.Warn("The --port flag is deprecated. Please use --url instead.")
   109  		opts.Url = opts.Port
   110  		opts.Port = ""
   111  	}
   112  
   113  	// Deprecated...
   114  	if opts.Scrape != "" {
   115  		logger.Warn("The --scrape flag is deprecated. Please use chifra scrape instead.")
   116  		opts.Scrape = ""
   117  	}
   118  
   119  	// Deprecated...
   120  	if opts.Monitor {
   121  		logger.Warn("The --monitor flag is deprecated. Please use chifra monitors --watch instead.")
   122  		opts.Monitor = false
   123  	}
   124  
   125  	// EXISTING_CODE
   126  	// EXISTING_CODE
   127  
   128  	return opts
   129  }
   130  
   131  // daemonFinishParse finishes the parsing for command line invocations. Returns a new DaemonOptions.
   132  func daemonFinishParse(args []string) *DaemonOptions {
   133  	// remove duplicates from args if any (not needed in api mode because the server does it).
   134  	dedup := map[string]int{}
   135  	if len(args) > 0 {
   136  		tmp := []string{}
   137  		for _, arg := range args {
   138  			if value := dedup[arg]; value == 0 {
   139  				tmp = append(tmp, arg)
   140  			}
   141  			dedup[arg]++
   142  		}
   143  		args = tmp
   144  	}
   145  
   146  	defFmt := "txt"
   147  	opts := GetOptions()
   148  	opts.Conn = opts.Globals.FinishParse(args, opts.getCaches())
   149  
   150  	// Deprecated...
   151  	if opts.Port != ":8080" && opts.Url == "localhost:8080" {
   152  		logger.Warn("The --port flag is deprecated. Please use --url instead.")
   153  		opts.Url = opts.Port
   154  		opts.Port = ""
   155  	}
   156  
   157  	// Deprecated...
   158  	if opts.Scrape != "" {
   159  		logger.Warn("The --scrape flag is deprecated. Please use chifra scrape instead.")
   160  		opts.Scrape = ""
   161  	}
   162  
   163  	// Deprecated...
   164  	if opts.Monitor {
   165  		logger.Warn("The --monitor flag is deprecated. Please use chifra monitors --watch instead.")
   166  		opts.Monitor = false
   167  	}
   168  
   169  	// EXISTING_CODE
   170  	// EXISTING_CODE
   171  	if len(opts.Globals.Format) == 0 || opts.Globals.Format == "none" {
   172  		opts.Globals.Format = defFmt
   173  	}
   174  
   175  	return opts
   176  }
   177  
   178  func GetOptions() *DaemonOptions {
   179  	// EXISTING_CODE
   180  	// EXISTING_CODE
   181  	return &defaultDaemonOptions
   182  }
   183  
   184  func getCaps() caps.Capability {
   185  	var capabilities caps.Capability // capabilities for chifra daemon
   186  	capabilities = capabilities.Add(caps.Verbose)
   187  	capabilities = capabilities.Add(caps.Version)
   188  	capabilities = capabilities.Add(caps.Noop)
   189  	capabilities = capabilities.Add(caps.NoColor)
   190  	// EXISTING_CODE
   191  	// EXISTING_CODE
   192  	return capabilities
   193  }
   194  
   195  func ResetOptions(testMode bool) {
   196  	// We want to keep writer between command file calls
   197  	w := GetOptions().Globals.Writer
   198  	opts := DaemonOptions{}
   199  	globals.SetDefaults(&opts.Globals)
   200  	opts.Globals.TestMode = testMode
   201  	opts.Globals.Writer = w
   202  	opts.Globals.Caps = getCaps()
   203  	opts.Url = "localhost:8080"
   204  	opts.Port = ":8080"
   205  	opts.Api = "on"
   206  	defaultDaemonOptions = opts
   207  }
   208  
   209  func (opts *DaemonOptions) getCaches() (caches map[walk.CacheType]bool) {
   210  	// EXISTING_CODE
   211  	// EXISTING_CODE
   212  	return
   213  }
   214  
   215  // EXISTING_CODE
   216  // EXISTING_CODE