github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/cmd/monitors.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 cmd 10 11 // EXISTING_CODE 12 import ( 13 "os" 14 15 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals" 16 monitorsPkg "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/monitors" 17 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/caps" 18 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file" 19 outputHelpers "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output/helpers" 20 "github.com/spf13/cobra" 21 ) 22 23 // EXISTING_CODE 24 25 // monitorsCmd represents the monitors command 26 var monitorsCmd = &cobra.Command{ 27 Use: usageMonitors, 28 Long: longMonitors, 29 Version: versionText, 30 PreRun: outputHelpers.PreRunWithJsonWriter("monitors", func() *globals.GlobalOptions { 31 return &monitorsPkg.GetOptions().Globals 32 }), 33 RunE: file.RunWithFileSupport("monitors", monitorsPkg.RunMonitors, monitorsPkg.ResetOptions), 34 PostRun: outputHelpers.PostRunWithJsonWriter(func() *globals.GlobalOptions { 35 return &monitorsPkg.GetOptions().Globals 36 }), 37 } 38 39 const usageMonitors = `monitors [flags] <address> [address...] 40 41 Arguments: 42 addrs - one or more addresses (0x...) to process` 43 44 const longMonitors = `Purpose: 45 Add, remove, clean, and list address monitors.` 46 47 const notesMonitors = ` 48 Notes: 49 - An address must be either an ENS name or start with '0x' and be forty-two characters long. 50 - If no address is presented to the --clean command, all existing monitors will be cleaned. 51 - The --watch option requires two additional parameters to be specified: --watchlist and --commands. 52 - Addresses provided on the command line are ignored in --watch mode. 53 - Providing the value existing to the --watchlist monitors all existing monitor files (see --list).` 54 55 func init() { 56 var capabilities caps.Capability // capabilities for chifra monitors 57 capabilities = capabilities.Add(caps.Default) 58 capabilities = capabilities.Add(caps.Caching) 59 capabilities = capabilities.Add(caps.Names) 60 61 monitorsCmd.Flags().SortFlags = false 62 63 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().Delete, "delete", "", false, `delete a monitor, but do not remove it`) 64 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().Undelete, "undelete", "", false, `undelete a previously deleted monitor`) 65 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().Remove, "remove", "", false, `remove a previously deleted monitor`) 66 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().Clean, "clean", "C", false, `clean (i.e. remove duplicate appearances) from monitors, optionally clear stage`) 67 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().List, "list", "l", false, `list monitors in the cache (--verbose for more detail)`) 68 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().Count, "count", "c", false, `show the number of active monitors (included deleted but not removed monitors)`) 69 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().Staged, "staged", "S", false, `for --clean, --list, and --count options only, include staged monitors`) 70 monitorsCmd.Flags().BoolVarP(&monitorsPkg.GetOptions().Watch, "watch", "w", false, `continually scan for new blocks and extract data as per the command file`) 71 monitorsCmd.Flags().StringVarP(&monitorsPkg.GetOptions().Watchlist, "watchlist", "a", "", `available with --watch option only, a file containing the addresses to watch`) 72 monitorsCmd.Flags().StringVarP(&monitorsPkg.GetOptions().Commands, "commands", "d", "", `available with --watch option only, the file containing the list of commands to apply to each watched address`) 73 monitorsCmd.Flags().Uint64VarP(&monitorsPkg.GetOptions().BatchSize, "batch_size", "b", 8, `available with --watch option only, the number of monitors to process in each batch`) 74 monitorsCmd.Flags().Uint64VarP(&monitorsPkg.GetOptions().RunCount, "run_count", "u", 0, `available with --watch option only, run the monitor this many times, then quit`) 75 monitorsCmd.Flags().Float64VarP(&monitorsPkg.GetOptions().Sleep, "sleep", "s", 14, `available with --watch option only, the number of seconds to sleep between runs`) 76 globals.InitGlobals("monitors", monitorsCmd, &monitorsPkg.GetOptions().Globals, capabilities) 77 78 monitorsCmd.SetUsageTemplate(UsageWithNotes(notesMonitors)) 79 monitorsCmd.SetOut(os.Stderr) 80 81 // EXISTING_CODE 82 // EXISTING_CODE 83 84 chifraCmd.AddCommand(monitorsCmd) 85 }