Added env-file flag, moved error logging more general, added logrus, moved code to plugin file

This commit is contained in:
Thomas Boerger 2016-08-22 21:49:28 +02:00
parent ecf92bf1e9
commit f8e8803850
No known key found for this signature in database
GPG Key ID: 5A388F55283960B6
2 changed files with 37 additions and 40 deletions

56
main.go
View File

@ -1,19 +1,14 @@
package main package main
import ( import (
"fmt"
"os" "os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/joho/godotenv"
"github.com/urfave/cli" "github.com/urfave/cli"
_ "github.com/joho/godotenv/autoload"
) )
// build number set at compile-time var version string // build number set at compile-time
var version string
// default docker registry
const defaultRegistry = "https://index.docker.io/v1/"
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
@ -22,21 +17,17 @@ func main() {
app.Action = run app.Action = run
app.Version = version app.Version = version
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "dry-run", Name: "dry-run",
Usage: "dry run disables docker push", Usage: "dry run disables docker push",
EnvVar: "PLUGIN_DRY_RUN", EnvVar: "PLUGIN_DRY_RUN",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "commit.sha", Name: "commit.sha",
Usage: "git commit sha", Usage: "git commit sha",
EnvVar: "DRONE_COMMIT_SHA", EnvVar: "DRONE_COMMIT_SHA",
Value: "00000000", Value: "00000000",
}, },
// daemon parameters
cli.StringFlag{ cli.StringFlag{
Name: "daemon.mirror", Name: "daemon.mirror",
Usage: "docker daemon registry mirror", Usage: "docker daemon registry mirror",
@ -83,9 +74,6 @@ func main() {
Usage: "docker daemon executes in debug mode", Usage: "docker daemon executes in debug mode",
EnvVar: "PLUGIN_DAEMON_OFF", EnvVar: "PLUGIN_DAEMON_OFF",
}, },
// build parameters
cli.StringFlag{ cli.StringFlag{
Name: "dockerfile", Name: "dockerfile",
Usage: "build dockerfile", Usage: "build dockerfile",
@ -114,8 +102,6 @@ func main() {
Usage: "docker repository", Usage: "docker repository",
EnvVar: "PLUGIN_REPO", EnvVar: "PLUGIN_REPO",
}, },
// secret variables
cli.StringFlag{ cli.StringFlag{
Name: "docker.registry", Name: "docker.registry",
Usage: "docker username", Usage: "docker username",
@ -137,12 +123,22 @@ func main() {
Usage: "docker email", Usage: "docker email",
EnvVar: "DOCKER_EMAIL,PLUGIN_EMAIL", EnvVar: "DOCKER_EMAIL,PLUGIN_EMAIL",
}, },
cli.StringFlag{
Name: "env-file",
Usage: "source env file",
},
} }
app.Run(os.Args) if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func run(c *cli.Context) error {
if c.String("env-file") != "" {
_ = godotenv.Load(c.String("env-file"))
} }
func run(c *cli.Context) {
plugin := Plugin{ plugin := Plugin{
Dryrun: c.Bool("dry-run"), Dryrun: c.Bool("dry-run"),
Login: Login{ Login: Login{
@ -173,25 +169,5 @@ func run(c *cli.Context) {
}, },
} }
// this code attempts to normalize the repository name by appending the fully return plugin.Exec()
// qualified registry name if otherwise omitted.
if plugin.Login.Registry != defaultRegistry &&
strings.HasPrefix(plugin.Build.Repo, defaultRegistry) {
plugin.Build.Repo = plugin.Login.Registry + "/" + plugin.Build.Repo
} }
if err := plugin.Exec(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// TODO execute code remove dangling images
// this is problematic because we are running docker in scratch which does
// not have bash, so we need to hack something together
// docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi
}
/*
cmd = exec.Command("docker", "images", "-q", "-f", "dangling=true")
cmd = exec.Command("docker", append([]string{"rmi"}, images...)...)
*/

View File

@ -9,6 +9,11 @@ import (
"time" "time"
) )
const (
// default docker registry
defaultRegistry = "https://index.docker.io/v1/"
)
type ( type (
// Daemon defines Docker daemon parameters. // Daemon defines Docker daemon parameters.
Daemon struct { Daemon struct {
@ -53,6 +58,22 @@ type (
// Exec executes the plugin step // Exec executes the plugin step
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
// this code attempts to normalize the repository name by appending the fully
// qualified registry name if otherwise omitted.
if p.Login.Registry != defaultRegistry &&
strings.HasPrefix(p.Build.Repo, defaultRegistry) {
p.Build.Repo = p.Login.Registry + "/" + p.Build.Repo
}
// TODO execute code remove dangling images
// this is problematic because we are running docker in scratch which does
// not have bash, so we need to hack something together
// docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi
/*
cmd = exec.Command("docker", "images", "-q", "-f", "dangling=true")
cmd = exec.Command("docker", append([]string{"rmi"}, images...)...)
*/
// start the Docker daemon server // start the Docker daemon server
if !p.Daemon.Disabled { if !p.Daemon.Disabled {