github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/monitors/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 monitorsPkg 10 11 // EXISTING_CODE 12 import ( 13 "net/http" 14 "strings" 15 16 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals" 17 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 18 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output" 19 outputHelpers "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output/helpers" 20 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate" 21 "github.com/spf13/cobra" 22 ) 23 24 // EXISTING_CODE 25 26 // RunMonitors handles the monitors command for the command line. Returns error only as per cobra. 27 func RunMonitors(cmd *cobra.Command, args []string) error { 28 opts := monitorsFinishParse(args) 29 rCtx := output.NewRenderContext() 30 // EXISTING_CODE 31 // EXISTING_CODE 32 outputHelpers.SetWriterForCommand("monitors", &opts.Globals) 33 return opts.MonitorsInternal(rCtx) 34 } 35 36 // ServeMonitors handles the monitors command for the API. Returns an error. 37 func ServeMonitors(w http.ResponseWriter, r *http.Request) error { 38 opts := monitorsFinishParseApi(w, r) 39 rCtx := output.NewRenderContext() 40 // EXISTING_CODE 41 // TODO: can we move this to Validate? 42 var err1 error 43 if !opts.Globals.TestMode { // our test harness does not use DELETE 44 delOptions := "--delete, --undelete, or --remove" 45 if r.Method == "DELETE" { 46 if !opts.Delete && !opts.Undelete && !opts.Remove { 47 err1 = validate.Usage("Specify one of {0} when using the DELETE route.", delOptions) 48 } 49 } else { 50 if opts.Delete || opts.Undelete || opts.Remove { 51 delOptions = strings.Replace(delOptions, " or ", " and ", -1) 52 err1 = validate.Usage("The {0} options are not valid when using the GET route.", delOptions) 53 } 54 } 55 if err1 != nil { 56 return err1 57 } 58 } 59 // EXISTING_CODE 60 outputHelpers.InitJsonWriterApi("monitors", w, &opts.Globals) 61 err := opts.MonitorsInternal(rCtx) 62 outputHelpers.CloseJsonWriterIfNeededApi("monitors", err, &opts.Globals) 63 return err 64 } 65 66 // MonitorsInternal handles the internal workings of the monitors command. Returns an error. 67 func (opts *MonitorsOptions) MonitorsInternal(rCtx *output.RenderCtx) error { 68 var err error 69 if err = opts.validateMonitors(); err != nil { 70 return err 71 } 72 73 timer := logger.NewTimer() 74 msg := "chifra monitors" 75 // EXISTING_CODE 76 // EXISTING_CODE 77 if opts.Globals.Decache { 78 err = opts.HandleDecache(rCtx) 79 } else if opts.Count { 80 err = opts.HandleCount(rCtx) 81 } else if opts.Clean { 82 err = opts.HandleClean(rCtx) 83 } else if opts.List { 84 err = opts.HandleList(rCtx) 85 } else if opts.Watch { 86 err = opts.HandleWatch(rCtx) 87 } else if opts.anyCrud() { 88 err = opts.HandleCrud(rCtx) 89 } else { 90 err = opts.HandleShow(rCtx) 91 } 92 timer.Report(msg) 93 94 return err 95 } 96 97 // GetMonitorsOptions returns the options for this tool so other tools may use it. 98 func GetMonitorsOptions(args []string, g *globals.GlobalOptions) *MonitorsOptions { 99 ret := monitorsFinishParse(args) 100 if g != nil { 101 ret.Globals = *g 102 } 103 return ret 104 } 105 106 func (opts *MonitorsOptions) anyCrud() bool { 107 return opts.Delete || 108 opts.Undelete || 109 opts.Remove 110 }