From b7b18626d87dd42d5e5fe4fac8ff66eeb9836ee9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 15 Feb 2025 12:15:10 -0600 Subject: [PATCH] more on Machine.Init() --- http.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ init.go | 22 ++++++++++++++++++++++ patchset.Get.go | 45 --------------------------------------------- 3 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 http.go diff --git a/http.go b/http.go new file mode 100644 index 0000000..df9805e --- /dev/null +++ b/http.go @@ -0,0 +1,47 @@ +// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0 + +package forgepb + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" + "os/user" + + "go.wit.com/log" +) + +func (f *Forge) HttpPost(url string, data []byte) ([]byte, error) { + var err error + var req *http.Request + + req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) + log.Info("httpPost() with len", len(data), "url", url) + + usr, _ := user.Current() + req.Header.Set("author", usr.Username) + if f.Machine == nil { + // run f.InitMachine() here? + log.Info("you must run f.InitMachine()") + return nil, fmt.Errorf("you must run f.InitMachine()") + } + req.Header.Set("hostname", f.Machine.Hostname) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Error(err) + return []byte("client.Do(req) error"), err + } + defer resp.Body.Close() + log.Info("httpPost() with len", len(data)) + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Error(err) + return body, err + } + + return body, nil +} diff --git a/init.go b/init.go index 94c3cf0..2da1427 100644 --- a/init.go +++ b/init.go @@ -6,6 +6,7 @@ import ( "os" "os/user" "path/filepath" + "strings" "time" "go.wit.com/lib/gui/shell" @@ -133,6 +134,27 @@ func (f *Forge) InitPB() { } } +func (f *Forge) InitMachine() { + f.Machine = new(zoopb.Machine) + if err := f.Machine.ConfigLoad(); err != nil { + log.Log(WARN, "zoopb.ConfigLoad() failed", err) + f.Machine.InitWit() + } + + if f.Config.Username == "" { + usr, _ := user.Current() + f.Config.Username = usr.Username + } + + if f.Machine.Hostname == "" { + r, err := shell.RunVerbose([]string{"hostname", "-f"}) + if err == nil { + tmp := strings.Join(r.Stdout, "\n") + f.Machine.Hostname = strings.TrimSpace(tmp) + } + } +} + // only init's the protobuf. intended to not scan or change anything func InitPB() *Forge { f := DetermineGoPath() diff --git a/patchset.Get.go b/patchset.Get.go index 963618c..fcd59eb 100644 --- a/patchset.Get.go +++ b/patchset.Get.go @@ -3,12 +3,6 @@ package forgepb import ( - "bytes" - "io/ioutil" - "net/http" - "os" - "os/user" - "go.wit.com/log" ) @@ -38,42 +32,3 @@ func (f *Forge) GetPatchesets() (*Patchsets, error) { */ return psets, nil } - -func (f *Forge) HttpPost(url string, data []byte) ([]byte, error) { - var err error - var req *http.Request - - // data := []byte("some junk") - // url := "https://go.wit.com/register/" - - req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) - log.Info("httpPost() with len", len(data), "url", url) - - usr, _ := user.Current() - req.Header.Set("author", usr.Username) - hostname, _ := os.Hostname() - req.Header.Set("hostname", hostname) - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - log.Error(err) - return []byte("client.Do(req) error"), err - } - defer resp.Body.Close() - log.Info("httpPost() with len", len(data)) - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - return body, err - } - - // test := strings.TrimSpace(string(body)) - // log.Info("go.wit.com returned body:", test) - // if test == "OK" { - // return body, nil - // } - - return body, nil -}