1package env 2 3// Path contains operations for paths in a PATH env var. 4type Path struct { 5 Changed bool 6 Added map[string]bool 7 Removed map[string]bool 8 revPath []string 9} 10 11// NewPath returns a new Path 12func NewPath(paths []string) *Path { 13 return &Path{ 14 Added: make(map[string]bool), 15 Removed: make(map[string]bool), 16 revPath: ReversePaths(paths), 17 } 18} 19 20// Add adds a path to the list of paths. 21func (p *Path) Add(path string) { 22 p.revPath = append(p.revPath, path) // NOTE: reverse list, so append 23 p.Changed = true 24 p.Added[path] = true 25} 26 27// Remove removes a path from the list of paths. 28func (p *Path) Remove(path string) { 29 for i, x := range p.revPath { 30 if x == path { 31 p.revPath = append(p.revPath[:i], p.revPath[i+1:]...) 32 p.Changed = true 33 p.Removed[path] = true 34 return 35 } 36 } 37 return 38} 39 40// Has checks if a path is already included in the list of paths. 41func (p *Path) Has(path string) bool { 42 for _, x := range p.revPath { 43 if x == path { 44 return true 45 } 46 } 47 return false 48} 49 50// Get returns the list of paths. 51func (p *Path) Get() []string { 52 return ReversePaths(p.revPath) 53} 54 55// GetReversed returns the list of paths reversed. 56func (p *Path) GetReversed() []string { 57 return p.revPath 58} 59 60// ReversePaths reverses a list of paths and returns a new slice. 61func ReversePaths(a []string) []string { 62 res := make([]string, len(a)) 63 n := len(a) 64 for i, p := range a { 65 res[n-i-1] = p 66 } 67 return res 68} 69