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