Skip to content

Commit 0c0d432

Browse files
authored
Tunnel refactoring (#74)
* Refactor tunnel handling * Move code around
1 parent 642a999 commit 0c0d432

File tree

7 files changed

+84
-92
lines changed

7 files changed

+84
-92
lines changed

commands/local_var_expose.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/symfony-cli/console"
2626
"github.com/symfony-cli/symfony-cli/envs"
27+
"github.com/symfony-cli/symfony-cli/local/platformsh"
2728
"github.com/symfony-cli/terminal"
2829
)
2930

@@ -45,9 +46,11 @@ var localVariableExposeFromTunnelCmd = &console.Command{
4546
}
4647
}
4748

48-
tunnel := envs.Tunnel{
49-
Dir: dir,
49+
project, err := platformsh.ProjectFromDir(dir, false)
50+
if err != nil {
51+
return err
5052
}
53+
tunnel := envs.Tunnel{Project: project}
5154

5255
if c.Bool("off") {
5356
terminal.Eprintln("Stop exposing tunnel service environment variables ")

envs/local.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,17 @@ func (l *Local) Relationships() Relationships {
9090
// we need to call it in all cases so that l.DockerEnv is set correctly
9191
dockerRel := l.RelationshipsFromDocker()
9292

93-
tunnel := Tunnel{Dir: l.Dir, Debug: l.Debug}
93+
project, err := platformsh.ProjectFromDir(l.Dir, l.Debug)
94+
if err != nil {
95+
if l.Debug {
96+
fmt.Fprint(os.Stderr, "ERROR: unable to get Platform.sh project information\n")
97+
}
98+
return dockerRel
99+
}
100+
tunnel := Tunnel{
101+
Project: project,
102+
Debug: l.Debug,
103+
}
94104
if !tunnel.IsExposed() {
95105
return dockerRel
96106
}
@@ -139,7 +149,7 @@ func (l *Local) Language() string {
139149
if language != "" {
140150
return language
141151
}
142-
projectRoot, err := util.GetProjectRoot(l.Debug)
152+
projectRoot, err := platformsh.GetProjectRoot(l.Debug)
143153
if err != nil {
144154
if l.Debug {
145155
fmt.Fprint(os.Stderr, "ERROR: unable to get project root\n")

envs/local_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path/filepath"
2525

2626
"github.com/mitchellh/go-homedir"
27+
"github.com/symfony-cli/symfony-cli/local/platformsh"
2728
. "gopkg.in/check.v1"
2829
)
2930

@@ -46,9 +47,12 @@ func (s *LocalSuite) TestTunnelFilePath(c *C) {
4647
defer func() {
4748
os.Rename("testdata/project/.git", "testdata/project/git")
4849
}()
49-
tunnel := Tunnel{Dir: l.Dir}
50-
tunnelPath, _ := tunnel.computePath()
51-
c.Assert(filepath.Base(tunnelPath), Equals, "ism4mega7wpx6-toto--security.json")
50+
project, err := platformsh.ProjectFromDir(l.Dir, false)
51+
if err != nil {
52+
panic(err)
53+
}
54+
tunnel := Tunnel{Project: project}
55+
c.Assert(filepath.Base(tunnel.path()), Equals, "ism4mega7wpx6-toto--security-expose.json")
5256
}
5357

5458
func (s *LocalSuite) TestRelationships(c *C) {

envs/local_tunnel.go

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"strconv"
3232

3333
"github.com/mitchellh/go-homedir"
34-
"github.com/pkg/errors"
3534
"github.com/symfony-cli/symfony-cli/local/platformsh"
3635
"github.com/symfony-cli/symfony-cli/util"
3736
)
@@ -46,18 +45,10 @@ type pshtunnel struct {
4645
}
4746

4847
func (l *Local) relationshipsFromTunnel() Relationships {
49-
projectRoot := util.RepositoryRootDir(l.Dir)
50-
envID, err := util.PotentialCurrentEnvironmentID(projectRoot)
48+
project, err := platformsh.ProjectFromDir(l.Dir, l.Debug)
5149
if err != nil {
5250
if l.Debug {
53-
fmt.Fprintf(os.Stderr, "WARNING: unable to find the env: %s\n", err)
54-
}
55-
return nil
56-
}
57-
app := platformsh.GuessSelectedAppByDirectory(l.Dir, platformsh.FindLocalApplications(projectRoot))
58-
if app == nil {
59-
if l.Debug {
60-
fmt.Fprintf(os.Stderr, "WARNING: unable to find the app: %s\n", err)
51+
fmt.Fprintf(os.Stderr, "WARNING: unable to detect Platform.sh project: %s\n", err)
6152
}
6253
return nil
6354
}
@@ -88,16 +79,9 @@ func (l *Local) relationshipsFromTunnel() Relationships {
8879
tunnels = append(tunnels, config)
8980
}
9081
}
91-
gitConfig := util.GetProjectConfig(projectRoot, l.Debug)
92-
if gitConfig == nil {
93-
if l.Debug {
94-
fmt.Fprintf(os.Stderr, "WARNING: unable to read Git config: %s\n", err)
95-
}
96-
return nil
97-
}
9882
rels := make(Relationships)
9983
for _, config := range tunnels {
100-
if config.ProjectID == gitConfig.ID && config.EnvironmentID == envID && config.AppName == app.Name {
84+
if config.ProjectID == project.ID && config.EnvironmentID == project.Env && config.AppName == project.App {
10185
config.Service["port"] = strconv.Itoa(config.LocalPort)
10286
config.Service["host"] = "127.0.0.1"
10387
config.Service["ip"] = "127.0.0.1"
@@ -106,7 +90,7 @@ func (l *Local) relationshipsFromTunnel() Relationships {
10690
}
10791

10892
if len(rels) > 0 {
109-
l.Tunnel = envID
93+
l.Tunnel = project.Env
11094
l.TunnelEnv = true
11195
return rels
11296
}
@@ -117,83 +101,54 @@ func (l *Local) relationshipsFromTunnel() Relationships {
117101
var pathCleaningRegex = regexp.MustCompile("[^a-zA-Z0-9-\\.]+")
118102

119103
type Tunnel struct {
120-
Dir string
121-
Worker string
122-
Debug bool
123-
path string
104+
Project *platformsh.Project
105+
Worker string
106+
Debug bool
124107
}
125108

126109
func (t *Tunnel) IsExposed() bool {
127-
path, err := t.computePath()
128-
if err != nil {
129-
return false
130-
}
131-
if _, err := os.Stat(path + "-expose"); err != nil {
110+
if _, err := os.Stat(t.path()); err != nil {
132111
return false
133112
}
134113
return true
135114
}
136115

137116
func (t *Tunnel) Expose(expose bool) error {
138-
path, err := t.computePath()
139-
if err != nil {
140-
return err
141-
}
142-
117+
path := t.path()
143118
if expose {
144119
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
145120
return err
146121
}
147-
file, err := os.Create(path + "-expose")
122+
file, err := os.Create(path)
148123
if err != nil {
149124
return err
150125
}
151126
return file.Close()
152127
}
153128

154-
os.Remove(path + "-expose")
129+
os.Remove(path)
155130
return nil
156131
}
157132

158133
// Path returns the path to the Platform.sh local tunnel state file
159-
func (t *Tunnel) computePath() (string, error) {
160-
if t.path != "" {
161-
return t.path, nil
162-
}
163-
projectRoot, projectInfo := util.GuessProjectRoot(t.Dir, t.Debug)
164-
if projectInfo == nil {
165-
return "", errors.New("unable to get project root")
166-
}
167-
envID, err := util.PotentialCurrentEnvironmentID(projectRoot)
168-
if err != nil {
169-
return "", errors.Wrap(err, "unable to get current env")
170-
}
171-
app := platformsh.GuessSelectedAppByDirectory(t.Dir, platformsh.FindLocalApplications(projectRoot))
172-
if app == nil {
173-
return "", errors.New("unable to get current application")
174-
}
175-
t.path = getControlFileName(filepath.Join(util.GetHomeDir(), "tunnels"), projectInfo.ID, envID, app.Name, t.Worker)
176-
return t.path, nil
177-
}
178-
179-
func getControlFileName(dir, projectID, envID, appName, workerName string) string {
134+
func (t *Tunnel) path() string {
180135
var filename bytes.Buffer
181136

182-
filename.WriteString(projectID)
137+
filename.WriteString(t.Project.ID)
183138
filename.WriteRune('-')
184-
filename.WriteString(envID)
139+
filename.WriteString(t.Project.Env)
185140

186-
if appName != "" {
141+
if t.Project.App != "" {
187142
filename.WriteString("--")
188-
filename.WriteString(appName)
143+
filename.WriteString(t.Project.App)
189144
}
190145

191-
if workerName != "" {
146+
if t.Worker != "" {
192147
filename.WriteString("--")
193-
filename.WriteString(workerName)
148+
filename.WriteString(t.Worker)
194149
}
195150

196-
filename.WriteString(".json")
151+
filename.WriteString("-expose.json")
197152

198-
return filepath.Join(dir, pathCleaningRegex.ReplaceAllString(path.Clean(filename.String()), "-"))
153+
return filepath.Join(filepath.Join(util.GetHomeDir(), "tunnels"), pathCleaningRegex.ReplaceAllString(path.Clean(filename.String()), "-"))
199154
}

util/project.go renamed to local/platformsh/project.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
package util
20+
package platformsh
2121

2222
import (
2323
goerr "errors"
@@ -36,28 +36,54 @@ var (
3636
ErrNoGitBranchMatching = goerr.New("current git branch name doesn't match any Platform.sh environments")
3737
)
3838

39+
type Project struct {
40+
ID string
41+
App string
42+
Env string
43+
}
44+
45+
func ProjectFromDir(dir string, debug bool) (*Project, error) {
46+
projectRoot, projectID := guessProjectRoot(dir, debug)
47+
if projectID == "" {
48+
return nil, errors.New("unable to get project root")
49+
}
50+
envID, err := potentialCurrentEnvironmentID(projectRoot)
51+
if err != nil {
52+
return nil, errors.Wrap(err, "unable to get current env")
53+
}
54+
app := GuessSelectedAppByDirectory(dir, FindLocalApplications(projectRoot))
55+
if app == nil {
56+
return nil, errors.New("unable to get current application")
57+
}
58+
return &Project{
59+
ID: projectID,
60+
App: app.Name,
61+
Env: envID,
62+
}, nil
63+
}
64+
3965
func GetProjectRoot(debug bool) (string, error) {
4066
currentDir, err := os.Getwd()
4167
if err != nil {
4268
return "", errors.WithStack(err)
4369
}
4470

45-
if projectRoot, _ := GuessProjectRoot(currentDir, debug); projectRoot != "" {
71+
if projectRoot, _ := guessProjectRoot(currentDir, debug); projectRoot != "" {
4672
return projectRoot, nil
4773
}
4874

4975
return "", errors.WithStack(ErrProjectRootNotFoundNoGitRemote)
5076
}
5177

52-
func PotentialCurrentEnvironmentID(cwd string) (string, error) {
78+
func potentialCurrentEnvironmentID(cwd string) (string, error) {
5379
for _, potentialEnvironment := range guessCloudBranch(cwd) {
5480
return potentialEnvironment, nil
5581
}
5682

5783
return "", errors.New("no known git upstream, branch or environment name")
5884
}
5985

60-
func RepositoryRootDir(currentDir string) string {
86+
func repositoryRootDir(currentDir string) string {
6187
for {
6288
f, err := os.Stat(filepath.Join(currentDir, ".git"))
6389
if err == nil && f.IsDir() {
@@ -74,29 +100,25 @@ func RepositoryRootDir(currentDir string) string {
74100
return ""
75101
}
76102

77-
func GuessProjectRoot(currentDir string, debug bool) (string, *gitInfo) {
78-
rootDir := RepositoryRootDir(currentDir)
103+
func guessProjectRoot(currentDir string, debug bool) (string, string) {
104+
rootDir := repositoryRootDir(currentDir)
79105
if rootDir == "" {
80-
return "", nil
106+
return "", ""
81107
}
82-
config := GetProjectConfig(rootDir, debug)
83-
if config == nil {
84-
return "", nil
108+
config := getProjectConfig(rootDir, debug)
109+
if config == "" {
110+
return "", ""
85111
}
86112
return rootDir, config
87113
}
88114

89-
type gitInfo struct {
90-
ID string
91-
}
92-
93-
func GetProjectConfig(projectRoot string, debug bool) *gitInfo {
115+
func getProjectConfig(projectRoot string, debug bool) string {
94116
contents, err := ioutil.ReadFile(filepath.Join(projectRoot, ".platform", "local", "project.yaml"))
95117
if err != nil {
96118
if debug {
97119
fmt.Fprintf(os.Stderr, "WARNING: unable to find Platform.sh config file: %s\n", err)
98120
}
99-
return nil
121+
return ""
100122
}
101123
var config struct {
102124
ID string `yaml:"id"`
@@ -105,11 +127,9 @@ func GetProjectConfig(projectRoot string, debug bool) *gitInfo {
105127
if debug {
106128
fmt.Fprintf(os.Stderr, "ERROR: unable to decode Platform.sh config file: %s\n", err)
107129
}
108-
return nil
109-
}
110-
return &gitInfo{
111-
ID: config.ID,
130+
return ""
112131
}
132+
return config.ID
113133
}
114134

115135
func guessCloudBranch(cwd string) []string {

0 commit comments

Comments
 (0)