github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/cmd/root_initialize.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 cmd
     6  
     7  import (
     8  	"os"
     9  	"os/user"
    10  	"path/filepath"
    11  	"runtime"
    12  	"strings"
    13  
    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/logger"
    17  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
    18  )
    19  
    20  // Initialize makes sure everything is ready to run. These routines don't return if they aren't
    21  func Initialize() bool {
    22  	VerifyOs()
    23  	VerifyMigrations()
    24  
    25  	return true
    26  }
    27  
    28  // VerifyOs will panic if the operating system isn't cooperating
    29  func VerifyOs() {
    30  	userOs := runtime.GOOS
    31  	if userOs != "linux" && userOs != "darwin" && userOs != "windows" {
    32  		logger.Fatal("Unsupported operating system: ", userOs, "\n")
    33  	}
    34  
    35  	_, err := user.Current()
    36  	if err != nil {
    37  		logger.Fatal("Could not read user home directory\n")
    38  	}
    39  }
    40  
    41  // VerifyMigrations will panic if the installation is not properly migrated
    42  func VerifyMigrations() {
    43  	if utils.IsPermitted() {
    44  		// Allow certain status and config routes to pass so as to aide user in migrating...
    45  		return
    46  	}
    47  
    48  	user, _ := user.Current()
    49  
    50  	const doesNotExist string = `0002. A config item ({0}) is missing. See {https://trueblocks.io/docs/install/install-core/}.`
    51  	const shouldNotExist string = `0003. A config item ({0}) exists but should not. See {https://trueblocks.io/docs/install/install-core/}.`
    52  	const noChains string = `0004. The configuration file ({0}) contains no chain specifications. See {https://trueblocks.io/docs/install/install-core/}.`
    53  
    54  	// The old $HOME/.quickBlocks folder should not exist...
    55  	if _, err := os.Stat(filepath.Join(user.HomeDir, ".quickBlocks")); err == nil {
    56  		msg := strings.Replace(shouldNotExist, "{0}", "{~/.quickBlocks}", -1)
    57  		msg = colors.ColoredWith(msg, colors.Yellow)
    58  		logger.Fatal(msg)
    59  	}
    60  
    61  	// Both the config folder...
    62  	configFolder := config.PathToRootConfig()
    63  	if _, err := os.Stat(configFolder); err != nil {
    64  		msg := strings.Replace(doesNotExist, "{0}", "{"+configFolder+"}", -1)
    65  		msg = colors.ColoredWith(msg, colors.Yellow)
    66  		logger.Fatal(msg)
    67  	}
    68  
    69  	// ...and the config file better exist.
    70  	configFile := filepath.Join(configFolder, "trueBlocks.toml")
    71  	if _, err := os.Stat(configFile); err != nil {
    72  		msg := strings.Replace(doesNotExist, "{0}", "{"+configFile+"}", -1)
    73  		msg = colors.ColoredWith(msg, colors.Yellow)
    74  		logger.Fatal(msg)
    75  	}
    76  
    77  	// ...and some chains...
    78  	chainArray := config.GetChains()
    79  	if len(chainArray) == 0 {
    80  		msg := strings.Replace(noChains, "{0}", "{"+configFile+"}", -1)
    81  		msg = colors.ColoredWith(msg, colors.Yellow)
    82  		logger.Fatal(msg)
    83  	}
    84  
    85  	// We need to find the chain configuration path
    86  	chainConfigPath := config.MustGetPathToChainConfig("")
    87  	if _, err := os.Stat(chainConfigPath); err != nil {
    88  		msg := strings.Replace(doesNotExist, "{0}", "{"+chainConfigPath+"}", -1)
    89  		msg = colors.ColoredWith(msg, colors.Yellow)
    90  		logger.Fatal(msg)
    91  	}
    92  
    93  	// Make sure they've completed migrations prior to v1.0.0
    94  	items := []string{
    95  		"blocks",
    96  		"objs",
    97  		"recons",
    98  		"slurps",
    99  		"traces",
   100  		"txs",
   101  	}
   102  	for _, item := range items {
   103  		itemPath := filepath.Join(config.PathToCache(""), item)
   104  		if _, err := os.Stat(itemPath); err == nil {
   105  			msg := strings.Replace(shouldNotExist, "{0}", "{"+itemPath+"}", -1)
   106  			msg = colors.ColoredWith(msg, colors.Yellow)
   107  			logger.Fatal(msg)
   108  		}
   109  	}
   110  }