github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/list/output.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 listPkg
    10  
    11  // EXISTING_CODE
    12  import (
    13  	"net/http"
    14  
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals"
    16  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    17  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/monitor"
    18  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    19  	outputHelpers "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output/helpers"
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  // EXISTING_CODE
    24  
    25  // RunList handles the list command for the command line. Returns error only as per cobra.
    26  func RunList(cmd *cobra.Command, args []string) error {
    27  	opts := listFinishParse(args)
    28  	rCtx := output.NewRenderContext()
    29  	// EXISTING_CODE
    30  	// EXISTING_CODE
    31  	outputHelpers.SetWriterForCommand("list", &opts.Globals)
    32  	return opts.ListInternal(rCtx)
    33  }
    34  
    35  // ServeList handles the list command for the API. Returns an error.
    36  func ServeList(w http.ResponseWriter, r *http.Request) error {
    37  	opts := listFinishParseApi(w, r)
    38  	rCtx := output.NewRenderContext()
    39  	// EXISTING_CODE
    40  	// EXISTING_CODE
    41  	outputHelpers.InitJsonWriterApi("list", w, &opts.Globals)
    42  	err := opts.ListInternal(rCtx)
    43  	outputHelpers.CloseJsonWriterIfNeededApi("list", err, &opts.Globals)
    44  	return err
    45  }
    46  
    47  // ListInternal handles the internal workings of the list command. Returns an error.
    48  func (opts *ListOptions) ListInternal(rCtx *output.RenderCtx) error {
    49  	var err error
    50  	if err = opts.validateList(); err != nil {
    51  		return err
    52  	}
    53  
    54  	timer := logger.NewTimer()
    55  	msg := "chifra list"
    56  	// EXISTING_CODE
    57  	monitorArray := make([]monitor.Monitor, 0, len(opts.Addrs))
    58  	if canceled, err := opts.FreshenMonitorsForList(&monitorArray); err != nil || canceled {
    59  		return err
    60  	}
    61  	// EXISTING_CODE
    62  	if opts.Count {
    63  		err = opts.HandleCount(rCtx, monitorArray)
    64  	} else if opts.Bounds {
    65  		err = opts.HandleBounds(rCtx, monitorArray)
    66  	} else {
    67  		err = opts.HandleShow(rCtx, monitorArray)
    68  	}
    69  	timer.Report(msg)
    70  
    71  	return err
    72  }
    73  
    74  // GetListOptions returns the options for this tool so other tools may use it.
    75  func GetListOptions(args []string, g *globals.GlobalOptions) *ListOptions {
    76  	ret := listFinishParse(args)
    77  	if g != nil {
    78  		ret.Globals = *g
    79  	}
    80  	return ret
    81  }