Skip to content

Commit 1da62d8

Browse files
committed
Merge pull request #31 from mboersma/25-refresh-units-fix
fix(cmd): allow `-p` to specify where to save unit files
2 parents e1f7403 + 4497af2 commit 1da62d8

4 files changed

Lines changed: 23 additions & 27 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ installer:
99
@if [ ! -d makeself ]; then git clone -b deisctl-hack https://github.com/deis/makeself.git; fi
1010
PATH=./makeself:$$PATH makeself.sh --bzip2 --nox11 --target /usr/local/bin dist \
1111
dist/deisctl-`cat deis-version`-`go env GOOS`-`go env GOARCH`.run \
12-
"Deis Control Utility" "/usr/local/bin/deisctl refresh-units"
12+
"Deis Control Utility" "deisctl refresh-units"
1313

1414
install:
1515
godep go install -v ./...

client/unit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// path hierarchy for finding systemd service templates
17-
var rootPaths = []string{"/var/lib/deis/units", "~/.deisctl/units"}
17+
var rootPaths = []string{"~/.deisctl/units", "/var/lib/deis/units"}
1818

1919
// Units returns a list of units filtered by target
2020
func (c *FleetClient) Units(target string) (units []string, err error) {
@@ -119,7 +119,7 @@ func readTemplate(component string) (out []byte, err error) {
119119
} else {
120120
// otherwise look in rootPaths hierarchy
121121
for _, rootPath := range rootPaths {
122-
rootPath, _ := expandUser(rootPath)
122+
rootPath, _ := ExpandUser(rootPath)
123123
filename := path.Join(rootPath, templateName)
124124
if _, err := os.Stat(filename); err == nil {
125125
templateFile = filename
@@ -138,11 +138,11 @@ func readTemplate(component string) (out []byte, err error) {
138138
return
139139
}
140140

141-
// expandUser replaces "~" in a string with the current user's home directory.
142-
func expandUser(path string) (string, error) {
141+
// ExpandUser replaces "~" in a string with the current user's home directory.
142+
func ExpandUser(path string) (string, error) {
143143
user, err := user.Current()
144144
if err != nil {
145145
return path, err
146146
}
147-
return strings.Replace(path, "~/", user.HomeDir, 1), nil
147+
return strings.Replace(path, "~/", user.HomeDir+"/", 1), nil
148148
}

cmd/cmd.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io/ioutil"
66
"net/http"
77
"os"
8-
"os/user"
98
"path/filepath"
109
"regexp"
1110
"strconv"
@@ -15,6 +14,7 @@ import (
1514
"github.com/deis/deisctl/constant"
1615
"github.com/deis/deisctl/update"
1716
"github.com/deis/deisctl/utils"
17+
"github.com/docopt/docopt-go"
1818
)
1919

2020
func ListUnits(c client.Client) error {
@@ -257,17 +257,26 @@ func Update() error {
257257
}
258258

259259
func RefreshUnits() error {
260-
// create the $HOME/.deisctl directory if necessary
261-
user, err := user.Current()
260+
usage := `Refreshes local unit files from the master repository.
261+
262+
Usage:
263+
deisctl refresh-units [-p <target>]
264+
265+
Options:
266+
-p --path=<target> where to save unit files [default: /var/lib/deis/units]
267+
`
268+
// parse command-line arguments
269+
args, err := docopt.Parse(usage, nil, true, "", false)
262270
if err != nil {
263-
return err
271+
fmt.Printf("Error: %v\n", err)
272+
os.Exit(2)
264273
}
265-
dir := filepath.Join(user.HomeDir, ".deisctl")
266-
if err = os.MkdirAll(dir, 0700); err != nil {
274+
dir, _ := client.ExpandUser(args["--path"].(string))
275+
// create the target dir if necessary
276+
if err := os.MkdirAll(dir, 0700); err != nil {
267277
return err
268278
}
269-
270-
// download and save the unit files to $HOME/.deisctl
279+
// download and save the unit files to the specified path
271280
rootURL := "https://raw.githubusercontent.com/deis/deisctl/"
272281
branch := "master"
273282
units := []string{

utils/utils.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ func GetKey(dir, key, perm string) string {
4646
return result.Node.Value
4747
}
4848

49-
// func GetNewClient() (
50-
// cli *client.DockerCli, stdout *io.PipeReader, stdoutPipe *io.PipeWriter) {
51-
// testDaemonAddr := "/var/run/docker.sock"
52-
// testDaemonProto := "unix"
53-
// stdout, stdoutPipe = io.Pipe()
54-
// cli = client.NewDockerCli(
55-
// nil, stdoutPipe, nil, testDaemonProto, testDaemonAddr, nil)
56-
// return
57-
// }
58-
5949
func PullImage(args string) error {
6050
cmdl := exec.Command("docker", "pull", args)
6151
if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil {
@@ -196,7 +186,6 @@ func getExitCode(err error) (int, error) {
196186
}
197187

198188
// RunCommandWithStdoutStderr execs a command and returns its output.
199-
200189
func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
201190
var stdout, stderr bytes.Buffer
202191
stderrPipe, err := cmd.StderrPipe()
@@ -267,5 +256,3 @@ func stripTrailingCharacters(target string) string {
267256
func nLines(s string) int {
268257
return strings.Count(s, "\n")
269258
}
270-
271-
//func deis(bash string , arg string , cmd string )

0 commit comments

Comments
 (0)