github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/config/paths.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 config 6 7 import ( 8 "os" 9 "path/filepath" 10 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 13 ) 14 15 // MustGetPathToChainConfig returns the chain-specific config folder ignoring errors 16 func MustGetPathToChainConfig(chain string) string { 17 path, _ := PathToChainConfig(chain) 18 return path 19 } 20 21 // PathToChainConfig returns the chain-specific config folder 22 func PathToChainConfig(chain string) (string, error) { 23 // We always need a chain 24 if len(chain) == 0 { 25 chain = GetSettings().DefaultChain 26 } 27 ret := PathToRootConfig() 28 29 // Our configuration files are always in ./config folder relative to top most folder 30 cfgFolder := filepath.Join(ret, "config", chain) 31 _, err := os.Stat(cfgFolder) 32 return cfgFolder, err 33 } 34 35 // PathToManifest returns the path to the manifest database per chain 36 func PathToManifest(chain string) string { 37 return filepath.Join(PathToIndex(chain), "manifest.json") 38 } 39 40 // PathToTimestamps returns the path to the timestamps database per chain 41 func PathToTimestamps(chain string) string { 42 return filepath.Join(PathToIndex(chain), "ts.bin") 43 } 44 45 // PathToIndex returns the one and only indexPath 46 func PathToIndex(chain string) string { 47 // We need the index path from either XDG which dominates or the config file 48 indexPath, err := pathFromXDG("XDG_CACHE_HOME") 49 if err != nil { 50 logger.Fatal(err) 51 } else if len(indexPath) == 0 { 52 indexPath = trueBlocksConfig.Settings.IndexPath 53 } 54 55 // Probably already true, but can't hurt to be sure 56 if filepath.Base(indexPath) != "unchained" { 57 indexPath = filepath.Join(indexPath, "unchained") 58 } 59 60 // We always have to have a chain... 61 if len(chain) == 0 { 62 chain = trueBlocksConfig.Settings.DefaultChain 63 } 64 65 // We know what we want, create it if it doesn't exist and return it 66 newPath := filepath.Join(indexPath, chain) 67 EstablishIndexPaths(newPath) 68 return newPath 69 } 70 71 // PathToCache returns the one and only cachePath 72 func PathToCache(chain string) string { 73 // We need the index path from either XDG which dominates or the config file 74 cachePath, err := pathFromXDG("XDG_CACHE_HOME") 75 if err != nil { 76 logger.Fatal(err) 77 } else if len(cachePath) == 0 { 78 cachePath = GetSettings().CachePath 79 } 80 81 // Probably already true, but can't hurt to be sure 82 if filepath.Base(cachePath) != "cache" { 83 cachePath = filepath.Join(cachePath, "cache") 84 } 85 86 // We always have to have a chain... 87 if len(chain) == 0 { 88 chain = GetSettings().DefaultChain 89 } 90 91 // We know what we want, create it if it doesn't exist and return it 92 newPath := filepath.Join(cachePath, chain) 93 EstablishCachePaths(newPath) 94 return newPath 95 } 96 97 // EstablishCachePaths sets up the cache folders and subfolders. It only returns if it succeeds. 98 func EstablishCachePaths(cachePath string) { 99 folders := []string{ 100 "abis", 101 "monitors", 102 filepath.Join("monitors", "staging"), 103 "names", 104 "tmp", 105 "v1", 106 } 107 108 if err := file.EstablishFolders(cachePath, folders); err != nil { 109 logger.Fatal(err) 110 } 111 } 112 113 // EstablishIndexPaths sets up the index path and subfolders. It only returns if it succeeds. 114 func EstablishIndexPaths(indexPath string) { 115 folders := []string{ 116 "blooms", "finalized", "maps", "ripe", "staging", "unripe", 117 } 118 _, err := os.Stat(filepath.Join(indexPath, folders[len(folders)-1])) 119 if err == nil { 120 // If the last path already exists, assume we've been here before 121 return 122 } 123 124 if err := file.EstablishFolders(indexPath, folders); err != nil { 125 logger.Fatal(err) 126 } 127 }