blob: 11157c5c0dad2769dd8b808f7f04f67aa99bff79 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package hostname
import (
"bytes"
"fmt"
"os/exec"
)
// scutil --get HostName
// scutil --get LocalHostName
// scutil --get ComputerName
// scutil --set HostName my-mac.example.com
func osGetHostname() (string, error) {
return scutil([]string{"-get", "HostName"})
}
// getDomainName executes the 'domainname' command and returns its output.
func scutil(args []string) (string, error) {
cmd := exec.Command("scutil", args...)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to execute 'scutil': %w", err)
}
domain := out.String()
return domain, nil
}
|