grim/convey
Clone
Summary
Browse
Changes
Graph
Typo fix
2018-01-14, Gary Kramlich
4022dd81834e
Typo fix
// 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
(
"os"
"strings"
"time"
"bitbucket.org/rw_grim/govcs"
"bitbucket.org/rw_grim/convey/normalize"
)
// addEnv set the environment variable name to value and returns the name
func
addEnv
(
name
,
value
string
)
string
{
os
.
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
{}
vars
=
append
(
[]
string
{},
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
(
""
)),
)
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"
)
os
.
Setenv
(
"HOME"
,
"/tmp"
)
defer
os
.
Setenv
(
"HOME"
,
oldHome
)
os
.
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
}