|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// PathHelper represents the application path-helper. Takes a configuration as input, and uses local |
| 11 | +// attributes to keep list of files and directories to compose PATH. |
| 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 |
| 16 | +} |
| 17 | + |
| 18 | +// logger for path-helper instance, skip printing when verbose is off. |
| 19 | +func (p *PathHelper) logger(format string, v ...interface{}) { |
| 20 | + if p.config.Verbose { |
| 21 | + fmt.Printf(fmt.Sprintf("# %s\n", format), v...) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// append a direcotry in global list, making sure it skips duplicates when setting is enabled. |
| 26 | +func (p *PathHelper) append(directory string) { |
| 27 | + if p.config.SkipDuplicates { |
| 28 | + for _, d := range p.directories { |
| 29 | + if d == directory { |
| 30 | + p.logger("[WARN] Skipping entry '%s', is already defined.", directory) |
| 31 | + return |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + p.directories = append(p.directories, directory) |
| 36 | +} |
| 37 | + |
| 38 | +// globPathFiles load list of files in base directory. Returns errors when base directory does not |
| 39 | +// exist or when having issues to execute globing. |
| 40 | +func (p *PathHelper) globPathFiles() error { |
| 41 | + baseDir := p.config.BaseDir |
| 42 | + p.logger("Inspecting paths directory: '%s'", baseDir) |
| 43 | + if !dirExists(baseDir) { |
| 44 | + return fmt.Errorf("can't find base directory at '%s'", baseDir) |
| 45 | + } |
| 46 | + |
| 47 | + var err error |
| 48 | + pattern := path.Join(baseDir, "*") |
| 49 | + p.files, err = filepath.Glob(pattern) |
| 50 | + return err |
| 51 | +} |
| 52 | + |
| 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 { |
| 57 | + p.logger("File '%s'", file) |
| 58 | + directories, err := readLines(file) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("can't read file '%s': '%v'", file, err) |
| 61 | + } |
| 62 | + |
| 63 | + for _, directory := range directories { |
| 64 | + p.logger("\t- '%s'", directory) |
| 65 | + if p.config.SkipNotFound && !dirExists(directory) { |
| 66 | + p.logger("[WARN] Directory '%s' (%s) is not found! Skipping.", directory, file) |
| 67 | + continue |
| 68 | + } |
| 69 | + p.append(directory) |
| 70 | + } |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// pathDirsColonJoined return slice of direcotires joined by colon. |
| 76 | +func (p *PathHelper) pathDirsColonJoined() string { |
| 77 | + return strings.Join(p.directories, ":") |
| 78 | +} |
| 79 | + |
| 80 | +// RenderExpression print out the shell expression exporting PATH. Will forward errors from methods |
| 81 | +// listing and reading path files, and inspecting direcotories present found in those files. |
| 82 | +func (p *PathHelper) RenderExpression() (string, error) { |
| 83 | + if err := p.globPathFiles(); err != nil { |
| 84 | + return "", err |
| 85 | + } |
| 86 | + if err := p.gatherPathDirs(); err != nil { |
| 87 | + return "", err |
| 88 | + } |
| 89 | + |
| 90 | + return fmt.Sprintf("export PATH=\"%s\"", p.pathDirsColonJoined()), nil |
| 91 | +} |
| 92 | + |
| 93 | +// NewPathHelper instantiate a PathHelper type. |
| 94 | +func NewPathHelper(config *Config) *PathHelper { |
| 95 | + return &PathHelper{ |
| 96 | + config: config, |
| 97 | + files: []string{}, |
| 98 | + directories: []string{}, |
| 99 | + } |
| 100 | +} |
0 commit comments