github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/config/flags_directory.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package config
    19  
    20  import (
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/urfave/cli/v2"
    25  )
    26  
    27  var (
    28  	// FlagConfigDir directory containing all configuration files.
    29  	FlagConfigDir = cli.StringFlag{
    30  		Name:  "config-dir",
    31  		Usage: "Config directory containing all configuration files",
    32  	}
    33  	// FlagDataDir data directory for keystore and other persistent files.
    34  	FlagDataDir = cli.StringFlag{
    35  		Name:  "data-dir",
    36  		Usage: "Data directory containing keystore & other persistent files",
    37  	}
    38  	// FlagNodeUIDir directory containing downloaded nodeUI releases
    39  	FlagNodeUIDir = cli.StringFlag{
    40  		Name:  "node-ui-dir",
    41  		Usage: "Directory containing downloaded nodeUI releases",
    42  	}
    43  	// FlagLogDir is a directory for storing log files.
    44  	FlagLogDir = cli.StringFlag{
    45  		Name:  "log-dir",
    46  		Usage: "Log directory for storing log files. data-dir/logs is used if not specified.",
    47  	}
    48  	// FlagRuntimeDir runtime writable directory for temporary files.
    49  	FlagRuntimeDir = cli.StringFlag{
    50  		Name:  "runtime-dir",
    51  		Usage: "Runtime writable directory for temp files",
    52  	}
    53  	// FlagScriptDir directory containing script and helper files.
    54  	FlagScriptDir = cli.StringFlag{
    55  		Name:  "script-dir",
    56  		Usage: "Script directory containing all script and helper files",
    57  	}
    58  )
    59  
    60  // RegisterFlagsDirectory function register directory flags to flag list
    61  func RegisterFlagsDirectory(flags *[]cli.Flag) error {
    62  	userHomeDir, err := os.UserHomeDir()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	currentDir, err := getExecutableDir()
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	FlagDataDir.Value = filepath.Join(userHomeDir, ".mysterium")
    73  	FlagConfigDir.Value = FlagDataDir.Value
    74  	FlagLogDir.Value = filepath.Join(FlagDataDir.Value, "logs")
    75  	FlagRuntimeDir.Value = currentDir
    76  	FlagScriptDir.Value = filepath.Join(currentDir, "config")
    77  	FlagNodeUIDir.Value = filepath.Join(FlagDataDir.Value, "nodeui")
    78  
    79  	*flags = append(*flags,
    80  		&FlagConfigDir,
    81  		&FlagDataDir,
    82  		&FlagLogDir,
    83  		&FlagRuntimeDir,
    84  		&FlagScriptDir,
    85  		&FlagNodeUIDir,
    86  	)
    87  	return nil
    88  }
    89  
    90  // ParseFlagsDirectory function fills in directory options from CLI context
    91  func ParseFlagsDirectory(ctx *cli.Context) {
    92  	Current.ParseStringFlag(ctx, FlagConfigDir)
    93  	Current.ParseStringFlag(ctx, FlagDataDir)
    94  	Current.ParseStringFlag(ctx, FlagLogDir)
    95  	Current.ParseStringFlag(ctx, FlagRuntimeDir)
    96  	Current.ParseStringFlag(ctx, FlagScriptDir)
    97  	Current.ParseStringFlag(ctx, FlagNodeUIDir)
    98  }
    99  
   100  func getExecutableDir() (string, error) {
   101  	executable, err := os.Executable()
   102  	if err != nil {
   103  		return "", err
   104  	}
   105  
   106  	return filepath.Dir(executable), nil
   107  }