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