From d3eb367d845c24bf4749ce19c0275cbaef3832c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Fri, 12 Jun 2015 01:09:02 +0200 Subject: [PATCH] Smaller docker image --- .gitignore | 4 +- Dockerfile | 9 +- Godeps/Godeps.json | 4 + .../github.com/mitchellh/go-homedir/LICENSE | 21 ++++ .../github.com/mitchellh/go-homedir/README.md | 14 +++ .../mitchellh/go-homedir/homedir.go | 84 ++++++++++++++++ .../mitchellh/go-homedir/homedir_test.go | 98 +++++++++++++++++++ commands/name_pending.go | 6 +- docker.sh | 4 + 9 files changed, 235 insertions(+), 9 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go create mode 100755 docker.sh diff --git a/.gitignore b/.gitignore index d6aca9ab..d1063d19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ +build client/dist -client/node_modules \ No newline at end of file +client/node_modules +ca-certificates.crt \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index c7f82981..d4e20578 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,9 @@ -FROM golang +FROM scratch -ADD . /go/src/github.com/khlieng/name_pending - -RUN go install github.com/khlieng/name_pending +ADD build/name_pending / +ADD ca-certificates.crt /etc/ssl/certs/ VOLUME ["/data"] -ENTRYPOINT ["/go/bin/name_pending"] +ENTRYPOINT ["/name_pending"] CMD ["-p=8080", "--dir=/data"] \ No newline at end of file diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index af073805..e93fea4b 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -67,6 +67,10 @@ "Comment": "v1.5.2", "Rev": "d5929c67198951106f49f7ea425198d0f1a08f7f" }, + { + "ImportPath": "github.com/mitchellh/go-homedir", + "Rev": "1f6da4a72e57d4e7edd4a7295a585e0a3999a2d4" + }, { "ImportPath": "github.com/mitchellh/mapstructure", "Rev": "f7d28d5aeab42b9b95d2e6d6b956f73a290077fc" diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE new file mode 100644 index 00000000..f9c841a5 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Mitchell Hashimoto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md new file mode 100644 index 00000000..d70706d5 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md @@ -0,0 +1,14 @@ +# go-homedir + +This is a Go library for detecting the user's home directory without +the use of cgo, so the library can be used in cross-compilation environments. + +Usage is incredibly simple, just call `homedir.Dir()` to get the home directory +for a user, and `homedir.Expand()` to expand the `~` in a path to the home +directory. + +**Why not just use `os/user`?** The built-in `os/user` package requires +cgo on Darwin systems. This means that any Go code that uses that package +cannot cross compile. But 99% of the time the use for `os/user` is just to +retrieve the home directory, which we can do for the current user without +cgo. This library does that, enabling cross-compilation. diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go new file mode 100644 index 00000000..051f1116 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go @@ -0,0 +1,84 @@ +package homedir + +import ( + "bytes" + "errors" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" +) + +// Dir returns the home directory for the executing user. +// +// This uses an OS-specific method for discovering the home directory. +// An error is returned if a home directory cannot be detected. +func Dir() (string, error) { + if runtime.GOOS == "windows" { + return dirWindows() + } + + // Unix-like system, so just assume Unix + return dirUnix() +} + +// Expand expands the path to include the home directory if the path +// is prefixed with `~`. If it isn't prefixed with `~`, the path is +// returned as-is. +func Expand(path string) (string, error) { + if len(path) == 0 { + return path, nil + } + + if path[0] != '~' { + return path, nil + } + + if len(path) > 1 && path[1] != '/' && path[1] != '\\' { + return "", errors.New("cannot expand user-specific home dir") + } + + dir, err := Dir() + if err != nil { + return "", err + } + + return filepath.Join(dir, path[1:]), nil +} + +func dirUnix() (string, error) { + // First prefer the HOME environmental variable + if home := os.Getenv("HOME"); home != "" { + return home, nil + } + + // If that fails, try the shell + var stdout bytes.Buffer + cmd := exec.Command("sh", "-c", "eval echo ~$USER") + cmd.Stdout = &stdout + if err := cmd.Run(); err != nil { + return "", err + } + + result := strings.TrimSpace(stdout.String()) + if result == "" { + return "", errors.New("blank output when reading home directory") + } + + return result, nil +} + +func dirWindows() (string, error) { + drive := os.Getenv("HOMEDRIVE") + path := os.Getenv("HOMEPATH") + home := drive + path + if drive == "" || path == "" { + home = os.Getenv("USERPROFILE") + } + if home == "" { + return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank") + } + + return home, nil +} diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go new file mode 100644 index 00000000..ddc24ee0 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go @@ -0,0 +1,98 @@ +package homedir + +import ( + "fmt" + "os" + "os/user" + "testing" +) + +func patchEnv(key, value string) func() { + bck := os.Getenv(key) + deferFunc := func() { + os.Setenv(key, bck) + } + + os.Setenv(key, value) + return deferFunc +} + +func TestDir(t *testing.T) { + u, err := user.Current() + if err != nil { + t.Fatalf("err: %s", err) + } + + dir, err := Dir() + if err != nil { + t.Fatalf("err: %s", err) + } + + if u.HomeDir != dir { + t.Fatalf("%#v != %#v", u.HomeDir, dir) + } +} + +func TestExpand(t *testing.T) { + u, err := user.Current() + if err != nil { + t.Fatalf("err: %s", err) + } + + cases := []struct { + Input string + Output string + Err bool + }{ + { + "/foo", + "/foo", + false, + }, + + { + "~/foo", + fmt.Sprintf("%s/foo", u.HomeDir), + false, + }, + + { + "", + "", + false, + }, + + { + "~", + u.HomeDir, + false, + }, + + { + "~foo/foo", + "", + true, + }, + } + + for _, tc := range cases { + actual, err := Expand(tc.Input) + if (err != nil) != tc.Err { + t.Fatalf("Input: %#v\n\nErr: %s", tc.Input, err) + } + + if actual != tc.Output { + t.Fatalf("Input: %#v\n\nOutput: %#v", tc.Input, actual) + } + } + + defer patchEnv("HOME", "/custom/path/")() + expected := "/custom/path/foo/bar" + actual, err := Expand("~/foo/bar") + + if err != nil { + t.Errorf("No error is expected, got: %v", err) + } else if actual != "/custom/path/foo/bar" { + t.Errorf("Expected: %v; actual: %v", expected, actual) + } +} diff --git a/commands/name_pending.go b/commands/name_pending.go index 14211285..a2e0fc2c 100644 --- a/commands/name_pending.go +++ b/commands/name_pending.go @@ -4,9 +4,9 @@ import ( "io/ioutil" "log" "os" - "os/user" "path" + "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/mitchellh/go-homedir" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/viper" @@ -74,10 +74,10 @@ func initConfig() { } func defaultDir() string { - currentUser, err := user.Current() + dir, err := homedir.Dir() if err != nil { log.Fatal(err) } - return path.Join(currentUser.HomeDir, ".name_pending") + return path.Join(dir, ".name_pending") } diff --git a/docker.sh b/docker.sh new file mode 100755 index 00000000..0305eb7a --- /dev/null +++ b/docker.sh @@ -0,0 +1,4 @@ +#!/bin/sh +cp /etc/ssl/certs/ca-certificates.crt . +CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o build/name_pending +docker build -t khlieng/name_pending . \ No newline at end of file