use hostname.Get()

This commit is contained in:
Jeff Carr 2025-03-22 08:30:27 -05:00
parent b2ed410276
commit 88e87f5c1a
5 changed files with 17 additions and 87 deletions

View File

@ -1,53 +0,0 @@
// +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)
}

View File

@ -1,22 +0,0 @@
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
}

View File

@ -9,6 +9,7 @@ import (
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/hostname"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
@ -149,6 +150,8 @@ func (f *Forge) InitMachine() {
usr, _ := user.Current()
f.Config.Username = usr.Username
}
f.hostname, _ = hostname.Get()
// log.Info(hostname, err)
}
// only init's the protobuf. intended to not scan or change anything

View File

@ -9,17 +9,18 @@ import (
"time"
"github.com/google/uuid"
"go.wit.com/lib/hostname"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
func (f *Forge) newPatchset(name string) *Patchset {
func 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
pset.Hostname, _ = hostname.Get()
return pset
}

View File

@ -10,17 +10,18 @@ import (
// maybe an interface someday?
type Forge struct {
// one-time initialized data
initOnce sync.Once
initErr error // init error, if any
goSrc string // the path to go/src
configDir string // normally ~/.config/forge
goWork bool // means the user is currently using a go.work file
Config *ForgeConfigs // config repos for readonly, private, etc
Repos *gitpb.Repos // the repo protobufs
initOnce sync.Once
initErr error // init error, if any
goSrc string // the path to go/src
configDir string // normally ~/.config/forge
goWork bool // means the user is currently using a go.work file
Config *ForgeConfigs // config repos for readonly, private, etc
Repos *gitpb.Repos // the repo protobufs
configSave bool // if you need to save the config because things changed
hasFullScan bool // track last scan so it can be throttled
fullscan time.Time // time of the last scan so it can be throttled
hostname string // your hostname
// Machine *zoopb.Machine // things for virtigo to track vm's
configSave bool // if you need to save the config because things changed
hasFullScan bool // track last scan so it can be throttled
fullscan time.Time // time of the last scan so it can be throttled
}
func (f *Forge) GetGoSrc() string {