github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/chunks/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 chunksPkg 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/base" 17 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/identifiers" 18 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 19 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output" 20 outputHelpers "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output/helpers" 21 "github.com/spf13/cobra" 22 ) 23 24 // EXISTING_CODE 25 26 // RunChunks handles the chunks command for the command line. Returns error only as per cobra. 27 func RunChunks(cmd *cobra.Command, args []string) error { 28 opts := chunksFinishParse(args) 29 rCtx := output.NewRenderContext() 30 // EXISTING_CODE 31 // EXISTING_CODE 32 outputHelpers.SetWriterForCommand("chunks", &opts.Globals) 33 return opts.ChunksInternal(rCtx) 34 } 35 36 // ServeChunks handles the chunks command for the API. Returns an error. 37 func ServeChunks(w http.ResponseWriter, r *http.Request) error { 38 opts := chunksFinishParseApi(w, r) 39 rCtx := output.NewRenderContext() 40 // EXISTING_CODE 41 // EXISTING_CODE 42 outputHelpers.InitJsonWriterApi("chunks", w, &opts.Globals) 43 err := opts.ChunksInternal(rCtx) 44 outputHelpers.CloseJsonWriterIfNeededApi("chunks", err, &opts.Globals) 45 return err 46 } 47 48 // ChunksInternal handles the internal workings of the chunks command. Returns an error. 49 func (opts *ChunksOptions) ChunksInternal(rCtx *output.RenderCtx) error { 50 var err error 51 if err = opts.validateChunks(); err != nil { 52 return err 53 } 54 55 timer := logger.NewTimer() 56 msg := "chifra chunks" 57 // EXISTING_CODE 58 chain := opts.Globals.Chain 59 blockNums, err := identifiers.GetBlockNumbers(chain, opts.BlockIds) 60 if err != nil { 61 return err 62 } 63 if opts.Globals.TestMode && len(blockNums) > 200 { 64 blockNums = blockNums[:200] 65 } 66 if opts.Count { 67 err = opts.HandleCounts(rCtx, blockNums) 68 } else 69 // EXISTING_CODE 70 if opts.Check { 71 err = opts.HandleCheck(rCtx, blockNums) 72 } else if opts.List { 73 err = opts.HandleList(rCtx, blockNums) 74 } else if opts.Unpin { 75 err = opts.HandleUnpin(rCtx, blockNums) 76 } else if len(opts.Tag) > 0 { 77 err = opts.HandleTag(rCtx, blockNums) 78 } else if opts.Diff { 79 err = opts.HandleDiff(rCtx, blockNums) 80 } else if opts.Pin { 81 err = opts.HandlePin(rCtx, blockNums) 82 } else if opts.Publish { 83 err = opts.HandlePublish(rCtx, blockNums) 84 } else if opts.Truncate != base.NOPOSN { 85 err = opts.HandleTruncate(rCtx, blockNums) 86 } else { 87 err = opts.HandleShow(rCtx, blockNums) 88 } 89 timer.Report(msg) 90 91 return err 92 } 93 94 // GetChunksOptions returns the options for this tool so other tools may use it. 95 func GetChunksOptions(args []string, g *globals.GlobalOptions) *ChunksOptions { 96 ret := chunksFinishParse(args) 97 if g != nil { 98 ret.Globals = *g 99 } 100 return ret 101 }