github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ui/versionmanager/version_config.go (about) 1 /* 2 * Copyright (C) 2021 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 versionmanager 19 20 import ( 21 "encoding/json" 22 "os" 23 "path/filepath" 24 ) 25 26 const ( 27 fileName = "which.json" 28 29 // BundledVersionName bundled node UI version name 30 BundledVersionName = "bundled" 31 ) 32 33 // NodeUIVersionConfig interface 34 type NodeUIVersionConfig interface { 35 Version() (string, error) 36 UIBuildPath(versionName string) string 37 uiDir() string 38 uiDistPath(versionName string) string 39 uiDistFile(versionName string) string 40 write(w nodeUIVersion) error 41 } 42 43 // VersionConfig ui version config 44 type VersionConfig struct { 45 nodeUIDir string 46 } 47 48 // NewVersionConfig constructor for VersionConfig 49 func NewVersionConfig(nodeUIDir string) (*VersionConfig, error) { 50 err := os.MkdirAll(nodeUIDir, 0700) 51 if err != nil { 52 return nil, err 53 } 54 55 return &VersionConfig{nodeUIDir: nodeUIDir}, nil 56 } 57 58 // Version returns version to be used 59 func (vm *VersionConfig) Version() (string, error) { 60 exists, err := vm.exists() 61 62 if err != nil { 63 return BundledVersionName, err 64 } 65 66 if !exists { 67 return BundledVersionName, nil 68 } 69 70 w, err := vm.read() 71 return w.VersionName, err 72 } 73 74 func (vm *VersionConfig) exists() (bool, error) { 75 _, err := os.Stat(vm.whichFilePath()) 76 77 if err != nil && os.IsPermission(err) { 78 return false, err 79 } 80 81 if err != nil && os.IsNotExist(err) { 82 return false, nil 83 } 84 85 if err != nil { 86 return false, err 87 } 88 89 return true, nil 90 } 91 92 func (vm *VersionConfig) read() (nodeUIVersion, error) { 93 data, err := os.ReadFile(vm.whichFilePath()) 94 if err != nil { 95 return nodeUIVersion{}, err 96 } 97 98 var w nodeUIVersion 99 err = json.Unmarshal(data, &w) 100 if err != nil { 101 return nodeUIVersion{}, err 102 } 103 104 return w, nil 105 } 106 107 func (vm *VersionConfig) whichFilePath() string { 108 return filepath.Join(vm.nodeUIDir, fileName) 109 } 110 111 func (vm *VersionConfig) uiDistPath(versionName string) string { 112 return filepath.Join(vm.nodeUIDir, versionName) 113 } 114 115 // UIBuildPath build path to the assets of provided versionName 116 func (vm *VersionConfig) UIBuildPath(versionName string) string { 117 return filepath.Join(vm.nodeUIDir, versionName, "build") 118 } 119 120 func (vm *VersionConfig) uiDistFile(versionName string) string { 121 return filepath.Join(vm.uiDistPath(versionName), nodeUIAssetName) 122 } 123 124 func (vm *VersionConfig) write(w nodeUIVersion) error { 125 configJSON, err := json.Marshal(w) 126 if err != nil { 127 return err 128 } 129 130 return os.WriteFile(vm.whichFilePath(), configJSON, 0644) 131 } 132 133 func (vm *VersionConfig) uiDir() string { 134 return vm.nodeUIDir 135 } 136 137 type nodeUIVersion struct { 138 VersionName string `json:"version_name"` 139 }