@@ -10,9 +10,7 @@ import (
10
10
// PathHelper represents the application path-helper. Takes a configuration as input, and uses local
11
11
// attributes to keep list of files and directories to compose PATH.
12
12
type PathHelper struct {
13
- config * Config // parsed command-line flags
14
- files []string // slice of files in path.d
15
- directories []string // directories that will compose PATH
13
+ config * Config // parsed command-line flags
16
14
}
17
15
18
16
// logger for path-helper instance, skip printing when verbose is off.
@@ -23,44 +21,46 @@ func (p *PathHelper) logger(format string, v ...interface{}) {
23
21
}
24
22
25
23
// append a directory in global list, making sure it skips duplicates when setting is enabled.
26
- func (p * PathHelper ) append (directory string ) {
24
+ func (p * PathHelper ) append (directories [] string , directory string ) [] string {
27
25
if p .config .SkipDuplicates {
28
- for _ , d := range p . directories {
26
+ for _ , d := range directories {
29
27
if d == directory {
30
28
p .logger ("[WARN] Skipping entry '%s', is already defined." , directory )
31
- return
29
+ return directories
32
30
}
33
31
}
34
32
}
35
- p . directories = append (p . directories , directory )
33
+ return append (directories , directory )
36
34
}
37
35
38
36
// globPathFiles load list of files in base directory. Returns errors when base directory does not
39
37
// exist or when having issues to execute globing.
40
- func (p * PathHelper ) globPathFiles () error {
41
- baseDir := p .config .BaseDir
38
+ func (p * PathHelper ) globPathFiles (baseDir string ) ([]string , error ) {
42
39
p .logger ("Inspecting paths directory: '%s'" , baseDir )
43
40
if ! dirExists (baseDir ) {
44
- return fmt .Errorf ("can't find base directory at '%s'" , baseDir )
41
+ return nil , fmt .Errorf ("can't find base directory at '%s'" , baseDir )
45
42
}
46
43
47
- var err error
48
44
pattern := path .Join (baseDir , "*" )
49
- p .files , err = filepath .Glob (pattern )
50
- return err
45
+ files , err := filepath .Glob (pattern )
46
+ if err != nil {
47
+ return nil , err
48
+ }
49
+ return files , nil
51
50
}
52
51
53
- // gatherPathDirs based in path files, read and inspect direcotories listed in those. Can return
54
- // errors related to reading files.
55
- func (p * PathHelper ) gatherPathDirs () error {
56
- for _ , file := range p .files {
52
+ // inspectPathDirectories based in path files, read and inspect direcotories listed in those. Can
53
+ // return errors related to reading files.
54
+ func (p * PathHelper ) inspectPathDirectories (files []string ) ([]string , error ) {
55
+ directories := []string {}
56
+ for _ , file := range files {
57
57
p .logger ("File '%s'" , file )
58
- directories , err := readLines (file )
58
+ lines , err := readLines (file )
59
59
if err != nil {
60
- return fmt .Errorf ("can't read file '%s': '%v'" , file , err )
60
+ return nil , fmt .Errorf ("can't read file '%s': '%v'" , file , err )
61
61
}
62
62
63
- for _ , directory := range directories {
63
+ for _ , directory := range lines {
64
64
p .logger ("\t - '%s'" , directory )
65
65
if strings .HasPrefix (directory , "#" ) {
66
66
continue
@@ -69,35 +69,51 @@ func (p *PathHelper) gatherPathDirs() error {
69
69
p .logger ("[WARN] Directory '%s' (%s) is not found! Skipping." , directory , file )
70
70
continue
71
71
}
72
- p .append (directory )
72
+ directories = p .append (directories , directory )
73
73
}
74
74
}
75
- return nil
75
+ return directories , nil
76
+ }
77
+
78
+ // collect glob for files and open them to extract contents. File contents are threated as path
79
+ // directories, therefore configuration directive applies on them. It can return error when having
80
+ // problems to glob directories and on reading files.
81
+ func (p * PathHelper ) collect (baseDir string ) ([]string , error ) {
82
+ files , err := p .globPathFiles (baseDir )
83
+ if err != nil {
84
+ return nil , err
85
+ }
86
+ directories , err := p .inspectPathDirectories (files )
87
+ if err != nil {
88
+ return nil , err
89
+ }
90
+ return directories , nil
76
91
}
77
92
78
93
// pathDirsColonJoined return slice of direcotires joined by colon.
79
- func (p * PathHelper ) pathDirsColonJoined ( ) string {
80
- return strings .Join (p . directories , ":" )
94
+ func (p * PathHelper ) colonJoin ( directories [] string ) string {
95
+ return strings .Join (directories , ":" )
81
96
}
82
97
83
98
// RenderExpression print out the shell expression exporting PATH. Will forward errors from methods
84
99
// listing and reading path files, and inspecting direcotories present found in those files.
85
100
func (p * PathHelper ) RenderExpression () (string , error ) {
86
- if err := p .globPathFiles (); err != nil {
101
+ pathDirectories , err := p .collect (p .config .PathBaseDir )
102
+ if err != nil {
87
103
return "" , err
88
104
}
89
- if err := p .gatherPathDirs (); err != nil {
105
+
106
+ manDirectories , err := p .collect (p .config .ManBaseDir )
107
+ if err != nil {
90
108
return "" , err
91
109
}
92
110
93
- return fmt .Sprintf ("export PATH=\" %s\" " , p .pathDirsColonJoined ()), nil
111
+ expr := fmt .Sprintf (`PATH="%s" ; MANPATH="%s" ; export PATH MANPATH ;` ,
112
+ p .colonJoin (pathDirectories ), p .colonJoin (manDirectories ))
113
+ return expr , nil
94
114
}
95
115
96
116
// NewPathHelper instantiate a PathHelper type.
97
117
func NewPathHelper (config * Config ) * PathHelper {
98
- return & PathHelper {
99
- config : config ,
100
- files : []string {},
101
- directories : []string {},
102
- }
118
+ return & PathHelper {config : config }
103
119
}
0 commit comments