parse DNSOptions

Signed-off-by: Haiyan Meng <hmeng@redhat.com>
This commit is contained in:
Haiyan Meng 2016-07-25 10:24:22 -04:00 committed by Mrunal Patel
parent f2a2b06e61
commit b98900eb55
3 changed files with 87 additions and 3 deletions

View file

@ -3,8 +3,11 @@ package server
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
)
func getGPRCVersion() (string, error) {
@ -24,3 +27,67 @@ func getGPRCVersion() (string, error) {
}
return out, nil
}
func copyFile(src, dest string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
return nil
}
func removeFile(path string) error {
if _, err := os.Stat(path); err == nil {
if err := os.Remove(path); err != nil {
return err
}
}
return nil
}
func parseDNSOptions(servers, searches []string, path string) error {
nServers := len(servers)
nSearches := len(searches)
if nServers == 0 && nSearches == 0 {
return copyFile("/etc/resolv.conf", path)
}
if nSearches > maxDNSSearches {
return fmt.Errorf("DNSOption.Searches has more than 6 domains")
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
if nSearches > 0 {
data := fmt.Sprintf("search %s\n", strings.Join(searches, " "))
_, err = f.Write([]byte(data))
if err != nil {
return err
}
}
if nServers > 0 {
data := fmt.Sprintf("nameserver %s\n", strings.Join(servers, "\nnameserver "))
_, err = f.Write([]byte(data))
if err != nil {
return err
}
}
return nil
}