pathlist.v0: gopkg.in/pathlist.v0/env Index | Examples | Files

package env

import "gopkg.in/pathlist.v0/env"

Package env provides helper functions for manipulating environment variables holding filepath lists.

This example demonstrates setting PATH and GOPATH, both for the current process (SetPath) and a child process (SetSlice). It first creates a new Go workspace for building a Hello World executable, adding the workspace to GOPATH when invoking the go tool. Then it invokes the executable built, adding the workspace bin directory to PATH.

Code:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "os/exec"
    "path/filepath"

    "gopkg.in/pathlist.v0"
    "gopkg.in/pathlist.v0/env"
)

const helloworld = `
package main

import "fmt"

func main() {
	fmt.Println("Hello World!")
}
`

// This example demonstrates setting PATH and GOPATH, both for the current
// process (SetPath) and a child process (SetSlice).
// It first creates a new Go workspace for building a Hello World executable,
// adding the workspace to GOPATH when invoking the go tool.
// Then it invokes the executable built, adding the workspace bin directory to
// PATH.
func main() {
    // use a separate function as deferreds are not invoked with log.Fatal
    out, err := doExample()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(out))

}

func doExample() ([]byte, error) {
    // create workspace
    wkspc, err := ioutil.TempDir(os.TempDir(), "env_test.Example_")
    if err != nil {
        return nil, err
    }
    defer os.RemoveAll(wkspc)

    // place source file
    hellodir := filepath.Join(wkspc, "src", "hello")
    if err := os.MkdirAll(hellodir, 0777); err != nil {
        return nil, err
    }
    if err := ioutil.WriteFile(filepath.Join(hellodir, "hello.go"),
        []byte(helloworld), 0666); err != nil {

        return nil, err
    }

    // invoke go install
    newgopath, err := pathlist.PrependTo(env.Gopath(), wkspc)
    if err != nil {
        return nil, err
    }
    newenv := env.SetSlice(os.Environ(), env.VarGopath, newgopath)
    goinst := exec.Command("go", "install", "hello")
    goinst.Env = newenv
    if stdouterr, err := goinst.CombinedOutput(); err != nil {
        log.Print(string(stdouterr))
        return nil, err
    }

    // invoke executable
    bindir := filepath.Join(wkspc, "bin") // should be created by go install
    oldpath := env.Path()
    newpath, err := pathlist.PrependTo(oldpath, bindir)
    if err != nil {
        return nil, err
    }
    env.SetPath(newpath)
    defer env.SetPath(oldpath)
    hello := exec.Command("hello")
    stdouterr, err := hello.CombinedOutput()
    if err != nil {
        log.Print(string(stdouterr))
        return nil, err
    }

    return stdouterr, nil
}

Index

Examples

Package Files

env.go env_unix.go

Constants

const VarGopath = "GOPATH"

VarGopath is the Go workspace path variable name.

const VarPath = "PATH"

VarPath is the OS (shell) specific executable search path variable name.

func Gopath Uses

func Gopath() pathlist.List

Gopath gets the Go workspace path.

func Path Uses

func Path() pathlist.List

Path gets the OS (shell) specific executable search path.

func SetGopath Uses

func SetGopath(l pathlist.List) error

SetGopath sets the Go workspace path.

func SetPath Uses

func SetPath(l pathlist.List) error

SetPath sets the OS (shell) specific executable search path.

func SetSlice Uses

func SetSlice(env []string, key string, list pathlist.List) []string

SetSlice takes a slice of environment variables (as used with os.Environ and os/exec.Cmd.Env), and returns a copy of env with key set to list.

func Slice Uses

func Slice(env []string, key string) pathlist.List

Slice gets the value for key as a pathlist.List from a slice of environment variables (as used with os.Environ and os/exec.Cmd.Env).

Package env imports 3 packages (graph). Updated 2018-05-13. Refresh now. Tools for package owners.