github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/export/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 exportPkg
    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  // RunExport handles the export command for the command line. Returns error only as per cobra.
    26  func RunExport(cmd *cobra.Command, args []string) error {
    27  	opts := exportFinishParse(args)
    28  	rCtx := output.NewRenderContext()
    29  	// EXISTING_CODE
    30  	// EXISTING_CODE
    31  	outputHelpers.SetWriterForCommand("export", &opts.Globals)
    32  	return opts.ExportInternal(rCtx)
    33  }
    34  
    35  // ServeExport handles the export command for the API. Returns an error.
    36  func ServeExport(w http.ResponseWriter, r *http.Request) error {
    37  	opts := exportFinishParseApi(w, r)
    38  	rCtx := output.NewRenderContext()
    39  	// EXISTING_CODE
    40  	// EXISTING_CODE
    41  	outputHelpers.InitJsonWriterApi("export", w, &opts.Globals)
    42  	err := opts.ExportInternal(rCtx)
    43  	outputHelpers.CloseJsonWriterIfNeededApi("export", err, &opts.Globals)
    44  	return err
    45  }
    46  
    47  // ExportInternal handles the internal workings of the export command. Returns an error.
    48  func (opts *ExportOptions) ExportInternal(rCtx *output.RenderCtx) error {
    49  	var err error
    50  	if err = opts.validateExport(); err != nil {
    51  		return err
    52  	}
    53  
    54  	timer := logger.NewTimer()
    55  	msg := "chifra export"
    56  	// EXISTING_CODE
    57  	monitorArray := make([]monitor.Monitor, 0, len(opts.Addrs))
    58  	if canceled, err := opts.FreshenMonitorsForExport(rCtx, &monitorArray); err != nil || canceled {
    59  		return err
    60  	}
    61  	// EXISTING_CODE
    62  	if opts.Globals.Decache {
    63  		err = opts.HandleDecache(rCtx, monitorArray)
    64  	} else if opts.Count {
    65  		err = opts.HandleCount(rCtx, monitorArray)
    66  	} else if opts.Receipts {
    67  		err = opts.HandleReceipts(rCtx, monitorArray)
    68  	} else if opts.Logs {
    69  		err = opts.HandleLogs(rCtx, monitorArray)
    70  	} else if opts.Traces {
    71  		err = opts.HandleTraces(rCtx, monitorArray)
    72  	} else if opts.Withdrawals {
    73  		err = opts.HandleWithdrawals(rCtx, monitorArray)
    74  	} else if opts.Appearances {
    75  		err = opts.HandleAppearances(rCtx, monitorArray)
    76  	} else if opts.Balances {
    77  		err = opts.HandleBalances(rCtx, monitorArray)
    78  	} else if opts.Neighbors {
    79  		err = opts.HandleNeighbors(rCtx, monitorArray)
    80  	} else if opts.Statements {
    81  		err = opts.HandleStatements(rCtx, monitorArray)
    82  	} else if opts.Accounting {
    83  		err = opts.HandleAccounting(rCtx, monitorArray)
    84  	} else {
    85  		err = opts.HandleShow(rCtx, monitorArray)
    86  	}
    87  	timer.Report(msg)
    88  
    89  	return err
    90  }
    91  
    92  // GetExportOptions returns the options for this tool so other tools may use it.
    93  func GetExportOptions(args []string, g *globals.GlobalOptions) *ExportOptions {
    94  	ret := exportFinishParse(args)
    95  	if g != nil {
    96  		ret.Globals = *g
    97  	}
    98  	return ret
    99  }