forked from rancher-sandbox/rancher-desktop-opensuse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.go
More file actions
37 lines (32 loc) · 934 Bytes
/
metadata.go
File metadata and controls
37 lines (32 loc) · 934 Bytes
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
29
30
31
32
33
34
35
36
37
package main
import (
"context"
"fmt"
"log/slog"
"os"
"github.com/goccy/go-yaml"
"golang.org/x/sys/unix"
)
const (
// Path to cloud-config style metadata file provided by Lima.
metadataPath = "/mnt/lima-cidata/meta-data"
)
// Load /mnt/lima-cidata/meta-data
func LoadMetadata(ctx context.Context) ([]string, error) {
var metaData struct {
LocalHostName string `yaml:"local-hostname"`
}
file, err := os.Open(metadataPath)
if err != nil {
return nil, fmt.Errorf("failed to load meta-data file: %w", err)
}
defer file.Close()
if err := yaml.NewDecoder(file).DecodeContext(ctx, &metaData); err != nil {
return nil, fmt.Errorf("failed to unmarshal meta-data file: %w", err)
}
slog.InfoContext(ctx, "setting host name", "hostname", metaData.LocalHostName)
if err := unix.Sethostname([]byte(metaData.LocalHostName)); err != nil {
return nil, fmt.Errorf("failed to set hostname: %w", err)
}
return nil, nil
}