code.vegaprotocol.io/vega@v0.79.0/blockexplorer/config/config.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 config 17 18 import ( 19 "fmt" 20 21 "code.vegaprotocol.io/vega/blockexplorer/api" 22 "code.vegaprotocol.io/vega/blockexplorer/store" 23 vgfs "code.vegaprotocol.io/vega/libs/fs" 24 "code.vegaprotocol.io/vega/logging" 25 "code.vegaprotocol.io/vega/paths" 26 ) 27 28 type VegaHomeFlag struct { 29 VegaHome string `description:"Path to the custom home for vega" long:"home"` 30 } 31 32 type Config struct { 33 API api.Config 34 Store store.Config 35 Logging logging.Config `group:"logging" namespace:"logging"` 36 } 37 38 func NewDefaultConfig() Config { 39 return Config{ 40 API: api.NewDefaultConfig(), 41 Store: store.NewDefaultConfig(), 42 Logging: logging.NewDefaultConfig(), 43 } 44 } 45 46 type Loader struct { 47 configFilePath string 48 } 49 50 func NewLoader(vegaPaths paths.Paths) (*Loader, error) { 51 configFilePath, err := vegaPaths.CreateConfigPathFor(paths.BlockExplorerDefaultConfigFile) 52 if err != nil { 53 return nil, fmt.Errorf("couldn't get path for %s: %w", paths.NodeDefaultConfigFile, err) 54 } 55 56 return &Loader{ 57 configFilePath: configFilePath, 58 }, nil 59 } 60 61 func (l *Loader) ConfigFilePath() string { 62 return l.configFilePath 63 } 64 65 func (l *Loader) ConfigExists() (bool, error) { 66 exists, err := vgfs.FileExists(l.configFilePath) 67 if err != nil { 68 return false, err 69 } 70 return exists, nil 71 } 72 73 func (l *Loader) Save(cfg *Config) error { 74 if err := paths.WriteStructuredFile(l.configFilePath, cfg); err != nil { 75 return fmt.Errorf("couldn't write configuration file at %s: %w", l.configFilePath, err) 76 } 77 return nil 78 } 79 80 func (l *Loader) Get() (*Config, error) { 81 cfg := NewDefaultConfig() 82 if err := paths.ReadStructuredFile(l.configFilePath, &cfg); err != nil { 83 return nil, fmt.Errorf("couldn't read configuration file at %s: %w", l.configFilePath, err) 84 } 85 return &cfg, nil 86 }