grim/convey

Bump the version for release
v0.14.0-alpha3
2018-02-20, Gary Kramlich
166a6d1979fa
Bump the version for release
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package environment providies utilities for managing environment variables.
package environment
import (
"fmt"
"os"
"strings"
"time"
"bitbucket.org/rw_grim/govcs"
"bitbucket.org/rw_grim/govcs/hg"
"bitbucket.org/rw_grim/convey/normalize"
)
func setEnv(name, value string) {
if err := os.Setenv(name, value); err != nil {
fmt.Printf("error setting environment variable '%s': %s\n", name, err)
}
}
// addEnv set the environment variable name to value and returns the name
func addEnv(name, value string) string {
setEnv(name, value)
return name
}
func getVCS(wd string) ([]string, error) {
repo, err := govcs.Detect(wd)
if err != nil {
return nil, err
}
name := strings.ToUpper(repo.Name())
vars := []string{
addEnv("VCS", name),
addEnv("COMMIT", repo.Commit()),
addEnv("COMMIT_SHORT", repo.ShortCommit()),
addEnv("COMMIT", repo.Commit()),
addEnv("BRANCH", repo.Branch()),
addEnv("BRANCH_NORMALIZED", normalize.Normalize(repo.Branch())),
addEnv("REMOTE", repo.Remote("")),
addEnv(name+"_COMMIT", repo.Commit()),
addEnv(name+"_COMMIT_SHORT", repo.ShortCommit()),
addEnv(name+"_COMMIT", repo.Commit()),
addEnv(name+"_BRANCH", repo.Branch()),
addEnv(name+"_BRANCH_NORMALIZED", normalize.Normalize(repo.Branch())),
addEnv(name+"_REMOTE", repo.Remote("")),
}
// check for vcs specific values. If this grows past 1, make it a switch
// type.
if hg, ok := repo.(*hg.Mercurial); ok {
vars = append(
vars,
addEnv("BOOKMARK", hg.Bookmark()),
addEnv(name+"_BOOKMARK", hg.Bookmark()),
)
}
return vars, nil
}
// Initialize sets a bunch of environment variables and then returns the name
// of them in a string slice. It will also determine what version control, if
// any is in use, and set variables for it as well.
func Initialize(wd string) ([]string, error) {
oldHome := os.Getenv("HOME")
setEnv("HOME", "/tmp")
defer setEnv("HOME", oldHome)
setEnv("RUN_TIME", time.Now().UTC().Format("2006-01-02T15:04:05-0700"))
env := []string{"RUN_TIME"}
vcs, err := getVCS(wd)
if err != nil {
return nil, err
}
env = append(env, vcs...)
return env, nil
}