github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/blocks/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 blocksPkg
    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/output"
    18  	outputHelpers "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output/helpers"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  // EXISTING_CODE
    23  
    24  // RunBlocks handles the blocks command for the command line. Returns error only as per cobra.
    25  func RunBlocks(cmd *cobra.Command, args []string) error {
    26  	opts := blocksFinishParse(args)
    27  	rCtx := output.NewRenderContext()
    28  	// EXISTING_CODE
    29  	// EXISTING_CODE
    30  	outputHelpers.SetWriterForCommand("blocks", &opts.Globals)
    31  	return opts.BlocksInternal(rCtx)
    32  }
    33  
    34  // ServeBlocks handles the blocks command for the API. Returns an error.
    35  func ServeBlocks(w http.ResponseWriter, r *http.Request) error {
    36  	opts := blocksFinishParseApi(w, r)
    37  	rCtx := output.NewRenderContext()
    38  	// EXISTING_CODE
    39  	// EXISTING_CODE
    40  	outputHelpers.InitJsonWriterApi("blocks", w, &opts.Globals)
    41  	err := opts.BlocksInternal(rCtx)
    42  	outputHelpers.CloseJsonWriterIfNeededApi("blocks", err, &opts.Globals)
    43  	return err
    44  }
    45  
    46  // BlocksInternal handles the internal workings of the blocks command. Returns an error.
    47  func (opts *BlocksOptions) BlocksInternal(rCtx *output.RenderCtx) error {
    48  	var err error
    49  	if err = opts.validateBlocks(); err != nil {
    50  		return err
    51  	}
    52  
    53  	timer := logger.NewTimer()
    54  	msg := "chifra blocks"
    55  	// EXISTING_CODE
    56  	// EXISTING_CODE
    57  	if opts.Globals.Decache {
    58  		err = opts.HandleDecache(rCtx)
    59  	} else if opts.Count {
    60  		err = opts.HandleCount(rCtx)
    61  	} else if opts.Logs {
    62  		err = opts.HandleLogs(rCtx)
    63  	} else if opts.Withdrawals {
    64  		err = opts.HandleWithdrawals(rCtx)
    65  	} else if opts.Traces {
    66  		err = opts.HandleTraces(rCtx)
    67  	} else if opts.Uncles {
    68  		err = opts.HandleUncles(rCtx)
    69  	} else if opts.Uniq {
    70  		err = opts.HandleUniq(rCtx)
    71  	} else if opts.Hashes {
    72  		err = opts.HandleHashes(rCtx)
    73  	} else {
    74  		err = opts.HandleShow(rCtx)
    75  	}
    76  	timer.Report(msg)
    77  
    78  	return err
    79  }
    80  
    81  // GetBlocksOptions returns the options for this tool so other tools may use it.
    82  func GetBlocksOptions(args []string, g *globals.GlobalOptions) *BlocksOptions {
    83  	ret := blocksFinishParse(args)
    84  	if g != nil {
    85  		ret.Globals = *g
    86  	}
    87  	return ret
    88  }