Skip to content

Commit 0760d7b

Browse files
committed
Introducing manpath support.
1 parent 118e643 commit 0760d7b

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

pkg/path-helper/path_helper.go

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
// PathHelper represents the application path-helper. Takes a configuration as input, and uses local
1111
// attributes to keep list of files and directories to compose PATH.
1212
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
1614
}
1715

1816
// logger for path-helper instance, skip printing when verbose is off.
@@ -23,44 +21,46 @@ func (p *PathHelper) logger(format string, v ...interface{}) {
2321
}
2422

2523
// 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 {
2725
if p.config.SkipDuplicates {
28-
for _, d := range p.directories {
26+
for _, d := range directories {
2927
if d == directory {
3028
p.logger("[WARN] Skipping entry '%s', is already defined.", directory)
31-
return
29+
return directories
3230
}
3331
}
3432
}
35-
p.directories = append(p.directories, directory)
33+
return append(directories, directory)
3634
}
3735

3836
// globPathFiles load list of files in base directory. Returns errors when base directory does not
3937
// 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) {
4239
p.logger("Inspecting paths directory: '%s'", baseDir)
4340
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)
4542
}
4643

47-
var err error
4844
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
5150
}
5251

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 {
5757
p.logger("File '%s'", file)
58-
directories, err := readLines(file)
58+
lines, err := readLines(file)
5959
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)
6161
}
6262

63-
for _, directory := range directories {
63+
for _, directory := range lines {
6464
p.logger("\t- '%s'", directory)
6565
if strings.HasPrefix(directory, "#") {
6666
continue
@@ -69,35 +69,51 @@ func (p *PathHelper) gatherPathDirs() error {
6969
p.logger("[WARN] Directory '%s' (%s) is not found! Skipping.", directory, file)
7070
continue
7171
}
72-
p.append(directory)
72+
directories = p.append(directories, directory)
7373
}
7474
}
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
7691
}
7792

7893
// 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, ":")
8196
}
8297

8398
// RenderExpression print out the shell expression exporting PATH. Will forward errors from methods
8499
// listing and reading path files, and inspecting direcotories present found in those files.
85100
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 {
87103
return "", err
88104
}
89-
if err := p.gatherPathDirs(); err != nil {
105+
106+
manDirectories, err := p.collect(p.config.ManBaseDir)
107+
if err != nil {
90108
return "", err
91109
}
92110

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
94114
}
95115

96116
// NewPathHelper instantiate a PathHelper type.
97117
func NewPathHelper(config *Config) *PathHelper {
98-
return &PathHelper{
99-
config: config,
100-
files: []string{},
101-
directories: []string{},
102-
}
118+
return &PathHelper{config: config}
103119
}

pkg/path-helper/path_helper_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ func TestPathHelper(t *testing.T) {
1313
SkipDuplicates: true,
1414
SkipNotFound: true,
1515
Verbose: true,
16-
BaseDir: "../../test/paths.d",
16+
ManBaseDir: "../../test/paths.d",
17+
PathBaseDir: "../../test/paths.d",
1718
}
1819
expectedJoinedPaths := "/a/a/a:/b/b/b:/c/c/c:/d/d/d"
1920
expectedJoinedPathsDuplicated := "/a/a/a:/b/b/b:/c/c/c:/d/d/d:/d/d/d"
@@ -72,19 +73,19 @@ func TestPathHelper(t *testing.T) {
7273
// share the test context and expected characteristics.
7374
func assertFilesAndDirectories(t *testing.T, c *Config, expectedLen int, expectedPaths string) {
7475
p := NewPathHelper(c)
75-
err := p.globPathFiles()
76+
files, err := p.globPathFiles(c.PathBaseDir)
7677
t.Logf("Error: '%#v", err)
7778
assert.NoError(t, err)
78-
t.Logf("Files: '%#v'", p.files)
79-
assert.True(t, len(p.files) >= expectedLen)
79+
t.Logf("Files: '%#v'", files)
80+
assert.True(t, len(files) >= expectedLen)
8081

81-
err = p.gatherPathDirs()
82+
directories, err := p.inspectPathDirectories(files)
8283
t.Logf("Error: '%#v", err)
8384
assert.NoError(t, err)
84-
t.Logf("Directories: '%#v'", p.directories)
85-
assert.True(t, len(p.directories) >= expectedLen)
85+
t.Logf("Directories: '%#v'", directories)
86+
assert.True(t, len(directories) >= expectedLen)
8687

87-
assert.Equal(t, expectedPaths, p.pathDirsColonJoined())
88+
assert.Equal(t, expectedPaths, p.colonJoin(directories))
8889
}
8990

9091
// assertExpression assert primary objective of this app, the shell expression to export PATH. Method
@@ -95,5 +96,7 @@ func assertExpression(t *testing.T, c *Config, expectedExpression string) {
9596
assert.NoError(t, err)
9697
t.Logf("Expression: '%s'", s)
9798
assert.NotEmpty(t, s)
98-
assert.Equal(t, fmt.Sprintf("export PATH=\"%s\"", expectedExpression), s)
99+
expr := fmt.Sprintf(`PATH="%s" ; MANPATH="%s" ; export PATH MANPATH ;`,
100+
expectedExpression, expectedExpression)
101+
assert.Equal(t, expr, s)
99102
}

0 commit comments

Comments
 (0)