github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/network/debinterfaces/stanza.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package debinterfaces 5 6 var _ Stanza = (*stanza)(nil) 7 8 // stanza implements Stanza. 9 type stanza struct { 10 definition string 11 location Location 12 } 13 14 // Stanza represents a network definition as described by 15 // interfaces(8). 16 type Stanza interface { 17 // A definition is the top-level stanza, together with any 18 // options. 19 Definition() []string 20 21 // The file and linenumber, if any, where the stanza was 22 // declared. 23 Location() Location 24 } 25 26 // Location represents source file and line number information 27 type Location struct { 28 Filename string 29 LineNum int 30 } 31 32 // AllowStanza are lines beginning with the word 'allow-*'. 33 type AllowStanza struct { 34 stanza 35 DeviceNames []string 36 } 37 38 // AutoStanza are lines beginning with the word "auto". 39 type AutoStanza struct { 40 stanza 41 DeviceNames []string 42 } 43 44 // IfaceStanza are lines beginning with 'iface'. 45 type IfaceStanza struct { 46 stanza 47 DeviceName string 48 HasBondMasterOption bool 49 HasBondOptions bool 50 IsAlias bool 51 IsBridged bool 52 IsVLAN bool 53 Options []string 54 } 55 56 // MappingStanza are lines beginning with the word "mapping". 57 type MappingStanza struct { 58 stanza 59 DeviceNames []string 60 Options []string 61 } 62 63 // NoAutoDownStanza are lines beginning with "no-auto-down". 64 type NoAutoDownStanza struct { 65 stanza 66 DeviceNames []string 67 } 68 69 // NoScriptsStanza are lines beginning with "no-scripts". 70 type NoScriptsStanza struct { 71 stanza 72 DeviceNames []string 73 } 74 75 // SourceStanza are lines beginning with "source". 76 type SourceStanza struct { 77 stanza 78 Path string 79 Sources []string 80 Stanzas []Stanza 81 } 82 83 // SourceDirectoryStanza are lines beginning with "source-directory". 84 type SourceDirectoryStanza struct { 85 stanza 86 Path string 87 Sources []string 88 Stanzas []Stanza 89 } 90 91 func definitionWithOptions(definition string, options []string) []string { 92 result := make([]string, 1+len(options)) 93 result[0] = definition 94 for i := range options { 95 result[i+1] = options[i] 96 } 97 return result 98 } 99 100 // Location returns the filename and line number of the first line of 101 // the definition, if any. 102 func (s stanza) Location() Location { 103 return s.location 104 } 105 106 // Definition returns all the lines that define the stanza. The 107 // individual lines are trimmed of leading and trailing whitespace. 108 func (s stanza) Definition() []string { 109 return []string{s.definition} 110 } 111 112 // Definition returns all the lines that define the stanza. The 113 // individual lines are trimmed of leading and trailing whitespace. 114 func (s IfaceStanza) Definition() []string { 115 return definitionWithOptions(s.definition, s.Options) 116 } 117 118 // Definition returns all the lines that define the stanza. The 119 // individual lines are trimmed of leading and trailing whitespace. 120 func (s MappingStanza) Definition() []string { 121 return definitionWithOptions(s.definition, s.Options) 122 }