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

     1  package history
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
     7  )
     8  
     9  // FromHistoryBool retrieves a key/value pair from the history file
    10  func FromHistoryBool(historyFile, key string) bool {
    11  	return strings.ToLower(FromHistory(historyFile, key)) == "true"
    12  }
    13  
    14  // FromHistory retrieves a key/value pair from the history file
    15  func FromHistory(historyFile, key string) string {
    16  	lines := file.AsciiFileToLines(historyFile)
    17  	for _, line := range lines {
    18  		if strings.HasPrefix(line, key+"=") {
    19  			return strings.Replace(line, key+"=", "", -1)
    20  		}
    21  	}
    22  	return ""
    23  }
    24  
    25  // ToHistory stores a key/value pair in the history file
    26  func ToHistory(historyFile, key, value string) error {
    27  	lines := file.AsciiFileToLines(historyFile)
    28  	for i, line := range lines {
    29  		if strings.HasPrefix(line, key+"=") {
    30  			lines[i] = key + "=" + value
    31  			return file.LinesToAsciiFile(historyFile, lines)
    32  		}
    33  	}
    34  	lines = append(lines, key+"="+value)
    35  	return file.LinesToAsciiFile(historyFile, lines)
    36  }