more on Machine.Init()

This commit is contained in:
Jeff Carr 2025-02-15 12:15:10 -06:00
parent 7900b1416e
commit b7b18626d8
3 changed files with 69 additions and 45 deletions

47
http.go Normal file
View File

@ -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
}

22
init.go
View File

@ -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()

View File

@ -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
}