1// Copyright 2015 Google Inc. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5// Program aedeploy assists with deploying App Engine "flexible environment" Go apps to production.
6// A temporary directory is created; the app, its subdirectories, and all its
7// dependencies from $GOPATH are copied into the directory; then the app
8// is deployed to production with the provided command.
9//
10// The app must be in "package main".
11//
12// This command must be issued from within the root directory of the app
13// (where the app.yaml file is located).
14package main
15
16import (
17	"flag"
18	"fmt"
19	"log"
20	"os"
21	"os/exec"
22	"strings"
23)
24
25func usage() {
26	fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
27	fmt.Fprintf(os.Stderr, "\t%s gcloud --verbosity debug app deploy --version myversion ./app.yaml\tDeploy app to production\n", os.Args[0])
28}
29
30var verbose bool
31
32// vlogf logs to stderr if the "-v" flag is provided.
33func vlogf(f string, v ...interface{}) {
34	if !verbose {
35		return
36	}
37	log.Printf("[aedeploy] "+f, v...)
38}
39
40func main() {
41	flag.BoolVar(&verbose, "v", false, "Verbose logging.")
42	flag.Usage = usage
43	flag.Parse()
44	if flag.NArg() < 1 {
45		usage()
46		os.Exit(1)
47	}
48
49	notice := func() {
50		fmt.Fprintln(os.Stderr, `NOTICE: aedeploy is deprecated. Just use "gcloud app deploy".`)
51	}
52
53	notice()
54	if err := deploy(); err != nil {
55		fmt.Fprintf(os.Stderr, os.Args[0]+": Error: %v\n", err)
56		notice()
57		fmt.Fprintln(os.Stderr, `You might need to update gcloud. Run "gcloud components update".`)
58		os.Exit(1)
59	}
60	notice() // Make sure they see it at the end.
61}
62
63// deploy calls the provided command to deploy the app from the temporary directory.
64func deploy() error {
65	vlogf("Running command %v", flag.Args())
66	cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
67	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
68	if err := cmd.Run(); err != nil {
69		return fmt.Errorf("unable to run %q: %v", strings.Join(flag.Args(), " "), err)
70	}
71	return nil
72}
73