code.vegaprotocol.io/vega@v0.79.0/paths/default.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package paths
    17  
    18  import (
    19  	"fmt"
    20  	"path/filepath"
    21  
    22  	"github.com/adrg/xdg"
    23  )
    24  
    25  // The default Vega file structure is mapped on the XDG standard. This standard
    26  // defines where the files should be looked for, depending on their purpose,
    27  // through environment variables, prefixed by `$XDG_`. The value of these
    28  // variables matches the standards of the platform the program runs on.
    29  //
    30  // More on XDG at:
    31  // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
    32  //
    33  // At each location, Vega files are grouped under a `vega` folder, as follows
    34  // `$XDG_*/vega`, before being sorted in sub-folders. The file structure of
    35  // these sub-folder is described in paths.go.
    36  //
    37  // Default file structure:
    38  //
    39  // $XDG_CACHE_HOME
    40  // └── vega
    41  //
    42  // $XDG_CONFIG_HOME
    43  // └── vega
    44  //
    45  // $XDG_DATA_HOME
    46  // └── vega
    47  //
    48  // $XDG_STATE_HOME
    49  // └── vega
    50  
    51  type DefaultPaths struct{}
    52  
    53  // CreateCachePathFor builds the default path for a cache file and creates
    54  // intermediate directories, if needed.
    55  func (p *DefaultPaths) CreateCachePathFor(relFilePath CachePath) (string, error) {
    56  	return CreateDefaultCachePathFor(relFilePath)
    57  }
    58  
    59  // CreateCacheDirFor builds the default path for a cache directory and creates
    60  // it, along with intermediate directories, if needed.
    61  func (p *DefaultPaths) CreateCacheDirFor(relDirPath CachePath) (string, error) {
    62  	return CreateDefaultCacheDirFor(relDirPath)
    63  }
    64  
    65  // CreateConfigPathFor builds the default path for a configuration file and
    66  // creates intermediate directories, if needed.
    67  func (p *DefaultPaths) CreateConfigPathFor(relFilePath ConfigPath) (string, error) {
    68  	return CreateDefaultConfigPathFor(relFilePath)
    69  }
    70  
    71  // CreateConfigDirFor builds the default path for a config directory and creates
    72  // it, along with intermediate directories, if needed.
    73  func (p *DefaultPaths) CreateConfigDirFor(relDirPath ConfigPath) (string, error) {
    74  	return CreateDefaultConfigDirFor(relDirPath)
    75  }
    76  
    77  // CreateDataPathFor builds the default path for a data file and creates
    78  // intermediate directories, if needed.
    79  func (p *DefaultPaths) CreateDataPathFor(relFilePath DataPath) (string, error) {
    80  	return CreateDefaultDataPathFor(relFilePath)
    81  }
    82  
    83  // CreateDataDirFor builds the default path for a data directory and creates
    84  // it, along with intermediate directories, if needed.
    85  func (p *DefaultPaths) CreateDataDirFor(relDirPath DataPath) (string, error) {
    86  	return CreateDefaultDataDirFor(relDirPath)
    87  }
    88  
    89  // CreateStatePathFor builds the default path for a state file and creates
    90  // intermediate directories, if needed.
    91  func (p *DefaultPaths) CreateStatePathFor(relFilePath StatePath) (string, error) {
    92  	return CreateDefaultStatePathFor(relFilePath)
    93  }
    94  
    95  // CreateStateDirFor builds the default path for a state directory and creates
    96  // it, along with intermediate directories, if needed.
    97  func (p *DefaultPaths) CreateStateDirFor(relDirPath StatePath) (string, error) {
    98  	return CreateDefaultStateDirFor(relDirPath)
    99  }
   100  
   101  // CachePathFor build the default path for a cache file or directory. It
   102  // doesn't create any resources.
   103  func (p *DefaultPaths) CachePathFor(relPath CachePath) string {
   104  	return DefaultCachePathFor(relPath)
   105  }
   106  
   107  // ConfigPathFor build the default path for a config file or directory. It
   108  // doesn't create any resources.
   109  func (p *DefaultPaths) ConfigPathFor(relPath ConfigPath) string {
   110  	return DefaultConfigPathFor(relPath)
   111  }
   112  
   113  // DataPathFor build the default path for a data file or directory. It
   114  // doesn't create any resources.
   115  func (p *DefaultPaths) DataPathFor(relPath DataPath) string {
   116  	return DefaultDataPathFor(relPath)
   117  }
   118  
   119  // StatePathFor build the default path for a state file or directory. It
   120  // doesn't create any resources.
   121  func (p *DefaultPaths) StatePathFor(relPath StatePath) string {
   122  	return DefaultStatePathFor(relPath)
   123  }
   124  
   125  // CreateDefaultCachePathFor builds the default path for a cache file and creates
   126  // intermediate directories, if needed.
   127  func CreateDefaultCachePathFor(relFilePath CachePath) (string, error) {
   128  	path, err := xdg.CacheFile(filepath.Join(VegaHome, relFilePath.String()))
   129  	if err != nil {
   130  		return "", fmt.Errorf("couldn't create the default directory for file: %w", err)
   131  	}
   132  	return path, nil
   133  }
   134  
   135  // CreateDefaultCacheDirFor builds the default path for a cache directory and creates
   136  // it, along with intermediate directories, if needed.
   137  func CreateDefaultCacheDirFor(relDirPath CachePath) (string, error) {
   138  	// We append fake-file to xdg library creates all directory up to fake-file.
   139  	path, err := xdg.CacheFile(filepath.Join(VegaHome, relDirPath.String(), "fake-file"))
   140  	if err != nil {
   141  		return "", fmt.Errorf("couldn't create the default directory: %w", err)
   142  	}
   143  	return filepath.Dir(path), nil
   144  }
   145  
   146  // CreateDefaultConfigPathFor builds the default path for a configuration file and
   147  // creates intermediate directories, if needed.
   148  func CreateDefaultConfigPathFor(relFilePath ConfigPath) (string, error) {
   149  	path, err := xdg.ConfigFile(filepath.Join(VegaHome, relFilePath.String()))
   150  	if err != nil {
   151  		return "", fmt.Errorf("couldn't create the default directory for file: %w", err)
   152  	}
   153  	return path, nil
   154  }
   155  
   156  // CreateDefaultConfigDirFor builds the default path for a config directory and creates
   157  // it, along with intermediate directories, if needed.
   158  func CreateDefaultConfigDirFor(relDirPath ConfigPath) (string, error) {
   159  	// We append fake-file to xdg library creates all directory up to fake-file.
   160  	path, err := xdg.ConfigFile(filepath.Join(VegaHome, relDirPath.String(), "fake-file"))
   161  	if err != nil {
   162  		return "", fmt.Errorf("couldn't create the default directory: %w", err)
   163  	}
   164  	return filepath.Dir(path), nil
   165  }
   166  
   167  // CreateDefaultDataPathFor builds the default path for a data file and creates
   168  // intermediate directories, if needed.
   169  func CreateDefaultDataPathFor(relFilePath DataPath) (string, error) {
   170  	path, err := xdg.DataFile(filepath.Join(VegaHome, relFilePath.String()))
   171  	if err != nil {
   172  		return "", fmt.Errorf("couldn't create the default directory for file: %w", err)
   173  	}
   174  	return path, nil
   175  }
   176  
   177  // CreateDefaultDataDirFor builds the default path for a data directory and creates
   178  // it, along with intermediate directories, if needed.
   179  func CreateDefaultDataDirFor(relDirPath DataPath) (string, error) {
   180  	// We append fake-file to xdg library creates all directory up to fake-file.
   181  	path, err := xdg.DataFile(filepath.Join(VegaHome, relDirPath.String(), "fake-file"))
   182  	if err != nil {
   183  		return "", fmt.Errorf("couldn't create the default directory: %w", err)
   184  	}
   185  	return filepath.Dir(path), nil
   186  }
   187  
   188  // CreateDefaultStatePathFor builds the default path for a state file and creates
   189  // intermediate directories, if needed.
   190  func CreateDefaultStatePathFor(relFilePath StatePath) (string, error) {
   191  	path, err := xdg.StateFile(filepath.Join(VegaHome, relFilePath.String()))
   192  	if err != nil {
   193  		return "", fmt.Errorf("couldn't create the default directory for file: %w", err)
   194  	}
   195  	return path, nil
   196  }
   197  
   198  // CreateDefaultStateDirFor builds the default path for a state directory and creates
   199  // it, along with intermediate directories, if needed.
   200  func CreateDefaultStateDirFor(relDirPath StatePath) (string, error) {
   201  	// We append fake-file to xdg library creates all directory up to fake-file.
   202  	path, err := xdg.StateFile(filepath.Join(VegaHome, relDirPath.String(), "fake-file"))
   203  	if err != nil {
   204  		return "", fmt.Errorf("couldn't create the default directory: %w", err)
   205  	}
   206  	return filepath.Dir(path), nil
   207  }
   208  
   209  // DefaultCachePathFor build the default path for a cache file or directory. It
   210  // doesn't create any resources.
   211  func DefaultCachePathFor(relPath CachePath) string {
   212  	return filepath.Join(xdg.CacheHome, VegaHome, relPath.String())
   213  }
   214  
   215  // DefaultConfigPathFor build the default path for a config file or directory.
   216  // It doesn't create any resources.
   217  func DefaultConfigPathFor(relPath ConfigPath) string {
   218  	return filepath.Join(xdg.ConfigHome, VegaHome, relPath.String())
   219  }
   220  
   221  // DefaultDataPathFor build the default path for a data file or directory. It
   222  // doesn't create any resources.
   223  func DefaultDataPathFor(relPath DataPath) string {
   224  	return filepath.Join(xdg.DataHome, VegaHome, relPath.String())
   225  }
   226  
   227  // DefaultStatePathFor build the default path for a state file or directory. It
   228  // doesn't create any resources.
   229  func DefaultStatePathFor(relPath StatePath) string {
   230  	return filepath.Join(xdg.StateHome, VegaHome, relPath.String())
   231  }