package main import ( "bytes" "io/ioutil" "os" "github.com/yggdrasil-network/yggdrasil-go/src/config" ) func readConfig(useconf *bool, useconffile *string, normaliseconf *bool) *config.NodeConfig { // Use a configuration file. If -useconf, the configuration will be read // from stdin. If -useconffile, the configuration will be read from the // filesystem. var conf []byte var err error if *useconffile != "" { // Read the file from the filesystem conf, err = ioutil.ReadFile(*useconffile) } else { // Read the file from stdin. conf, err = ioutil.ReadAll(os.Stdin) } if err != nil { panic(err) } // If there's a byte order mark - which Windows 10 is now incredibly fond of // throwing everywhere when it's converting things into UTF-16 for the hell // of it - remove it and decode back down into UTF-8. This is necessary // because hjson doesn't know what to do with UTF-16 and will panic if bytes.Compare(conf[0:2], []byte{0xFF, 0xFE}) == 0 || bytes.Compare(conf[0:2], []byte{0xFE, 0xFF}) == 0 { utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM) decoder := utf.NewDecoder() conf, err = decoder.Bytes(conf) if err != nil { panic(err) } } // Generate blank configuration cfg := config.GenerateConfig() // ... and then update it with the supplied HJSON input if err := cfg.UnmarshalHJSON(conf); err != nil { panic(err) } return cfg }