code.vegaprotocol.io/vega@v0.79.0/paths/custom.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  	vgfs "code.vegaprotocol.io/vega/libs/fs"
    23  )
    24  
    25  // When opting for a custom Vega home, the all files are located under the
    26  // specified folder. They are sorted, by purpose, in sub-folders. The structure
    27  // of these sub-folder is described in paths.go.
    28  //
    29  // File structure for custom home:
    30  //
    31  // VEGA_HOME
    32  // 	├── cache/
    33  // 	├── config/
    34  // 	├── data/
    35  // 	└── state/
    36  
    37  type CustomPaths struct {
    38  	CustomHome string
    39  }
    40  
    41  // CreateCacheDirFor builds the path for cache files at the configured home and
    42  // creates intermediate directories.
    43  func (p *CustomPaths) CreateCacheDirFor(relDirPath CachePath) (string, error) {
    44  	return CreateCustomCacheDirFor(p.CustomHome, relDirPath)
    45  }
    46  
    47  // CreateCachePathFor builds the path for cache directories at the configured home
    48  // and creates intermediate directories.
    49  func (p *CustomPaths) CreateCachePathFor(relFilePath CachePath) (string, error) {
    50  	return CreateCustomCachePathFor(p.CustomHome, relFilePath)
    51  }
    52  
    53  // CreateConfigDirFor builds the path for configuration files at a given configured
    54  // home and creates intermediate directories.
    55  func (p *CustomPaths) CreateConfigDirFor(relDirPath ConfigPath) (string, error) {
    56  	return CreateCustomConfigDirFor(p.CustomHome, relDirPath)
    57  }
    58  
    59  // CreateConfigPathFor builds the path for config directories at the configured
    60  // home and creates intermediate directories.
    61  func (p *CustomPaths) CreateConfigPathFor(relFilePath ConfigPath) (string, error) {
    62  	return CreateCustomConfigPathFor(p.CustomHome, relFilePath)
    63  }
    64  
    65  // CreateDataDirFor builds the path for data files at the configured home and
    66  // creates intermediate directories.
    67  func (p *CustomPaths) CreateDataDirFor(relDirPath DataPath) (string, error) {
    68  	return CreateCustomDataDirFor(p.CustomHome, relDirPath)
    69  }
    70  
    71  // CreateDataPathFor builds the path for data directories at the configured home
    72  // and creates intermediate directories.
    73  func (p *CustomPaths) CreateDataPathFor(relFilePath DataPath) (string, error) {
    74  	return CreateCustomDataPathFor(p.CustomHome, relFilePath)
    75  }
    76  
    77  // CreateStateDirFor builds the path for cache files at the configured home and
    78  // creates intermediate directories.
    79  func (p *CustomPaths) CreateStateDirFor(relDirPath StatePath) (string, error) {
    80  	return CreateCustomStateDirFor(p.CustomHome, relDirPath)
    81  }
    82  
    83  // CreateStatePathFor builds the path for data directories at the configured home
    84  // and creates intermediate directories.
    85  func (p *CustomPaths) CreateStatePathFor(relFilePath StatePath) (string, error) {
    86  	return CreateCustomStatePathFor(p.CustomHome, relFilePath)
    87  }
    88  
    89  // CachePathFor builds the path for a cache file or directories at the
    90  // configured home. It doesn't create any resources.
    91  func (p *CustomPaths) CachePathFor(relPath CachePath) string {
    92  	return CustomCachePathFor(p.CustomHome, relPath)
    93  }
    94  
    95  // ConfigPathFor builds the path for a config file or directories at the
    96  // configured home. It doesn't create any resources.
    97  func (p *CustomPaths) ConfigPathFor(relPath ConfigPath) string {
    98  	return CustomConfigPathFor(p.CustomHome, relPath)
    99  }
   100  
   101  // DataPathFor builds the path for a data file or directories at the configured
   102  // home. It doesn't create any resources.
   103  func (p *CustomPaths) DataPathFor(relPath DataPath) string {
   104  	return CustomDataPathFor(p.CustomHome, relPath)
   105  }
   106  
   107  // StatePathFor builds the path for a state file or directories at the
   108  // configured home. It doesn't create any resources.
   109  func (p *CustomPaths) StatePathFor(relPath StatePath) string {
   110  	return CustomStatePathFor(p.CustomHome, relPath)
   111  }
   112  
   113  // CreateCustomCachePathFor builds the path for cache files at a given root path and
   114  // creates intermediate directories. It scoped the files under a "cache" folder,
   115  // and follow the default structure.
   116  func CreateCustomCachePathFor(customHome string, relFilePath CachePath) (string, error) {
   117  	fullPath := CustomCachePathFor(customHome, relFilePath)
   118  	dir := filepath.Dir(fullPath)
   119  	if err := vgfs.EnsureDir(dir); err != nil {
   120  		return "", fmt.Errorf("couldn't create directories for file: %w", err)
   121  	}
   122  	return fullPath, nil
   123  }
   124  
   125  // CreateCustomCacheDirFor builds the path for cache directories at a given root path
   126  // and creates intermediate directories. It scoped the files under a "data"
   127  // folder, and follow the default structure.
   128  func CreateCustomCacheDirFor(customHome string, relDirPath CachePath) (string, error) {
   129  	path := CustomCachePathFor(customHome, relDirPath)
   130  	if err := vgfs.EnsureDir(path); err != nil {
   131  		return "", fmt.Errorf("couldn't create directories: %w", err)
   132  	}
   133  	return path, nil
   134  }
   135  
   136  // CreateCustomConfigPathFor builds the path for configuration files at a given root
   137  // path and creates intermediate directories. It scoped the files under a
   138  // "config" folder, and follow the default structure.
   139  func CreateCustomConfigPathFor(customHome string, relFilePath ConfigPath) (string, error) {
   140  	fullPath := CustomConfigPathFor(customHome, relFilePath)
   141  	dir := filepath.Dir(fullPath)
   142  	if err := vgfs.EnsureDir(dir); err != nil {
   143  		return "", fmt.Errorf("couldn't create directories for file: %w", err)
   144  	}
   145  	return fullPath, nil
   146  }
   147  
   148  // CreateCustomConfigDirFor builds the path for config directories at a given root path
   149  // and creates intermediate directories. It scoped the files under a "data"
   150  // folder, and follow the default structure.
   151  func CreateCustomConfigDirFor(customHome string, relDirPath ConfigPath) (string, error) {
   152  	path := CustomConfigPathFor(customHome, relDirPath)
   153  	if err := vgfs.EnsureDir(path); err != nil {
   154  		return "", fmt.Errorf("couldn't create directories: %w", err)
   155  	}
   156  	return path, nil
   157  }
   158  
   159  // CreateCustomDataPathFor builds the path for data files at a given root path and
   160  // creates intermediate directories. It scoped the files under a "data" folder,
   161  // and follow the default structure.
   162  func CreateCustomDataPathFor(customHome string, relFilePath DataPath) (string, error) {
   163  	fullPath := CustomDataPathFor(customHome, relFilePath)
   164  	dir := filepath.Dir(fullPath)
   165  	if err := vgfs.EnsureDir(dir); err != nil {
   166  		return "", fmt.Errorf("couldn't create directories for file: %w", err)
   167  	}
   168  	return fullPath, nil
   169  }
   170  
   171  // CreateCustomDataDirFor builds the path for data directories at a given root path
   172  // and creates intermediate directories. It scoped the files under a "data"
   173  // folder, and follow the default structure.
   174  func CreateCustomDataDirFor(customHome string, relDirPath DataPath) (string, error) {
   175  	path := CustomDataPathFor(customHome, relDirPath)
   176  	if err := vgfs.EnsureDir(path); err != nil {
   177  		return "", fmt.Errorf("couldn't create directories: %w", err)
   178  	}
   179  	return path, nil
   180  }
   181  
   182  // CreateCustomStatePathFor builds the path for cache files at a given root path and
   183  // creates intermediate directories. It scoped the files under a "cache" folder,
   184  // and follow the default structure.
   185  func CreateCustomStatePathFor(customHome string, relFilePath StatePath) (string, error) {
   186  	fullPath := CustomStatePathFor(customHome, relFilePath)
   187  	dir := filepath.Dir(fullPath)
   188  	if err := vgfs.EnsureDir(dir); err != nil {
   189  		return "", fmt.Errorf("couldn't create directories for file: %w", err)
   190  	}
   191  	return fullPath, nil
   192  }
   193  
   194  // CreateCustomStateDirFor builds the path for data directories at a given root path
   195  // and creates intermediate directories. It scoped the files under a "data"
   196  // folder, and follow the default structure.
   197  func CreateCustomStateDirFor(customHome string, relDirPath StatePath) (string, error) {
   198  	path := CustomStatePathFor(customHome, relDirPath)
   199  	if err := vgfs.EnsureDir(path); err != nil {
   200  		return "", fmt.Errorf("couldn't create directories: %w", err)
   201  	}
   202  	return path, nil
   203  }
   204  
   205  // CustomCachePathFor builds the path for a cache file or directories at a given
   206  // root path. It doesn't create any resources.
   207  func CustomCachePathFor(customHome string, relPath CachePath) string {
   208  	return filepath.Join(customHome, "cache", relPath.String())
   209  }
   210  
   211  // CustomConfigPathFor builds the path for a config file or directories at a given
   212  // root path. It doesn't create any resources.
   213  func CustomConfigPathFor(customHome string, relPath ConfigPath) string {
   214  	return filepath.Join(customHome, "config", relPath.String())
   215  }
   216  
   217  // CustomDataPathFor builds the path for a data file or directories at a given
   218  // root path. It doesn't create any resources.
   219  func CustomDataPathFor(customHome string, relPath DataPath) string {
   220  	return filepath.Join(customHome, "data", relPath.String())
   221  }
   222  
   223  // CustomStatePathFor builds the path for a state file or directories at a given
   224  // root path. It doesn't create any resources.
   225  func CustomStatePathFor(customHome string, relPath StatePath) string {
   226  	return filepath.Join(customHome, "state", relPath.String())
   227  }