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