Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed Jun 7, 2024
0 parents commit 414002c
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/images/unpath.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- '1.22.x'
steps:
- run: echo ${{ github.ref }}
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- run: go test
- run: make dist
if: startsWith(github.ref, 'refs/tags/')
- name: Release
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
files: dist/*
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 3v0k4

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: dist

PLATFORMS = linux darwin
ARCHITECTURES = amd64 arm64

dist:
@for platform in $(PLATFORMS); do \
for arch in $(ARCHITECTURES); do \
GOOS=$$platform GOARCH=$$arch go build -trimpath -o dist/unpath-$$platform-$$arch main.go; \
done \
done
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Unpath

<div align="center">
<img width="200" width="200" src=".github/images/unpath.svg" />
</div>

```bash
Usage: unpath UNCMD CMD

unpath runs CMD with a modified PATH that does not contain UNCMD.

Arguments:
UNCMD the command to hide from PATH
CMD the command to run with the modified PATH

Examples:
unpath cat ./script script-arg

unpath cat CMD subcmd-arg

unpath cat unpath env CMD
```

## Installation

You can install unpath with Go:

```bash
go install github.com/3v0k4/unpath
```

Or fetch the executable from GitHub:

```bash
# PLATFORM {linux,darwin}
# ARCHITECTURE {amd64,arm64}
curl https://github.com/3v0k4/unpath/releases/download/v0.1.0/unpath-PLATFORM-ARCH --output unpath
chmod +x unpath
./unpath
```

## Usage

```bash
unpath cat ./script script-arg

unpath cat command command-arg
```

To show all the options:

```bash
unpath
```

## Development

Unpath is dependency-free (it only uses the Go standard library), so there are no prerequisites.

```bash
go test
```

To release a new version add a tag and push:

```bash
git tag vX.Y.Z
git push --tags
```

## Contributing

Bug reports and pull requests are welcome on [GitHub](https://github.com/3v0k4/unpath).

## License

The module is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/3v0k4/unpath

go 1.22.3
135 changes: 135 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package main

import (
"cmp"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"sync"
)

type program struct {
args []string
stdout io.Writer
stderr io.Writer
}

func newProgram(args []string, stdout, stderr io.Writer) *program {
return &program{args: args, stdout: stdout, stderr: stderr}
}

func main() {
program := newProgram(os.Args, os.Stdout, os.Stderr)
status := program.main()
os.Exit(status)
}

func (p *program) main() int {
uncmd, cmd, status := p.parse()
if status > 0 {
return status
}
path, status := p.unpath(uncmd)
if status > 0 {
return status
}
return p.run(cmd, path)
}

func (p *program) parse() (string, []string, int) {
if len(p.args) < 3 {
err := `Usage: {PROGRAM} UNCMD CMD
unpath runs CMD with a modified PATH that does not contain UNCMD.
Arguments:
UNCMD the command to hide from PATH
CMD the command to run with the modified PATH
Examples:
unpath cat ./script script-arg
unpath cat CMD subcmd-arg
unpath cat unpath env CMD`
err = strings.ReplaceAll(err, "{PROGRAM}", p.args[0])
fmt.Fprintf(p.stderr, fmt.Sprintln(err))
return "", nil, 1
}
return p.args[1], p.args[2:], 0
}

type result struct {
dir string
status int
}

func (p *program) unpath(cmd string) (string, int) {
path, _ := os.LookupEnv("PATH")
dirs := strings.Split(path, ":")
newDirs := make([]result, len(dirs))
var wg sync.WaitGroup
for i, dir := range dirs {
wg.Add(1)
go func(i int, dir string) {
entries, _ := os.ReadDir(dir) // ignore errors caused by empty dirs in PATH
n, found := slices.BinarySearchFunc(entries, cmd, func(a fs.DirEntry, b string) int {
return cmp.Compare(a.Name(), b)
})
if found {
dir, status := p.unpathEntry(dir, entries, n)
newDirs[i] = result{dir, status}
} else {
newDirs[i] = result{dir, 0}
}
wg.Done()
}(i, dir)
}
wg.Wait()
for i, result := range newDirs {
if result.status > 0 {
return "", result.status
}
dirs[i] = result.dir
}
return strings.Join(dirs, ":"), 0
}

func (p *program) unpathEntry(dir string, entries []fs.DirEntry, entriesIndex int) (string, int) {
tmpdir, err := os.MkdirTemp("", filepath.Base(dir))
if err != nil {
fmt.Fprintln(p.stderr, err)
return "", 1
}

for i, entry := range entries {
if i == entriesIndex {
continue
}
err := os.Symlink(filepath.Join(dir, entry.Name()), filepath.Join(tmpdir, entry.Name()))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return "", 1
}
}

return tmpdir, 0
}

func (p *program) run(cmd []string, path string) int {
arg := append([]string{"-P", path}, cmd...)
subcmd := exec.Command("env", arg...)
subcmd.Env = append(subcmd.Environ(), fmt.Sprintf("PATH=%s", path))
subcmd.Stdout = p.stdout
subcmd.Stderr = p.stderr
if subcmd.Run() == nil {
return 0
} else {
return 1
}
}
Loading

0 comments on commit 414002c

Please sign in to comment.