github.com/core-coin/go-core/v2@v2.1.9/cmd/gocore/config.go (about) 1 // Copyright 2023 by the Authors 2 // This file is part of go-core. 3 // 4 // go-core 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 // go-core 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 go-core. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "bufio" 21 "errors" 22 "fmt" 23 "os" 24 "reflect" 25 "unicode" 26 27 "github.com/naoina/toml" 28 "gopkg.in/urfave/cli.v1" 29 30 "github.com/core-coin/go-core/v2/cmd/utils" 31 "github.com/core-coin/go-core/v2/internal/xcbapi" 32 "github.com/core-coin/go-core/v2/node" 33 "github.com/core-coin/go-core/v2/params" 34 "github.com/core-coin/go-core/v2/xcb" 35 ) 36 37 var ( 38 dumpConfigCommand = cli.Command{ 39 Action: utils.MigrateFlags(dumpConfig), 40 Name: "dumpconfig", 41 Usage: "Show configuration values", 42 ArgsUsage: "", 43 Flags: append(nodeFlags, rpcFlags...), 44 Category: "MISCELLANEOUS COMMANDS", 45 Description: `The dumpconfig command shows configuration values.`, 46 } 47 48 configFileFlag = cli.StringFlag{ 49 Name: "config", 50 Usage: "TOML configuration file", 51 } 52 ) 53 54 // These settings ensure that TOML keys use the same names as Go struct fields. 55 var tomlSettings = toml.Config{ 56 NormFieldName: func(rt reflect.Type, key string) string { 57 return key 58 }, 59 FieldToKey: func(rt reflect.Type, field string) string { 60 return field 61 }, 62 MissingField: func(rt reflect.Type, field string) error { 63 link := "" 64 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 65 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 66 } 67 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 68 }, 69 } 70 71 type xcbstatsConfig struct { 72 URL string `toml:",omitempty"` 73 } 74 75 type gocoreConfig struct { 76 Xcb xcb.Config 77 Node node.Config 78 Xcbstats xcbstatsConfig 79 } 80 81 func loadConfig(file string, cfg *gocoreConfig) error { 82 f, err := os.Open(file) 83 if err != nil { 84 return err 85 } 86 defer f.Close() 87 88 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 89 // Add file name to errors that have a line number. 90 if _, ok := err.(*toml.LineError); ok { 91 err = errors.New(file + ", " + err.Error()) 92 } 93 return err 94 } 95 96 func defaultNodeConfig() node.Config { 97 cfg := node.DefaultConfig 98 cfg.Name = clientIdentifier 99 cfg.Version = params.VersionWithTag(gitTag, gitCommit, gitDate) 100 cfg.HTTPModules = append(cfg.HTTPModules, "xcb") 101 cfg.WSModules = append(cfg.WSModules, "xcb") 102 cfg.IPCPath = "gocore.ipc" 103 return cfg 104 } 105 106 // makeConfigNode loads gocore configuration and creates a blank node instance. 107 func makeConfigNode(ctx *cli.Context) (*node.Node, gocoreConfig) { 108 // Load defaults. 109 cfg := gocoreConfig{ 110 Xcb: xcb.DefaultConfig, 111 Node: defaultNodeConfig(), 112 } 113 114 // Load config file. 115 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 116 if err := loadConfig(file, &cfg); err != nil { 117 utils.Fatalf("%v", err) 118 } 119 } 120 121 // Apply flags. 122 utils.SetNodeConfig(ctx, &cfg.Node) 123 124 // Set default network id from --networkId, --devin flags 125 utils.SetDefaultNetworkID(ctx) 126 127 stack, err := node.New(&cfg.Node) 128 if err != nil { 129 utils.Fatalf("Failed to create the protocol stack: %v", err) 130 } 131 utils.SetXcbConfig(ctx, stack, &cfg.Xcb) 132 if ctx.GlobalIsSet(utils.XcbStatsURLFlag.Name) { 133 cfg.Xcbstats.URL = ctx.GlobalString(utils.XcbStatsURLFlag.Name) 134 } 135 136 return stack, cfg 137 } 138 139 // makeFullNode loads gocore configuration and creates the Core backend. 140 func makeFullNode(ctx *cli.Context) (*node.Node, xcbapi.Backend) { 141 stack, cfg := makeConfigNode(ctx) 142 143 backend := utils.RegisterXcbService(stack, &cfg.Xcb) 144 145 // Configure GraphQL if requested 146 if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { 147 utils.RegisterGraphQLService(stack, backend, cfg.Node) 148 } 149 // Add the Core Stats daemon if requested. 150 if cfg.Xcbstats.URL != "" { 151 utils.RegisterXcbStatsService(stack, backend, cfg.Xcbstats.URL) 152 } 153 return stack, backend 154 } 155 156 // dumpConfig is the dumpconfig command. 157 func dumpConfig(ctx *cli.Context) error { 158 _, cfg := makeConfigNode(ctx) 159 comment := "" 160 161 if cfg.Xcb.Genesis != nil { 162 cfg.Xcb.Genesis = nil 163 comment += "# Note: this config doesn't contain the genesis block.\n\n" 164 } 165 166 out, err := tomlSettings.Marshal(&cfg) 167 if err != nil { 168 return err 169 } 170 171 dump := os.Stdout 172 if ctx.NArg() > 0 { 173 dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 174 if err != nil { 175 return err 176 } 177 defer dump.Close() 178 } 179 dump.WriteString(comment) 180 dump.Write(out) 181 182 return nil 183 }