github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/scrape/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 scrapePkg
    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  
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  // EXISTING_CODE
    24  
    25  // RunScrape handles the scrape command for the command line. Returns error only as per cobra.
    26  func RunScrape(cmd *cobra.Command, args []string) error {
    27  	opts := scrapeFinishParse(args)
    28  	rCtx := output.NewRenderContext()
    29  	// EXISTING_CODE
    30  	// EXISTING_CODE
    31  	outputHelpers.SetWriterForCommand("scrape", &opts.Globals)
    32  	return opts.ScrapeInternal(rCtx)
    33  }
    34  
    35  // ServeScrape handles the scrape command for the API. Returns an error.
    36  func ServeScrape(w http.ResponseWriter, r *http.Request) error {
    37  	opts := scrapeFinishParseApi(w, r)
    38  	rCtx := output.NewRenderContext()
    39  	// EXISTING_CODE
    40  	// EXISTING_CODE
    41  	outputHelpers.InitJsonWriterApi("scrape", w, &opts.Globals)
    42  	err := opts.ScrapeInternal(rCtx)
    43  	outputHelpers.CloseJsonWriterIfNeededApi("scrape", err, &opts.Globals)
    44  	return err
    45  }
    46  
    47  // ScrapeInternal handles the internal workings of the scrape command. Returns an error.
    48  func (opts *ScrapeOptions) ScrapeInternal(rCtx *output.RenderCtx) error {
    49  	var err error
    50  	if err = opts.validateScrape(); err != nil {
    51  		return err
    52  	}
    53  
    54  	timer := logger.NewTimer()
    55  	msg := "chifra scrape"
    56  	// EXISTING_CODE
    57  	// EXISTING_CODE
    58  	if opts.Touch > 0 {
    59  		err = opts.HandleTouch(rCtx)
    60  	} else {
    61  		err = opts.HandleShow(rCtx)
    62  	}
    63  	timer.Report(msg)
    64  
    65  	return err
    66  }
    67  
    68  // GetScrapeOptions returns the options for this tool so other tools may use it.
    69  func GetScrapeOptions(args []string, g *globals.GlobalOptions) *ScrapeOptions {
    70  	ret := scrapeFinishParse(args)
    71  	if g != nil {
    72  		ret.Globals = *g
    73  	}
    74  	return ret
    75  }