debian.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package network
  15. import (
  16. "log"
  17. "strings"
  18. )
  19. func ProcessDebianNetconf(config []byte) ([]InterfaceGenerator, error) {
  20. log.Println("Processing Debian network config")
  21. lines := formatConfig(string(config))
  22. stanzas, err := parseStanzas(lines)
  23. if err != nil {
  24. return nil, err
  25. }
  26. interfaces := make([]*stanzaInterface, 0, len(stanzas))
  27. for _, stanza := range stanzas {
  28. switch s := stanza.(type) {
  29. case *stanzaInterface:
  30. interfaces = append(interfaces, s)
  31. }
  32. }
  33. log.Printf("Parsed %d network interfaces\n", len(interfaces))
  34. log.Println("Processed Debian network config")
  35. return buildInterfaces(interfaces), nil
  36. }
  37. func formatConfig(config string) []string {
  38. lines := []string{}
  39. config = strings.Replace(config, "\\\n", "", -1)
  40. for config != "" {
  41. split := strings.SplitN(config, "\n", 2)
  42. line := strings.TrimSpace(split[0])
  43. if len(split) == 2 {
  44. config = split[1]
  45. } else {
  46. config = ""
  47. }
  48. if strings.HasPrefix(line, "#") || line == "" {
  49. continue
  50. }
  51. lines = append(lines, line)
  52. }
  53. return lines
  54. }