This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
forked from Azure/draft-classic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
up.go
84 lines (72 loc) · 2.05 KB
/
up.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"io"
"os"
"github.com/Azure/draft/pkg/build"
"github.com/Azure/draft/pkg/draft"
"github.com/Azure/draft/pkg/draft/manifest"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
const upDesc = `
This command archives the current directory into a tar archive and uploads it to
the draft server.
Adding the "watch" option to draft.toml makes draft automatically archive and
upload whenever local files are saved. Draft delays a couple seconds to ensure
that changes have stopped before uploading, but that can be altered by the
"watch_delay" option.
`
const (
environmentEnvVar = "DRAFT_ENV"
ignoreFileName = ".draftignore"
)
type upCmd struct {
client *draft.Client
out io.Writer
src string
}
func newUpCmd(out io.Writer) *cobra.Command {
var (
up = &upCmd{out: out}
runningEnvironment string
)
cmd := &cobra.Command{
Use: "up [path]",
Short: "upload the current directory to the draft server for deployment",
Long: upDesc,
PreRunE: setupConnection,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
up.src = args[0]
}
up.client = ensureDraftClient(up.client)
if up.src == "" || up.src == "." {
if up.src, err = os.Getwd(); err != nil {
return err
}
}
return up.run(runningEnvironment)
},
}
f := cmd.Flags()
f.StringVarP(&runningEnvironment, "environment", "e", defaultDraftEnvironment(), "the environment (development, staging, qa, etc) that draft will run under")
return cmd
}
func (u *upCmd) run(environment string) (err error) {
var buildctx *build.Context
if buildctx, err = build.LoadWithEnv(u.src, environment); err != nil {
return fmt.Errorf("failed loading build context with env %q: %v", environment, err)
}
if err = u.client.Up(context.Background(), buildctx); err != nil {
return fmt.Errorf("there was an error running 'draft up': %v", err)
}
return nil
}
func defaultDraftEnvironment() string {
env := os.Getenv(environmentEnvVar)
if env == "" {
env = manifest.DefaultEnvironmentName
}
return env
}