cloud-control-panel/config.go

253 lines
6.1 KiB
Go

package main
/*
This simply parses the command line arguments using the default golang
package called 'flag'. This can be used as a simple template to parse
command line arguments in other programs.
It puts everything in the 'config' package which I think is a good
wrapper around the 'flags' package and doesn't need a whole mess of
global variables
*/
import "log"
import "os"
import "os/user"
import "flag"
import "fmt"
import "runtime"
import "io/ioutil"
import "strings"
import "github.com/golang/protobuf/jsonpb"
import pb "git.wit.com/wit/witProtobuf"
import "github.com/davecgh/go-spew/spew"
// always override the debugging flag from the command line
var debugging *bool
var debugtable *bool
var config *pb.Config
// This loads the config file and marshals it into the
// config protocol buffer definition.
// Then it is very easy to pass all the config options
// around and the re-write that JSON file when the GUI
// exits
func loadConfigFile() {
// look up the user information
user, err := user.Current()
if err != nil {
onExit(err)
}
spew.Dump(user)
filename := ""
// TODO: figure out how to look for this file in the right
// place under Linux, MacOS and Windows
if runtime.GOOS == "linux" {
log.Println("OS: Linux")
filename = user.HomeDir + "/.config/cloud-control-panel.json"
} else if runtime.GOOS == "windows" {
log.Println("OS: Windows")
filename = user.HomeDir + "\\cloud-control-panel.json"
} else {
log.Println("OS: " + runtime.GOOS)
filename = user.HomeDir + "/.cloud-control-panel.json"
}
tmp := loadConfigFromFilename(filename)
config = &tmp
// os.Exit(1)
// junk := marshalEvent()
// spew.Dump(junk)
// config = unmarshalConfig(junk)
// config = unmarshalConfig(string(b))
config.Filename = filename
}
/* NEVER USE THIS
func unmarshalConfig(a string) *pb.Config {
var newpb *pb.Config
log.Println("ATTEMTED TO UNMARSHAL string =", a)
err := jsonpb.UnmarshalString(a, newpb)
log.Println("ATTEMTED TO UNMARSHAL err =", err)
spew.Dump(newpb)
return newpb
}
*/
var customUsage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Println("")
fmt.Println("EXAMPLES:")
fmt.Println("")
fmt.Println(os.Args[0] + " --hostname test.hostname.wit.com")
fmt.Println("")
}
func parseFlags() {
var hostname string
flag.StringVar (&hostname, "hostname", "localhost", "Your full hostname")
height := flag.Int ("height", 0, "Height of the Window")
width := flag.Int ("width", 0, "Width of the Window")
debugging = flag.Bool("debugging", false, "Enable debugging")
debugtable = flag.Bool("debugtable", false, "Enable GUI table debugging")
nogui := flag.Bool("nogui", false, "Do not display the GUI")
// Set the output if something fails to stdout rather than stderr
flag.CommandLine.SetOutput(os.Stdout)
flag.Usage = customUsage
flag.Parse()
if flag.Parsed() {
log.Println("flag.Parse() worked")
} else {
log.Println("flag.Parse() failed")
onExit(nil)
}
if (*width > 100) {
log.Println("ENABLE width =", int32(*width))
config.Width = int32(*width)
}
if (*height > 100) {
log.Println("ENABLE height =", int32(*height))
config.Height = int32(*height)
}
log.Println("ENABLE nogui =", *nogui)
log.Println("ENABLE debugging =", *debugging)
if (*debugging) {
log.Println("ENABLE width =", *width)
log.Println("ENABLE debugging =", *debugging)
} else {
log.Println("DISABLE DEBUG debugging =", *debugging)
}
if (hostname == "") {
config.Hostname = hostname
}
config.Debugging = *debugging
config.Debugtable = *debugtable
log.Println("config.Width", config.Width)
log.Println("config.Debugging", config.Debugging)
}
func parseConfig() {
// first load the config file
loadConfigFile()
// override the config file from the command line
parseFlags()
log.Println("config.width", config.Width)
log.Println("config.height", config.Height)
log.Println("config.debugging", config.Debugging)
// use this to restore the default config
// defaultConfig, _ := packrBox.FindString("cloud.json")
// config = pb.MakeDefaultConfig()
// spew.Dump(config)
}
func marshalEvent() string {
// This is how you marshal into JSON from a Protobuf
c := pb.MakeDefaultConfig()
marshaler := &jsonpb.Marshaler{}
stuff, _ := marshaler.MarshalToString(c)
log.Println(stuff)
// also:
// var j *bytes.Buffer
// marshaler.Marshal(j, event)
// w.Write(j.Bytes())
return stuff
}
func writeToFile(filename string, a string) {
f, _ := os.Create(filename)
f.WriteString(a)
f.Close()
}
func saveConfig() {
filename := config.Filename
if (filename == "") {
log.Println("NOT SAVING CONFIG FILE")
} else {
marshaler := &jsonpb.Marshaler{}
stuff, _ := marshaler.MarshalToString(config)
log.Println(stuff)
writeToFile(filename, stuff)
/*
f, err := os.Create(filename + ".yaml")
if err == nil {
// DumpTo(f, "yaml")
}
f, err = os.Create(filename)
if err == nil {
// DumpTo(f, "json")
}
*/
}
}
// will load the default config from within the go binary
func loadConfigFromFilename(filename string) pb.Config {
sets := pb.Config{}
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Println("ioutil.ReadFile() ERROR =", err)
// This probably means this is the first time the user is opening this
sets.Errors += 1
sets.Crash = "err"
sets.Counter = 1
sets.Width = 500
sets.Height = 500
return sets
}
log.Println("ioutil.ReadFile() b =", b)
err = jsonpb.Unmarshal(strings.NewReader(string(b)), &sets)
if err != nil {
log.Println("jsonpb.Unmarshal() ERROR =", err)
}
spew.Dump(sets)
return sets
}
// will load the default config from within the go binary
func loadDefaultConfig() pb.Config {
log.Println("TRY TO LOAD DEFAULT CONFIG")
// defaultConfig, _ := packrBox.FindString("protobuf-config.json")
b, err := packrBox.FindString("protobuf-small.json")
log.Println("b =", b)
log.Println("err =", err)
// var newpb *pb.Config
sets := pb.Config{}
err = jsonpb.Unmarshal(strings.NewReader(string(b)), &sets)
if err != nil {
log.Println("jsonpb.Unmarshal() ERROR =", err)
}
spew.Dump(sets)
// os.Exit(0)
return sets
}