Replace the HomeDir flag with a AppDir flag (#1615)

This commit is contained in:
Elichai Turkel 2021-03-17 12:48:38 +02:00 committed by GitHub
parent 1a4161ffc0
commit caf251b7a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 55 additions and 55 deletions

View File

@ -171,7 +171,7 @@ func doUpgrades() error {
// dbPath returns the path to the block database given a database type. // dbPath returns the path to the block database given a database type.
func databasePath(cfg *config.Config) string { func databasePath(cfg *config.Config) string {
return filepath.Join(cfg.HomeDir, "data") return filepath.Join(cfg.AppDir, "data")
} }
func removeDatabase(cfg *config.Config) error { func removeDatabase(cfg *config.Config) error {

View File

@ -24,9 +24,9 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultHomeDir = util.AppDataDir("kaspaminer", false) defaultAppDir = util.AppDir("kaspaminer", false)
defaultLogFile = filepath.Join(defaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(defaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(defaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(defaultAppDir, defaultErrLogFilename)
defaultRPCServer = "localhost" defaultRPCServer = "localhost"
) )

View File

@ -54,13 +54,13 @@ const (
) )
var ( var (
// DefaultHomeDir is the default home directory for kaspad. // DefaultAppDir is the default home directory for kaspad.
DefaultHomeDir = util.AppDataDir("kaspad", false) DefaultAppDir = util.AppDir("kaspad", false)
defaultConfigFile = filepath.Join(DefaultHomeDir, defaultConfigFilename) defaultConfigFile = filepath.Join(DefaultAppDir, defaultConfigFilename)
defaultDataDir = filepath.Join(DefaultHomeDir) defaultDataDir = filepath.Join(DefaultAppDir)
defaultRPCKeyFile = filepath.Join(DefaultHomeDir, "rpc.key") defaultRPCKeyFile = filepath.Join(DefaultAppDir, "rpc.key")
defaultRPCCertFile = filepath.Join(DefaultHomeDir, "rpc.cert") defaultRPCCertFile = filepath.Join(DefaultAppDir, "rpc.cert")
) )
// RunServiceCommand is only set to a real function on Windows. It is used // RunServiceCommand is only set to a real function on Windows. It is used
@ -73,7 +73,7 @@ var RunServiceCommand func(string) error
type Flags struct { type Flags struct {
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"` ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
HomeDir string `short:"b" long:"homedir" description:"Directory to store data"` AppDir string `short:"b" long:"appdir" description:"Directory to store data"`
LogDir string `long:"logdir" description:"Directory to log output."` LogDir string `long:"logdir" description:"Directory to log output."`
AddPeers []string `short:"a" long:"addpeer" description:"Add a peer to connect with at startup"` AddPeers []string `short:"a" long:"addpeer" description:"Add a peer to connect with at startup"`
ConnectPeers []string `long:"connect" description:"Connect only to the specified peers at startup"` ConnectPeers []string `long:"connect" description:"Connect only to the specified peers at startup"`
@ -144,7 +144,7 @@ type ServiceOptions struct {
func cleanAndExpandPath(path string) string { func cleanAndExpandPath(path string) string {
// Expand initial ~ to OS specific home directory. // Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") { if strings.HasPrefix(path, "~") {
homeDir := filepath.Dir(DefaultHomeDir) homeDir := filepath.Dir(DefaultAppDir)
path = strings.Replace(path, "~", homeDir, 1) path = strings.Replace(path, "~", homeDir, 1)
} }
@ -173,7 +173,7 @@ func defaultFlags() *Flags {
RPCMaxClients: defaultMaxRPCClients, RPCMaxClients: defaultMaxRPCClients,
RPCMaxWebsockets: defaultMaxRPCWebsockets, RPCMaxWebsockets: defaultMaxRPCWebsockets,
RPCMaxConcurrentReqs: defaultMaxRPCConcurrentReqs, RPCMaxConcurrentReqs: defaultMaxRPCConcurrentReqs,
HomeDir: defaultDataDir, AppDir: defaultDataDir,
RPCKey: defaultRPCKeyFile, RPCKey: defaultRPCKeyFile,
RPCCert: defaultRPCCertFile, RPCCert: defaultRPCCertFile,
BlockMaxMass: defaultBlockMaxMass, BlockMaxMass: defaultBlockMaxMass,
@ -266,7 +266,7 @@ func LoadConfig() (*Config, error) {
// Create the home directory if it doesn't already exist. // Create the home directory if it doesn't already exist.
funcName := "loadConfig" funcName := "loadConfig"
err = os.MkdirAll(DefaultHomeDir, 0700) err = os.MkdirAll(DefaultAppDir, 0700)
if err != nil { if err != nil {
// Show a nicer error message if it's because a symlink is // Show a nicer error message if it's because a symlink is
// linked to a directory that does not exist (probably because // linked to a directory that does not exist (probably because
@ -309,17 +309,17 @@ func LoadConfig() (*Config, error) {
} }
cfg.RelayNonStd = relayNonStd cfg.RelayNonStd = relayNonStd
cfg.HomeDir = cleanAndExpandPath(cfg.HomeDir) cfg.AppDir = cleanAndExpandPath(cfg.AppDir)
// Append the network type to the home directory so it is "namespaced" // Append the network type to the app directory so it is "namespaced"
// per network. // per network.
// All data is specific to a network, so namespacing the data directory // All data is specific to a network, so namespacing the data directory
// means each individual piece of serialized data does not have to // means each individual piece of serialized data does not have to
// worry about changing names per network and such. // worry about changing names per network and such.
cfg.HomeDir = filepath.Join(cfg.HomeDir, cfg.NetParams().Name) cfg.AppDir = filepath.Join(cfg.AppDir, cfg.NetParams().Name)
// Logs directory is usually under the home directory, unless otherwise specified // Logs directory is usually under the home directory, unless otherwise specified
if cfg.LogDir == "" { if cfg.LogDir == "" {
cfg.LogDir = filepath.Join(cfg.HomeDir, defaultLogDirname) cfg.LogDir = filepath.Join(cfg.AppDir, defaultLogDirname)
} }
cfg.LogDir = cleanAndExpandPath(cfg.LogDir) cfg.LogDir = cleanAndExpandPath(cfg.LogDir)

View File

@ -18,8 +18,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -2,5 +2,5 @@ package common
import "github.com/kaspanet/kaspad/util" import "github.com/kaspanet/kaspad/util"
// DefaultHomeDir is the default home directory to be used by all tests // DefaultAppDir is the default app directory to be used by all tests
var DefaultHomeDir = util.AppDataDir("stability-tests", false) var DefaultAppDir = util.AppDir("stability-tests", false)

View File

@ -18,8 +18,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -14,8 +14,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -18,8 +18,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -14,8 +14,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -16,8 +16,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -19,8 +19,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -17,8 +17,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -17,8 +17,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -18,8 +18,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -15,8 +15,8 @@ const (
var ( var (
// Default configuration options // Default configuration options
defaultLogFile = filepath.Join(common.DefaultHomeDir, defaultLogFilename) defaultLogFile = filepath.Join(common.DefaultAppDir, defaultLogFilename)
defaultErrLogFile = filepath.Join(common.DefaultHomeDir, defaultErrLogFilename) defaultErrLogFile = filepath.Join(common.DefaultAppDir, defaultErrLogFilename)
) )
type configFlags struct { type configFlags struct {

View File

@ -32,7 +32,7 @@ const (
func setConfig(t *testing.T, harness *appHarness) { func setConfig(t *testing.T, harness *appHarness) {
harness.config = commonConfig() harness.config = commonConfig()
harness.config.HomeDir = randomDirectory(t) harness.config.AppDir = randomDirectory(t)
harness.config.Listeners = []string{harness.p2pAddress} harness.config.Listeners = []string{harness.p2pAddress}
harness.config.RPCListeners = []string{harness.rpcAddress} harness.config.RPCListeners = []string{harness.rpcAddress}
harness.config.UTXOIndex = harness.utxoIndex harness.config.UTXOIndex = harness.utxoIndex

View File

@ -134,6 +134,6 @@ func setDatabaseContext(t *testing.T, harness *appHarness) {
} }
func openDB(cfg *config.Config) (database.Database, error) { func openDB(cfg *config.Config) (database.Database, error) {
dbPath := filepath.Join(cfg.HomeDir, "db") dbPath := filepath.Join(cfg.AppDir, "db")
return ldb.NewLevelDB(dbPath, 8) return ldb.NewLevelDB(dbPath, 8)
} }

View File

@ -13,12 +13,12 @@ import (
"unicode" "unicode"
) )
// appDataDir returns an operating system specific directory to be used for // appDir returns an operating system specific directory to be used for
// storing application data for an application. See AppDataDir for more // storing application data for an application. See AppDir for more
// details. This unexported version takes an operating system argument // details. This unexported version takes an operating system argument
// primarily to enable the testing package to properly test the function by // primarily to enable the testing package to properly test the function by
// forcing an operating system that is not the currently one. // forcing an operating system that is not the currently one.
func appDataDir(goos, appName string, roaming bool) string { func appDir(goos, appName string, roaming bool) string {
if appName == "" || appName == "." { if appName == "" || appName == "." {
return "." return "."
} }
@ -79,7 +79,7 @@ func appDataDir(goos, appName string, roaming bool) string {
return "." return "."
} }
// AppDataDir returns an operating system specific directory to be used for // AppDir returns an operating system specific directory to be used for
// storing application data for an application. // storing application data for an application.
// //
// The appName parameter is the name of the application the data directory is // The appName parameter is the name of the application the data directory is
@ -95,11 +95,11 @@ func appDataDir(goos, appName string, roaming bool) string {
// (%LOCALAPPDATA%) that is used by default. // (%LOCALAPPDATA%) that is used by default.
// //
// Example results: // Example results:
// dir := AppDataDir("myapp", false) // dir := AppDir("myapp", false)
// POSIX (Linux/BSD): ~/.myapp // POSIX (Linux/BSD): ~/.myapp
// Mac OS: $HOME/Library/Application Support/Myapp // Mac OS: $HOME/Library/Application Support/Myapp
// Windows: %LOCALAPPDATA%\Myapp // Windows: %LOCALAPPDATA%\Myapp
// Plan 9: $home/myapp // Plan 9: $home/myapp
func AppDataDir(appName string, roaming bool) string { func AppDir(appName string, roaming bool) string {
return appDataDir(runtime.GOOS, appName, roaming) return appDir(runtime.GOOS, appName, roaming)
} }

View File

@ -15,7 +15,7 @@ import (
"github.com/kaspanet/kaspad/util" "github.com/kaspanet/kaspad/util"
) )
// TestAppDataDir tests the API for AppDataDir to ensure it gives expected // TestAppDataDir tests the API for AppDir to ensure it gives expected
// results for various operating systems. // results for various operating systems.
func TestAppDataDir(t *testing.T) { func TestAppDataDir(t *testing.T) {
// App name plus upper and lowercase variants. // App name plus upper and lowercase variants.
@ -124,7 +124,7 @@ func TestAppDataDir(t *testing.T) {
for i, test := range tests { for i, test := range tests {
ret := util.TstAppDataDir(test.goos, test.appName, test.roaming) ret := util.TstAppDataDir(test.goos, test.appName, test.roaming)
if ret != test.want { if ret != test.want {
t.Errorf("appDataDir #%d (%s) does not match - "+ t.Errorf("appDir #%d (%s) does not match - "+
"expected got %s, want %s", i, test.goos, ret, "expected got %s, want %s", i, test.goos, ret,
test.want) test.want)
continue continue

View File

@ -16,10 +16,10 @@ import (
"golang.org/x/crypto/ripemd160" "golang.org/x/crypto/ripemd160"
) )
// TstAppDataDir makes the internal appDataDir function available to the test // TstAppDataDir makes the internal appDir function available to the test
// package. // package.
func TstAppDataDir(goos, appName string, roaming bool) string { func TstAppDataDir(goos, appName string, roaming bool) string {
return appDataDir(goos, appName, roaming) return appDir(goos, appName, roaming)
} }
func TstAddressPubKeyHash(prefix Bech32Prefix, hash [ripemd160.Size]byte) *AddressPubKeyHash { func TstAddressPubKeyHash(prefix Bech32Prefix, hash [ripemd160.Size]byte) *AddressPubKeyHash {