github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/monitors/handle_count.go (about)

     1  // Copyright 2021 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  package monitorsPkg
     6  
     7  import (
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    15  )
    16  
    17  // HandleCount handles the chifra abis --count command.
    18  func (opts *MonitorsOptions) HandleCount(rCtx *output.RenderCtx) error {
    19  	testMode := opts.Globals.TestMode
    20  	chain := opts.Globals.Chain
    21  
    22  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    23  		count := uint64(0)
    24  		if testMode {
    25  			count = 199
    26  			if opts.Staged {
    27  				count = 99
    28  			}
    29  		} else {
    30  			vFunc := func(fn string, vP any) (bool, error) {
    31  				_, name := filepath.Split(fn)
    32  				incStaged := opts.Staged
    33  				isStaging := strings.Contains(fn, "staging")
    34  				isMonitor := strings.HasSuffix(name, ".mon.bin")
    35  				include := isMonitor && (incStaged || !isStaging)
    36  				if include {
    37  					count++
    38  				}
    39  				return true, nil
    40  			}
    41  			path := filepath.Join(config.PathToCache(chain), "monitors")
    42  			_ = walk.ForEveryFileInFolder(path, vFunc, errorChan)
    43  		}
    44  
    45  		s := types.Count{
    46  			Count: count,
    47  		}
    48  
    49  		modelChan <- &s
    50  	}
    51  
    52  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts())
    53  }