From e1b30b97cc9dae2e07d44a87ec63fa1169566ed4 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 5 Jun 2019 18:03:10 -0700 Subject: [PATCH] working on an auto-update methodology Signed-off-by: Jeff Carr --- config.go | 23 +++++++++++++++++++ wget.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 wget.go diff --git a/config.go b/config.go index 6c5a1c9..af65b8d 100644 --- a/config.go +++ b/config.go @@ -19,6 +19,7 @@ import "runtime" import "io/ioutil" import "strings" import "reflect" +import "bytes" import "github.com/golang/protobuf/jsonpb" import pb "git.wit.com/wit/witProtobuf" @@ -251,12 +252,34 @@ func parseConfig() { config.Dirty = true } + buf, _ := wget("https://mirrors.wit.com/cloud/control-panel/VERSION") + upstream := chompBytesBuffer(buf) + + _, epoch, _ := shell.Run("git log -1 --format=%at v" + version) + // epoch = chomp(epoch) + + if (! config.Dirty) { + // See if a newer version of this control panel exists + + // if (epoch > time.Date().Unix()) + } + + log.Println("epoch =", epoch) + log.Println("version =", version) + log.Println("upstream =", upstream) log.Println("tagref =", tagref) log.Println("config.Gitref =", config.Gitref) log.Println("config.Goversion =", config.Goversion) log.Println("config.Dirty =", config.Dirty) } +func chompBytesBuffer(buf *bytes.Buffer) string { + var bytesSplice []byte + bytesSplice = buf.Bytes() + + return chomp(string(bytesSplice)) +} + // TODO: fix this to chomp \n \r NULL \t and ' ' func chomp(s string) string { // var bytesBuffer bytes.Buffer diff --git a/wget.go b/wget.go new file mode 100644 index 0000000..c0ca656 --- /dev/null +++ b/wget.go @@ -0,0 +1,68 @@ +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 a 'config' Protobuf which I think is a good + wrapper around the 'flags' package and doesn't need a whole mess of + global variables +*/ + +import "os" +import "net/http" +import "bytes" +import "io" + +/* +import "log" +import "os/user" +import "flag" +import "fmt" +import "runtime" +import "strings" +import "reflect" + +import "github.com/golang/protobuf/jsonpb" +import pb "git.wit.com/wit/witProtobuf" +import "git.wit.com/wit/shell" + +import "github.com/davecgh/go-spew/spew" +*/ + +func wget(url string) (*bytes.Buffer, error) { + buf := new(bytes.Buffer) + + // Get the data + resp, err := http.Get(url) + if err != nil { + return buf, err + } + defer resp.Body.Close() + + buf.ReadFrom(resp.Body) + // newStr := buf.String() + // tmp := string(resp.Body) + return buf, err +} + +func wgetToFile(filepath string, url string) error { + // Get the data + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + // Create the file + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + + // Write the body to file + _, err = io.Copy(out, resp.Body) + return err +}