|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | +) |
| 8 | + |
| 9 | +// fatal print out error as a shell comment and exit on error. |
| 10 | +func fatal(err error) { |
| 11 | + fmt.Fprintf(os.Stderr, fmt.Sprintf("# [ERROR] %v\n", err)) |
| 12 | + os.Exit(1) |
| 13 | +} |
| 14 | + |
| 15 | +// commandLineParser handle command line flags, display help message. |
| 16 | +func commandLineParser(config *Config) { |
| 17 | + flag.Usage = func() { |
| 18 | + fmt.Printf(`## path-helper |
| 19 | +
|
| 20 | +Helper command-line application to compose "PATH" expression based in a "paths.d" |
| 21 | +directory, respecting order of files and adding toggles to skip entries. |
| 22 | +
|
| 23 | +To export new "PATH" to your shell instance, run "eval" against "path-helper" |
| 24 | +output. Examples below. |
| 25 | +
|
| 26 | +Usage: |
| 27 | + $ path-helper [-h|--help|flags] |
| 28 | +
|
| 29 | +Examples: |
| 30 | + $ path-helper -v |
| 31 | + $ path-helper -v -s=false -d=false |
| 32 | +
|
| 33 | +Shell-Export: |
| 34 | + $ eval "$(path-helper -s=false -d=false)" |
| 35 | + $ echo $PATH |
| 36 | +
|
| 37 | +Command-Line Options: |
| 38 | +`) |
| 39 | + flag.PrintDefaults() |
| 40 | + } |
| 41 | + |
| 42 | + flag.StringVar(&config.BaseDir, "b", "/etc/paths.d", "Base directory") |
| 43 | + flag.BoolVar(&config.SkipNotFound, "d", true, "Skip not found directories") |
| 44 | + flag.BoolVar(&config.SkipDuplicates, "s", true, "Skip duplicated entries") |
| 45 | + flag.BoolVar(&config.Verbose, "v", false, "Verbose") |
| 46 | + |
| 47 | + flag.Parse() |
| 48 | +} |
| 49 | + |
| 50 | +func main() { |
| 51 | + config := &Config{} |
| 52 | + commandLineParser(config) |
| 53 | + |
| 54 | + p := NewPathHelper(config) |
| 55 | + expr, err := p.RenderExpression() |
| 56 | + if err != nil { |
| 57 | + fatal(err) |
| 58 | + } |
| 59 | + fmt.Println(expr) |
| 60 | +} |
0 commit comments