1
0
mirror of https://github.com/rancher/os.git synced 2025-08-29 11:34:42 +00:00
os/pkg/config/tftpget.go
2021-10-13 16:01:08 -07:00

42 lines
750 B
Go

package config
import (
"bytes"
"fmt"
"net"
"net/url"
"gopkg.in/pin/tftp.v2"
"sigs.k8s.io/yaml"
)
func tftpGet(tftpURL string) (map[string]interface{}, error) {
u, err := url.Parse(tftpURL)
if err != nil {
return nil, err
}
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host + ":69"
}
fmt.Printf("Downloading config from host %s, file %s\n", host, u.Path)
client, err := tftp.NewClient(host)
if err != nil {
return nil, err
}
writerTo, err := client.Receive(u.Path, "octet")
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
if _, err := writerTo.WriteTo(buf); err != nil {
return nil, err
}
result := map[string]interface{}{}
return result, yaml.Unmarshal(buf.Bytes(), &result)
}