Ver código fonte

Keep linebreaks and generalize code

Thijs Terlouw 12 anos atrás
pai
commit
c349b4d56c
1 arquivos alterados com 10 adições e 10 exclusões
  1. 10 10
      utils/utils.go

+ 10 - 10
utils/utils.go

@@ -772,7 +772,7 @@ func GetResolvConf() ([]byte, error) {
 // CheckLocalDns looks into the /etc/resolv.conf,
 // CheckLocalDns looks into the /etc/resolv.conf,
 // it returns true if there is a local nameserver or if there is no nameserver.
 // it returns true if there is a local nameserver or if there is no nameserver.
 func CheckLocalDns(resolvConf []byte) bool {
 func CheckLocalDns(resolvConf []byte) bool {
-	var parsedResolvConf = ParseResolvConf(resolvConf)
+	var parsedResolvConf = StripComments(resolvConf, []byte("#"))
 	if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
 	if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
 		return true
 		return true
 	}
 	}
@@ -787,20 +787,20 @@ func CheckLocalDns(resolvConf []byte) bool {
 	return false
 	return false
 }
 }
 
 
-// ParseResolvConf parses the resolv.conf file into lines and strips away comments.
-func ParseResolvConf(resolvConf []byte) []byte {
-	lines := bytes.Split(resolvConf, []byte("\n"))
-	var noCommentsResolvConf []byte
+// StripComments parses input into lines and strips away comments.
+func StripComments(input []byte, commentMarker []byte) []byte {
+	lines := bytes.Split(input, []byte("\n"))
+	var output []byte
 	for _, currentLine := range lines {
 	for _, currentLine := range lines {
-		var cleanLine = bytes.TrimLeft(currentLine, " \t")
-		var commentIndex = bytes.Index(cleanLine, []byte("#"))
+		var commentIndex = bytes.Index(currentLine, commentMarker)
 		if ( commentIndex == -1 ) {
 		if ( commentIndex == -1 ) {
-			noCommentsResolvConf = append(noCommentsResolvConf, cleanLine...)
+			output = append(output, currentLine...)
 		} else {
 		} else {
-			noCommentsResolvConf = append(noCommentsResolvConf, cleanLine[:commentIndex]...)
+			output = append(output, currentLine[:commentIndex]...)
 		}
 		}
+		output = append(output, []byte("\n")...)
 	}
 	}
-	return noCommentsResolvConf
+	return output
 }
 }
 
 
 func ParseHost(host string, port int, addr string) string {
 func ParseHost(host string, port int, addr string) string {