env

package
v0.0.0-...-bd4bd5f Latest Latest
Warning

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

Go to latest
Published: May 13, 2018 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Overview

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

Example

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.

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
}
Output:

Hello World!

Index

Examples

Constants

View Source
const VarGopath = "GOPATH"

VarGopath is the Go workspace path variable name.

View Source
const VarPath = "PATH"

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

Variables

This section is empty.

Functions

func Gopath

func Gopath() pathlist.List

Gopath gets the Go workspace path.

func Path

func Path() pathlist.List

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

func SetGopath

func SetGopath(l pathlist.List) error

SetGopath sets the Go workspace path.

func SetPath

func SetPath(l pathlist.List) error

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

func SetSlice

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

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).

Types

This section is empty.

Jump to

Keyboard shortcuts

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