which

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2020 License: MIT Imports: 5 Imported by: 14

README

go-which

Build Status hairyhenderson/go-which on DockerHub GoDoc

A cross-platform Go implementation of the which(1) command, usable both as a CLI and library.

Usage of which:
  -a    List all instances of executables found (instead of just the first).
  -s    No output, just return 0 if all executables are found, or 1 if some were not found.
  -v    Print the version

Unlike the UNIX which(1) command, even if multiple programs are given as input, only the first one found will be returned.

CLI Usage

Chances are you don't really need this, since most UNIX-like OSes come with the more established (and significantly smaller) C implementation of which(1), either as a standalone binary, or as a shell builtin.

But if there's some reason this may be useful to you, you can use this just like the normal which(1):

$ which zsh
/usr/local/bin/zsh
$ which -a zsh
/usr/local/bin/zsh
/bin/zsh
$ which zsh bash sh
/usr/local/bin/zsh
$ which -a zsh bash sh
/usr/local/bin/zsh
/bin/zsh
/bin/bash
/bin/sh
$ if (which -s zsh bash); then
> echo 'I have zsh and bash installed';
> fi
I have zsh and bash installed
$ if (which -s zsh bash ash); then echo 'yup'
> else
> echo "I'm missing one of them...";
> fi
I'm missing one of them...

Go package usage

If you're writing a program in the Go language, it can be useful to not have to shell out to which(1) to locate a binary.

The simplest usage is:

package main

import (
  "fmt"
  "github.com/hairyhenderson/go-which"
)

func main() {
  zshPath := which.Which("zsh")

  fmt.Printf("zsh found at %s", zshPath)
}

See the godocs for more information.

License

The MIT License

Copyright (c) 2018-2020 Dave Henderson

Documentation

Overview

Package which - locates executable files in the current path. A cross-platform implementation of the `which(1)` command.

This allows finding programs by searching the current `PATH` environment variable without needing to shell out to the operating system's `which` command.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(program ...string) []string

All returns all instances of the executable program(s), instead of just the first one.

Example
path := All("zsh")
fmt.Printf("%v", path)
Output:

[/usr/local/bin/zsh /bin/zsh]
Example (Multiples)

When given multiple arguments, `All` will return all paths, sorted by argument order

path := All("zsh", "bash")
fmt.Printf("%v", path)
Output:

[/usr/local/bin/zsh /bin/zsh /bin/bash]

func Found

func Found(program ...string) bool

Found returns true if all of the given executable program(s) are found, false if one or more are not found.

Example
if Found("zsh") {
	fmt.Println("got it!")
}

if !Found("bogon") {
	fmt.Println("phew, no bogons")
}
Output:

got it!
phew, no bogons
Example (Multiples)

When given multiple arguments, `Found` will return all paths, sorted by argument order

if Found("zsh", "bash") {
	fmt.Println("a decent collection of shells")
}

if !Found("zsh", "bash", "ash") {
	fmt.Println("just missing the ashes...")
}
Output:

a decent collection of shells
just missing the ashes...

func Which

func Which(program ...string) string

Which locates executable program(s) in the user's path. If more than one occurrence is found, the first will be returned. Unlike the UNIX which(1) command, even if multiple programs are given as input, only the first-found will be returned. If none of the programs are found, the empty string is returned.

Example
path := Which("sh")
fmt.Printf("Found sh at: %s", path)
Output:

Found sh at: /bin/sh
Example (Multiples)

When given multiple arguments, `Which` will return the path for the first found

path := Which("bogus", "sh")
fmt.Printf("First found was: %s", path)
Output:

First found was: /bin/sh

Types

This section is empty.

Directories

Path Synopsis
cmd
which
CLI for the which command.
CLI for the which command.
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL