Compare commits

...

7 Commits

Author SHA1 Message Date
293c75456e Version bumped to 1.1.0 2024-01-19 22:49:57 +01:00
95934ee91a Updated README.md
* commit '2253683cab77e2aa4355223480394ff9fbe989b2':
  Updated `README.md`
2024-01-19 21:47:30 +00:00
2253683cab Updated README.md 2024-01-19 22:46:03 +01:00
22b7120c43 v1.1.0
* commit '23e043fee20716b28d17db5c00304f22ded4f472':
  Dedupe output parts
  Support for comments
2024-01-19 21:36:57 +00:00
23e043fee2 Dedupe output parts 2024-01-19 22:02:52 +01:00
96a20b9278 Support for comments 2024-01-19 22:02:52 +01:00
1d4ca55f2a master -> development
* commit '8d12831fe5ebb8f76e34a45bb89969a4f7743fc4':
  Version bumped to `1.0.2`
2024-01-19 20:59:11 +00:00
7 changed files with 64 additions and 15 deletions

View File

@ -6,7 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.1.0] - 2024-01-19
### Added
- Support for comments (`#`) in parts files.
- Explicitly inherit paths from `PATH` environment variable.
- Ability to deduplicate output paths.
## [1.0.2] - 2023-12-05
### Added
- Initial release.

View File

@ -1,4 +1,4 @@
VERSION = 1.0.2
VERSION = 1.1.0
PREFIX ?= /usr/local
OS != uname -o

View File

@ -35,7 +35,7 @@ Running the program with such a directory would yield the following result:
```
$ pathsd paths.d/
export PATH="/Users/bilbo/opt/bin:/opt/homebrew/bin:/opt/homebrew/sbin":$PATH
export PATH="/Users/bilbo/opt/bin:/opt/homebrew/bin:/opt/homebrew/sbin":<paths inherited from $PATH>
```
You can specify multiple directories. They'll be processed one by one in the

View File

@ -1,6 +1,6 @@
# Package
version = "1.0.2"
version = "1.1.0"
author = "Tomek Wójcik <contact@bthlabs.pl>"
description = "pathsd by BTHLabs"
license = "MIT"

View File

@ -1,4 +1,4 @@
import std/[algorithm, dirs, logging, options, paths, strutils, syncio]
import std/[algorithm, dirs, envvars, logging, options, paths, strutils, syncio]
import strformat
import commandant
@ -9,7 +9,7 @@ type
output*: seq[string]
errorCode*: ErrorCode
const version = "1.0.2"
const version = "1.1.0"
const usage_string = """usage: pathsd [--version | --help] [-v | -q] [-s shell] search_path ..."""
const version_string = fmt"""pathsd {version}"""
const help_string = fmt"""pathsd {version} by BTHLabs
@ -19,6 +19,7 @@ Usage:
pathsd [options] search_path ...
Options:
-s --shell target shell (defaults to bash)
-d --dedupe deduplicate the final paths
-v --verbose turn debugging messages on
-q --quiet supress all logging messages
--version show the version
@ -52,7 +53,8 @@ proc readPart(path: Path): seq[string] =
try:
var line: string = readLine(pathFile)
while line != "":
result.add(line)
if line.startsWith("#") == false:
result.add(line)
line = readLine(pathFile)
except EOFError as exception:
discard
@ -62,7 +64,7 @@ proc readPart(path: Path): seq[string] =
return result
proc main*(searchPaths: seq[string]): MainResult =
proc main*(searchPaths: seq[string], envParts: seq[string], dedupe: bool): MainResult =
var parts: seq[string] = @[]
logger.log(lvlDebug, fmt"Processing search paths: {searchPaths}")
@ -83,6 +85,16 @@ proc main*(searchPaths: seq[string]): MainResult =
for partPath in partPaths:
parts = parts & readPart(partPath)
parts = parts & envParts
if dedupe == true:
var dedupedParts: seq[string] = @[]
for part in parts:
if dedupedParts.contains(part) == false:
dedupedParts.add(part)
parts = dedupedParts
return MainResult(output: parts, errorCode: QuitSuccess)
@ -92,6 +104,7 @@ when isMainModule:
commandant.option(shell, string, "shell", "s", "bash")
flag(verbose, "verbose", "v")
flag(quiet, "quiet", "q")
flag(dedupe, "dedupe", "d")
exitoption("help", "h", help_string, QuitFailure)
exitoption("version", "", version_string, QuitFailure)
errorproc(handleCommandantError)
@ -104,7 +117,11 @@ when isMainModule:
logger.levelThreshold = loggerLevel
var mainResult = main(searchPaths)
var envParts: seq[string] = @[]
var envPath = envvars.getEnv("PATH")
envParts = envPath.split(":")
var mainResult = main(searchPaths, envParts, dedupe)
if mainResult.output != @[] and mainResult.errorCode == QuitSuccess:
var output: string = ""
@ -112,7 +129,7 @@ when isMainModule:
case shell
of "bash":
let pathComponents = join(mainResult.output, ":")
output = fmt"""export PATH="{pathComponents}":$PATH"""
output = fmt"""export PATH="{pathComponents}""""
if output != "":
echo(output)

View File

@ -1 +1,2 @@
/Users/bilbo/opt/bin
#/Users/bilbo/.local/bin

View File

@ -7,13 +7,17 @@ suite "Test main() proc":
setup:
const fixturesPath = Path(currentSourcePath) / Path("..") / Path("..") / Path("fixtures")
const pathsdFixturePath = fixturesPath / Path("paths.d")
const expectedOutput = @["/Users/bilbo/opt/bin", "/opt/homebrew/bin", "/opt/homebrew/sbin"]
const envParts = @["/usr/local/bin", "/usr/bin"]
const expectedOutput = @[
"/Users/bilbo/opt/bin", "/opt/homebrew/bin", "/opt/homebrew/sbin",
"/usr/local/bin", "/usr/bin",
]
pathsd.logger.levelThreshold = lvlNone
test "Test happy path":
# When
var mainResult = pathsd.main(@[pathsdFixturePath.string])
var mainResult = pathsd.main(@[pathsdFixturePath.string], envParts, false)
# Then
check mainResult.output == expectedOutput
@ -21,10 +25,28 @@ suite "Test main() proc":
test "Test skipping search paths that don't exist":
# When
var mainResult = pathsd.main(@[
pathsdFixturePath.string,
(fixturesPath / Path("idontexist")).string,
])
var mainResult = pathsd.main(
@[
pathsdFixturePath.string,
(fixturesPath / Path("idontexist")).string,
],
envParts,
false,
)
# Then
check mainResult.output == expectedOutput
test "Test dedupe":
# Given
const localEnvParts = @[
"/usr/local/bin", "/usr/bin", "/Users/bilbo/opt/bin",
]
# When
var mainResult = pathsd.main(
@[pathsdFixturePath.string], localEnvParts, true,
)
# Then
check mainResult.output == expectedOutput