Skip to content

Commit

Permalink
Force port mapping (#3)
Browse files Browse the repository at this point in the history
Force port mapping
  • Loading branch information
Korbeil authored Nov 21, 2019
2 parents d0be688 + d1687e9 commit f5ba896
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pomdok:
projects:
- domain: 'api.project'
path: '/apps/api'
port: 9990
- domain: 'www.project'
path: '/apps/front'
- domain: 'admin.project'
Expand All @@ -38,6 +39,8 @@ pomdok:
You'll need at least `tld` field and one project to have a valid configuration.

For each "project" you have, you'll need at least a `domain` and `port` fields. `port` field is optional and used to force a given port for your webserver.

To init `pomdok` for your project run:
```bash
pomdok init
Expand Down
6 changes: 5 additions & 1 deletion generics.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package main

type SymfonyJsonProxy struct {
// SymfonyJSONProxy is a representation of Symfony CLI's configuration file
type SymfonyJSONProxy struct {
Tld string `json:"tld"`
Port int `json:"port"`
Domains map[string]string `json:"domains"`
Ports map[string]int `json:"ports"`
}

// PomdokYamlConfig is a representation of Pomdok's configuration file
type PomdokYamlConfig struct {
Pomdok struct {
Tld string
Projects []struct {
Domain string `yaml:"domain"`
Path string `yaml:"path"`
Port int `yaml:"port"`
}
}
}
38 changes: 27 additions & 11 deletions initCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -27,14 +28,12 @@ var initCommand = &cli.Command{
printHeader()

argv := ctx.Argv().(*initT)
file := findFileUp(argv.Config, 0)
if file == "" {
config, baseDirectory, err := loadPomdokConfig(argv.Config)
if nil != err {
fmt.Println(err)
return nil
}

data, _ := ioutil.ReadFile(file)
config := PomdokYamlConfig{}
yaml.Unmarshal([]byte(data), &config)
if config.Pomdok.Tld == "" {
fmt.Printf("Configuration file error 🙊. Maybe you should give a %s to your domains 🧐\n", yellow("tld"))
return nil
Expand All @@ -45,7 +44,7 @@ var initCommand = &cli.Command{
}

fileDomains := make(map[string]string)
baseDirectory := path.Dir(file)
filePorts := make(map[string]int)
for _, element := range config.Pomdok.Projects {
if element.Domain == "" {
fmt.Printf("Configuration file error 🙊. One of the project has empty/no %s 🧐\n", yellow("domain"))
Expand All @@ -66,15 +65,18 @@ var initCommand = &cli.Command{
fmt.Printf("Configuration file error 🙊. Domain %s is used more than one time 🧐\n", yellow(element.Domain))
return nil
}

fileDomains[element.Domain] = fullPath
filePorts[element.Domain] = element.Port
}

symfonyJsonData := SymfonyJsonProxy{
symfonyJSONData := SymfonyJSONProxy{
Tld: config.Pomdok.Tld,
Port: 7080,
Domains: fileDomains,
Ports: filePorts,
}
symfonyJson, _ := json.MarshalIndent(symfonyJsonData, "", " ")
symfonyJSON, _ := json.MarshalIndent(symfonyJSONData, "", " ")

currentUser, _ := user.Current()

Expand All @@ -84,20 +86,34 @@ var initCommand = &cli.Command{
return nil
}

symfonyDirUserUid := fmt.Sprint((info.Sys().(*syscall.Stat_t)).Uid)
symfonyDirUser, _ := user.LookupId(symfonyDirUserUid)
symfonyDirUserUID := fmt.Sprint((info.Sys().(*syscall.Stat_t)).Uid)
symfonyDirUser, _ := user.LookupId(symfonyDirUserUID)
if symfonyDirUser.Username != currentUser.Username {
fmt.Printf("Permission error 🙊. Directory ~/.symfony is owned by %s, please use: 'sudo chown -R %s ~/.symfony' 🧐\n", yellow(symfonyDirUser.Username), currentUser.Username)
return nil
}

ioutil.WriteFile(fmt.Sprintf("%s/.symfony/proxy.json", currentUser.HomeDir), symfonyJson, 0644)
ioutil.WriteFile(fmt.Sprintf("%s/.symfony/proxy.json", currentUser.HomeDir), symfonyJSON, 0644)
fmt.Printf("Project setup done ✔\n")

return nil
},
}

func loadPomdokConfig(fileName string) (PomdokYamlConfig, string, error) {
config := PomdokYamlConfig{}

file := findFileUp(fileName, 0)
if file == "" {
return config, "", errors.New("No file found")
}

data, _ := ioutil.ReadFile(file)
yaml.Unmarshal([]byte(data), &config)

return config, path.Dir(file), nil
}

func findFileUp(file string, level int) string {
temp := file
if level > 0 {
Expand Down
2 changes: 1 addition & 1 deletion rootCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type appT struct {

var app = appT{
"pomdok",
"v1.0.0",
"v1.1.0",
}

func sprintHeader() string {
Expand Down
21 changes: 14 additions & 7 deletions startAndStopCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ var stopCommand = &cli.Command{
Fn: func(ctx *cli.Context) error {
printHeader()
startOrStopCommand("local:server:stop", "stopped 🛑")
runCommand("/usr/local/bin/symfony proxy:stop")
runCommand("symfony proxy:stop")

return nil
},
}

func startOrStopCommand(command string, message string) {
if false == symfonyProxyRunning() {
runCommand("/usr/local/bin/symfony proxy:start")
runCommand("symfony proxy:start")
fmt.Print("Started Symfony proxy server 👮\n")
}

Expand All @@ -58,12 +58,19 @@ func startOrStopCommand(command string, message string) {
}
file, _ := ioutil.ReadFile(symfonyProxyConfigPah)

symfonyJsonData := SymfonyJsonProxy{}
json.Unmarshal(file, &symfonyJsonData)
symfonyJSONData := SymfonyJSONProxy{}
json.Unmarshal(file, &symfonyJSONData)

for domain, path := range symfonyJsonData.Domains {
runCommand(fmt.Sprintf("/usr/local/bin/symfony %s --dir=%s", command, path))
fmt.Printf("%s %s\n", message, yellow(fmt.Sprintf("%s.%s", domain, symfonyJsonData.Tld)))
for domain, path := range symfonyJSONData.Domains {
forcedPort := symfonyJSONData.Ports[domain]
formattedCommand := fmt.Sprintf("symfony %s --dir=%s", command, path)

if "local:server:start --daemon" == command && 0 != forcedPort {
formattedCommand = fmt.Sprintf("symfony %s --port=%d --dir=%s", command, forcedPort, path)
}

runCommand(formattedCommand)
fmt.Printf("%s %s\n", message, yellow(fmt.Sprintf("%s.%s", domain, symfonyJSONData.Tld)))
}

return
Expand Down

0 comments on commit f5ba896

Please sign in to comment.