github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/abis/handle_decache.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 abisPkg
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/abi"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    16  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
    17  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    18  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    19  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    20  )
    21  
    22  func (opts *AbisOptions) HandleDecache(rCtx *output.RenderCtx) error {
    23  	chain := opts.Globals.Chain
    24  	testMode := opts.Globals.TestMode
    25  
    26  	if len(opts.Addrs) == 0 {
    27  		if testMode {
    28  			logger.Info("Cleaning empty abis is not tested in test mode.")
    29  		} else {
    30  			filenameChan := make(chan walk.CacheFileInfo)
    31  			var nRoutines = 1
    32  			go walk.WalkCacheFolder(rCtx.Ctx, chain, walk.Cache_Abis, nil, filenameChan)
    33  			for result := range filenameChan {
    34  				switch result.Type {
    35  				case walk.Cache_Abis:
    36  					path := result.Path
    37  					skip := !walk.IsCacheType(path, walk.Cache_Abis, true /* checkExt */)
    38  					if !skip {
    39  						size := file.FileSize(path)
    40  						if size <= int64(len(abi.AbiNotFound)+5) {
    41  							// Over time, the contents of an empty ABI file have taken on various shapes. We clean that up here before comparing.
    42  							clean := func(in string) string {
    43  								return strings.Replace(strings.Replace(in, " ", "", -1), "\"", "", -1)
    44  							}
    45  							contents := clean(file.AsciiFileToString(path))
    46  							empty := clean(abi.AbiNotFound)
    47  							if len(contents) == 0 || contents == empty {
    48  								if file.FileExists(path) {
    49  									_, fn := filepath.Split(path)
    50  									if err := os.Remove(path); err != nil {
    51  										msg := fmt.Sprintf("%sCould not remove abi file %s: %s.%s", colors.Red, fn, err, colors.Off)
    52  										logger.Warn(msg)
    53  									} else {
    54  										msg := fmt.Sprintf("%sAbi file %s removed: %d bytes.%s", colors.Green, fn, size, colors.Off)
    55  										logger.Warn(msg)
    56  									}
    57  								}
    58  							}
    59  						}
    60  					}
    61  				case walk.Cache_NotACache:
    62  					nRoutines--
    63  					if nRoutines == 0 {
    64  						close(filenameChan)
    65  					}
    66  				}
    67  			}
    68  		}
    69  	} else {
    70  		for _, addr := range opts.Addrs {
    71  			path := filepath.Join(config.PathToCache(chain), "abis", addr + ".json")
    72  			if file.FileExists(path) {
    73  				if err := os.Remove(path); err != nil {
    74  					logger.Warn(colors.Red+"Could not remove abi for address", addr, ":", err, "."+colors.Off)
    75  				} else {
    76  					logger.Info(colors.Green+"Abi file for address", addr, "removed."+colors.Off)
    77  				}
    78  			}
    79  		}
    80  	}
    81  
    82  	return nil
    83  }