-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
sequelpro.go
126 lines (108 loc) · 3.46 KB
/
sequelpro.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cmd
import (
"fmt"
"github.com/drud/ddev/pkg/nodeps"
"os"
"os/exec"
"path/filepath"
"strconv"
"runtime"
"github.com/drud/ddev/pkg/ddevapp"
"github.com/drud/ddev/pkg/dockerutil"
"github.com/drud/ddev/pkg/output"
"github.com/drud/ddev/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// SequelproLoc is where we expect to find the sequel pro.app
// It's global so it can be mocked in testing.
var SequelproLoc = "/Applications/Sequel Pro.app"
// DdevSequelproCmd represents the sequelpro command
var DdevSequelproCmd = &cobra.Command{
Use: "sequelpro",
Short: "Connect sequelpro to a project database",
Long: `A helper command for using sequelpro (macOS database browser) with a running DDEV-Local project's database'.`,
Example: `ddev sequelpro`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
output.UserOut.Fatalf("invalid arguments to sequelpro command: %v", args)
}
out, err := handleSequelProCommand(SequelproLoc)
if err != nil {
output.UserOut.Fatalf("Could not run sequelpro command: %s", err)
}
util.Success(out)
},
}
// handleSequelProCommand() is the "real" handler for the real command
func handleSequelProCommand(appLocation string) (string, error) {
app, err := ddevapp.GetActiveApp("")
if err != nil {
return "", err
}
if app.SiteStatus() != ddevapp.SiteRunning {
return "", errors.New("project is not running. The project must be running to create a Sequel Pro connection")
}
db, err := app.FindContainerByType("db")
if err != nil {
return "", err
}
dbPrivatePort, err := strconv.ParseInt(ddevapp.GetPort("db"), 10, 64)
if err != nil {
return "", err
}
dbPublishPort := fmt.Sprint(dockerutil.GetPublishedPort(dbPrivatePort, *db))
tmpFilePath := filepath.Join(app.GetAppRoot(), ".ddev/sequelpro.spf")
tmpFile, err := os.Create(tmpFilePath)
if err != nil {
output.UserOut.Fatalln(err)
}
defer util.CheckClose(tmpFile)
dockerIP, err := dockerutil.GetDockerIP()
if err != nil {
return "", err
}
_, err = tmpFile.WriteString(fmt.Sprintf(
ddevapp.SequelproTemplate,
"db", //dbname
dockerIP, //host
app.HostName(), //connection name
"db", // dbpass
dbPublishPort, // port
"db", //dbuser
))
util.CheckErr(err)
err = exec.Command("open", tmpFilePath).Run()
if err != nil {
return "", err
}
return "sequelpro command finished successfully!", nil
}
// dummyDevSequelproCmd represents the "not available" sequelpro command
var dummyDevSequelproCmd = &cobra.Command{
Use: "sequelpro",
Short: "This command is not available since sequel pro.app is not installed",
Long: `Where installed, "ddev sequelpro" launches the sequel pro database browser`,
Run: func(cmd *cobra.Command, args []string) {
util.Failed("The sequelpro command is not available because sequel pro.app is not detected on your workstation")
},
}
// init installs the real command if it's available, otherwise dummy command (if on OSX), otherwise no command
func init() {
switch {
case detectSequelpro():
app, err := ddevapp.GetActiveApp("")
if err == nil && app != nil && !nodeps.ArrayContainsString(app.GetOmittedContainers(), "db") {
RootCmd.AddCommand(DdevSequelproCmd)
}
case runtime.GOOS == "darwin":
RootCmd.AddCommand(dummyDevSequelproCmd)
}
}
// detectSequelpro looks for the sequel pro app in /Applications; returns true if found
func detectSequelpro() bool {
if _, err := os.Stat(SequelproLoc); err == nil {
return true
}
return false
}