github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/names/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 namesPkg 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 // RunNames handles the names command for the command line. Returns error only as per cobra. 25 func RunNames(cmd *cobra.Command, args []string) error { 26 opts := namesFinishParse(args) 27 rCtx := output.NewRenderContext() 28 // EXISTING_CODE 29 var err1 error 30 if err1 = opts.LoadCrudDataIfNeeded(nil); err1 != nil { 31 return err1 32 } 33 // EXISTING_CODE 34 outputHelpers.SetWriterForCommand("names", &opts.Globals) 35 return opts.NamesInternal(rCtx) 36 } 37 38 // ServeNames handles the names command for the API. Returns an error. 39 func ServeNames(w http.ResponseWriter, r *http.Request) error { 40 opts := namesFinishParseApi(w, r) 41 rCtx := output.NewRenderContext() 42 // EXISTING_CODE 43 var err1 error 44 if err1 = opts.LoadCrudDataIfNeeded(r); err1 != nil { 45 return err1 46 } 47 // EXISTING_CODE 48 outputHelpers.InitJsonWriterApi("names", w, &opts.Globals) 49 err := opts.NamesInternal(rCtx) 50 outputHelpers.CloseJsonWriterIfNeededApi("names", err, &opts.Globals) 51 return err 52 } 53 54 // NamesInternal handles the internal workings of the names command. Returns an error. 55 func (opts *NamesOptions) NamesInternal(rCtx *output.RenderCtx) error { 56 var err error 57 if err = opts.validateNames(); err != nil { 58 return err 59 } 60 61 timer := logger.NewTimer() 62 msg := "chifra names" 63 // EXISTING_CODE 64 // EXISTING_CODE 65 if len(opts.Autoname) > 0 { 66 err = opts.HandleAutoname(rCtx) 67 } else if opts.Clean { 68 err = opts.HandleClean(rCtx) 69 } else if opts.Tags { 70 err = opts.HandleTags(rCtx) 71 } else if opts.anyCrud() { 72 err = opts.HandleCrud(rCtx) 73 } else { 74 err = opts.HandleShow(rCtx) 75 } 76 timer.Report(msg) 77 78 return err 79 } 80 81 // GetNamesOptions returns the options for this tool so other tools may use it. 82 func GetNamesOptions(args []string, g *globals.GlobalOptions) *NamesOptions { 83 ret := namesFinishParse(args) 84 if g != nil { 85 ret.Globals = *g 86 } 87 return ret 88 } 89 90 func (opts *NamesOptions) anyCrud() bool { 91 return opts.Create || 92 opts.Update || 93 opts.Delete || 94 opts.Undelete || 95 opts.Remove 96 }