-
Notifications
You must be signed in to change notification settings - Fork 7
/
project.go
105 lines (88 loc) · 2.84 KB
/
project.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package dev
import (
"fmt"
"os"
"strings"
log "github.com/sirupsen/logrus"
c "github.com/wish/dev/config"
"github.com/wish/dev/docker"
)
// Project is the group of functionality provided by a docker-compose file.
type Project struct {
Name string
Config *c.Project
}
//type ProjectConstructor func(config *c.Project) *Project
// NewProject is the Project constructor.
func NewProject(config *c.Project) *Project {
return &Project{
Name: config.Name,
Config: config,
}
}
// PreRun implements the Dependency interface. It brings up the project prior
// to the shell and up commads.
func (p *Project) PreRun(command string, appConfig *c.Dev, project *Project) {
if !SliceContainsString([]string{UP, SH}, command) {
return
}
p.Up(appConfig)
}
// Dependencies implements the Dependency interface. It returns a list of
// the names of its dependencies. These can be names of other projects,
// networks or registries.
func (p *Project) Dependencies() []string {
return p.Config.Dependencies
}
// GetName returns the name of the project as configured by the user in the dev
// configuration file.
func (p *Project) GetName() string {
return p.Name
}
// Up brings up the specified project container with its dependencies.
func (p *Project) Up(appConfig *c.Dev) {
RunComposeUp(appConfig.ImagePrefix, p.Config.DockerComposeFilenames, "-d", "--no-build")
}
// UpFollowProjectLogs brings up the specified project with its dependencies
// and tails the logs of the project container.
func (p *Project) UpFollowProjectLogs(appConfig *c.Dev) {
p.Up(appConfig)
RunComposeLogs(appConfig.ImagePrefix, p.Config.DockerComposeFilenames, "-f", p.Config.Name)
}
// Shell runs commands or creates an interfactive shell on the Project
// container.
func (p *Project) Shell(appConfig *c.Dev, args []string) {
running, err := docker.IsContainerRunning(p.Config.Name)
if err != nil {
log.Fatalf("Error communicating with docker daemon, is it up? %s", err)
}
if !running {
log.Infof("Project %s not running, bringing it up", p.Config.Name)
p.Up(appConfig)
}
// Get current directory, attempt to find its location
// on the container and cd to it. This allows developers to
// use relative directories like they would in a non-containerized
// development environment.
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get current directory: %s", err)
}
relativePath := ""
configDir := p.Config.Directory
if strings.HasPrefix(cwd, configDir) {
start := strings.Index(cwd, configDir) + len(configDir) + 1
if start < len(cwd) {
relativePath = cwd[start:]
} else {
relativePath = "."
}
}
if len(args) == 0 {
// no subcommands, so just provide a shell
args = append(args, p.Config.Shell)
}
cmdLine := []string{p.Config.Shell, "-c",
fmt.Sprintf("cd %s ; %s", relativePath, strings.Join(args, " "))}
RunOnContainer(p.Name, cmdLine...)
}