github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/cmd/when.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 whenPkg "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/when" 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 // whenCmd represents the when command 26 var whenCmd = &cobra.Command{ 27 Use: usageWhen, 28 Long: longWhen, 29 Version: versionText, 30 PreRun: outputHelpers.PreRunWithJsonWriter("when", func() *globals.GlobalOptions { 31 return &whenPkg.GetOptions().Globals 32 }), 33 RunE: file.RunWithFileSupport("when", whenPkg.RunWhen, whenPkg.ResetOptions), 34 PostRun: outputHelpers.PostRunWithJsonWriter(func() *globals.GlobalOptions { 35 return &whenPkg.GetOptions().Globals 36 }), 37 } 38 39 const usageWhen = `when [flags] < block | date > [ block... | date... ] 40 41 Arguments: 42 blocks - one or more dates, block numbers, hashes, or special named blocks (see notes)` 43 44 const longWhen = `Purpose: 45 Find block(s) based on date, blockNum, timestamp, or 'special'.` 46 47 const notesWhen = ` 48 Notes: 49 - The block list may contain any combination of number, hash, date, special named blocks. 50 - Block numbers, timestamps, or dates in the future are estimated with 13 second blocks. 51 - Dates must be formatted in JSON format: YYYY-MM-DD[THH[:MM[:SS]]].` 52 53 func init() { 54 var capabilities caps.Capability // capabilities for chifra when 55 capabilities = capabilities.Add(caps.Default) 56 capabilities = capabilities.Add(caps.Caching) 57 58 whenCmd.Flags().SortFlags = false 59 60 whenCmd.Flags().BoolVarP(&whenPkg.GetOptions().List, "list", "l", false, `export a list of the 'special' blocks`) 61 whenCmd.Flags().BoolVarP(&whenPkg.GetOptions().Timestamps, "timestamps", "t", false, `display or process timestamps`) 62 whenCmd.Flags().BoolVarP(&whenPkg.GetOptions().Count, "count", "U", false, `with --timestamps only, returns the number of timestamps in the cache`) 63 whenCmd.Flags().Uint64VarP((*uint64)(&whenPkg.GetOptions().Truncate), "truncate", "n", 0, `with --timestamps only, truncates the timestamp file at this block (hidden)`) 64 whenCmd.Flags().BoolVarP(&whenPkg.GetOptions().Repair, "repair", "r", false, `with --timestamps only, repairs block(s) in the block range by re-querying from the chain`) 65 whenCmd.Flags().BoolVarP(&whenPkg.GetOptions().Check, "check", "c", false, `with --timestamps only, checks the validity of the timestamp data`) 66 whenCmd.Flags().BoolVarP(&whenPkg.GetOptions().Update, "update", "u", false, `with --timestamps only, bring the timestamp database forward to the latest block`) 67 whenCmd.Flags().BoolVarP(&whenPkg.GetOptions().Deep, "deep", "d", false, `with --timestamps --check only, verifies timestamps from on chain (slow)`) 68 if os.Getenv("TEST_MODE") != "true" { 69 _ = whenCmd.Flags().MarkHidden("truncate") 70 } 71 globals.InitGlobals("when", whenCmd, &whenPkg.GetOptions().Globals, capabilities) 72 73 whenCmd.SetUsageTemplate(UsageWithNotes(notesWhen)) 74 whenCmd.SetOut(os.Stderr) 75 76 // EXISTING_CODE 77 // EXISTING_CODE 78 79 chifraCmd.AddCommand(whenCmd) 80 }