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