github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/fs/files/resolv_conf.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package files
     7  
     8  import (
     9  	"fmt"
    10  	"net"
    11  
    12  	"github.com/sylabs/singularity/internal/pkg/sylog"
    13  )
    14  
    15  // ResolvConf creates a resolv.conf content with provided dns list and returns it
    16  func ResolvConf(dns []string) (content []byte, err error) {
    17  	sylog.Verbosef("Creating resolv.conf content\n")
    18  	if len(dns) == 0 {
    19  		return content, fmt.Errorf("no dns ip provided")
    20  	}
    21  	for _, ip := range dns {
    22  		if net.ParseIP(ip) == nil {
    23  			return content, fmt.Errorf("dns ip %s is not a valid IP address", ip)
    24  		}
    25  		line := fmt.Sprintf("nameserver %s\n", ip)
    26  		content = append(content, line...)
    27  	}
    28  	return content, nil
    29  }