Browse code

Keep linebreaks and generalize code

Thijs Terlouw authored on 2013/08/21 22:48:39
Showing 1 changed files
... ...
@@ -772,7 +772,7 @@ func GetResolvConf() ([]byte, error) {
772 772
 // CheckLocalDns looks into the /etc/resolv.conf,
773 773
 // it returns true if there is a local nameserver or if there is no nameserver.
774 774
 func CheckLocalDns(resolvConf []byte) bool {
775
-	var parsedResolvConf = ParseResolvConf(resolvConf)
775
+	var parsedResolvConf = StripComments(resolvConf, []byte("#"))
776 776
 	if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
777 777
 		return true
778 778
 	}
... ...
@@ -787,20 +787,20 @@ func CheckLocalDns(resolvConf []byte) bool {
787 787
 	return false
788 788
 }
789 789
 
790
-// ParseResolvConf parses the resolv.conf file into lines and strips away comments.
791
-func ParseResolvConf(resolvConf []byte) []byte {
792
-	lines := bytes.Split(resolvConf, []byte("\n"))
793
-	var noCommentsResolvConf []byte
790
+// StripComments parses input into lines and strips away comments.
791
+func StripComments(input []byte, commentMarker []byte) []byte {
792
+	lines := bytes.Split(input, []byte("\n"))
793
+	var output []byte
794 794
 	for _, currentLine := range lines {
795
-		var cleanLine = bytes.TrimLeft(currentLine, " \t")
796
-		var commentIndex = bytes.Index(cleanLine, []byte("#"))
795
+		var commentIndex = bytes.Index(currentLine, commentMarker)
797 796
 		if ( commentIndex == -1 ) {
798
-			noCommentsResolvConf = append(noCommentsResolvConf, cleanLine...)
797
+			output = append(output, currentLine...)
799 798
 		} else {
800
-			noCommentsResolvConf = append(noCommentsResolvConf, cleanLine[:commentIndex]...)
799
+			output = append(output, currentLine[:commentIndex]...)
801 800
 		}
801
+		output = append(output, []byte("\n")...)
802 802
 	}
803
-	return noCommentsResolvConf
803
+	return output
804 804
 }
805 805
 
806 806
 func ParseHost(host string, port int, addr string) string {