work on setting the hostname in the patchset

This commit is contained in:
Jeff Carr 2025-03-22 05:22:04 -05:00
parent 68127e4356
commit b2ed410276
7 changed files with 127 additions and 96 deletions

53
hostname.go Normal file
View File

@ -0,0 +1,53 @@
// +build linux
package main
import (
"fmt"
"os"
"syscall"
)
// scutil --get ComputerName
// getDomainName fetches the domain name using the getdomainname syscall.
func getDomainName() (string, error) {
var buf [256]byte
err := syscall.Getdomainname(buf[:])
if err != nil {
return "", fmt.Errorf("failed to get domain name: %w", err)
}
// Trim null bytes
n := 0
for ; n < len(buf); n++ {
if buf[n] == 0 {
break
}
}
return string(buf[:n]), nil
}
// GetFullHostname returns the hostname + domain name (if set).
func GetFullHostname() (string, error) {
host, err := os.Hostname()
if err != nil {
return "", fmt.Errorf("failed to get hostname: %w", err)
}
domain, err := getDomainName()
if err != nil || domain == "" {
return host, nil // fallback to short hostname
}
return fmt.Sprintf("%s.%s", host, domain), nil
}
func main() {
fqdn, err := GetFullHostname()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Hostname + Domain:", fqdn)
}

22
hostname_linux.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"fmt"
"os"
"syscall"
)
// GetFullHostname returns the hostname + domain name (if set).
func getFullHostname() (string, error) {
host, err := os.Hostname()
if err != nil {
return "", fmt.Errorf("failed to get hostname: %w", err)
}
domain, err := getDomainName()
if err != nil || domain == "" {
return host, nil // fallback to short hostname
}
return fmt.Sprintf("%s.%s", host, domain), nil
}

37
http.go
View File

@ -11,36 +11,6 @@ import (
"go.wit.com/log"
)
/*
func (f *Forge) HttpPostMachine(url string) ([]byte, error) {
if f.Machine == nil {
// run f.InitMachine() here?
log.Info("you must run f.InitMachine()")
return nil, fmt.Errorf("you must run f.InitMachine()")
}
if f.Machine.Hostname == "" {
log.Info("WTF. hostname is blank")
} else {
log.Info("GOOD. hostname is set to", f.Machine.Hostname)
}
log.Info("GOOD2. hostname is set to", f.Machine.Hostname)
msg, err := f.Machine.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
return nil, err
}
log.Info("GOOD3. hostname is set to", f.Machine.Hostname)
check := new(zoopb.Machine)
check.Unmarshal(msg)
if check == nil {
log.Info("WTF. check == nil")
}
log.Info("good? check.hostname =", check.Hostname)
return f.HttpPost(url, msg)
}
*/
func (f *Forge) HttpPost(url string, data []byte) ([]byte, error) {
var err error
var req *http.Request
@ -50,13 +20,6 @@ func (f *Forge) HttpPost(url string, data []byte) ([]byte, error) {
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", "fixme:hostname")
client := &http.Client{}

18
init.go
View File

@ -145,28 +145,10 @@ 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

View File

@ -14,13 +14,20 @@ import (
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
// creates a patchset
// works from the user branches against the devel branches
func (f *Forge) MakeDevelPatchSet(name string) (*Patchset, error) {
func (f *Forge) newPatchset(name string) *Patchset {
pset := new(Patchset)
pset.Name = name
pset.Ctime = timestamppb.New(time.Now())
pset.Uuid = uuid.New().String()
pset.Hostname = f.Machine.hostname
return pset
}
// creates a patchset
// works from the user branches against the devel branches
func (f *Forge) MakeDevelPatchSet(name string) (*Patchset, error) {
pset := newPatchset(name)
if os.Getenv("GIT_AUTHOR_NAME") == "" {
return nil, fmt.Errorf("GIT_AUTHOR_NAME not set")
} else {
@ -65,19 +72,9 @@ func (f *Forge) MakeDevelPatchSet(name string) (*Patchset, error) {
return pset, nil
}
func (f *Forge) SubmitDevelPatchSet(name string) (*Patchset, error) {
pset, err := f.MakeDevelPatchSet(name)
if err != nil {
return nil, err
}
if err := f.submitPatchset(pset); err != nil {
return nil, err
}
return pset, nil
}
/*
func (f *Forge) MakeMasterPatchSet() (*Patchset, error) {
pset := new(Patchset)
pset := newPatchset("masterBranchPS")
dir, err := os.MkdirTemp("", "forge")
if err != nil {
return nil, err
@ -107,6 +104,7 @@ func (f *Forge) MakeMasterPatchSet() (*Patchset, error) {
}
return pset, nil
}
*/
func (pset *Patchset) makePatchSetNew(repo *gitpb.Repo) error {
startBranch := pset.StartBranchName
@ -244,29 +242,3 @@ func onlyWalkDirs(pDir string) error {
})
return baderr
}
func (f *Forge) submitPatchset(pset *Patchset) error {
var url string
url = forgeURL + "patchset"
msg, err := pset.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
return err
}
log.Info("proto.Marshal() msg len", len(msg))
body, err := f.HttpPost(url, msg)
if err != nil {
log.Info("httpPost() failed:", err)
return err
}
test := strings.TrimSpace(string(body))
lines := strings.Split(test, "\n")
count := 0
for _, line := range lines {
log.Info("got back:", line)
count += 1
}
log.Info("Total patches:", count)
return nil
}

View File

@ -5,6 +5,7 @@ package forgepb
import (
"errors"
"strings"
"time"
"go.wit.com/log"
@ -25,3 +26,40 @@ func (f *Forge) SendPatchSet(pset *Patchset) error {
return errors.New("don't know how to send yet")
}
func (f *Forge) SubmitDevelPatchSet(name string) (*Patchset, error) {
pset, err := f.MakeDevelPatchSet(name)
if err != nil {
return nil, err
}
if err := f.submitPatchset(pset); err != nil {
return nil, err
}
return pset, nil
}
func (f *Forge) submitPatchset(pset *Patchset) error {
var url string
url = forgeURL + "patchset"
msg, err := pset.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
return err
}
log.Info("proto.Marshal() msg len", len(msg))
body, err := f.HttpPost(url, msg)
if err != nil {
log.Info("httpPost() failed:", err)
return err
}
test := strings.TrimSpace(string(body))
lines := strings.Split(test, "\n")
count := 0
for _, line := range lines {
log.Info("got back:", line)
count += 1
}
log.Info("Total patches:", count)
return nil
}

View File

@ -77,6 +77,7 @@ message Patchset { // `autogenpb:mars
string endBranchHash = 11; //
string state = 12; // the state of the patch
string uuid = 13; // `autogenpb:sort` `autogenpb:unique`
string hostname = 14; //
}
message Patchsets { // `autogenpb:marshal` `autogenpb:gui`