Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev 1.5.0 alpha.0 #116

Open
wants to merge 10 commits into
base: dev-1.5.0-alpha.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94 h1:0ngsPmuP6XIjiFRN
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
<<<<<<< HEAD
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
<<<<<<< HEAD
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
=======
>>>>>>> 162f157ccc313c23936308015ce597c8cfde28ec
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
=======
>>>>>>> c2810dbc64957c362c5d3fda51fde652253f679c
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
Expand Down Expand Up @@ -101,10 +111,19 @@ golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpbl
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190515120540-06a5c4944438 h1:khxRGsvPk4n2y8I/mLLjp7e5dMTJmH75wvqS6nMwUtY=
golang.org/x/sys v0.0.0-20190515120540-06a5c4944438/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
<<<<<<< HEAD
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
=======
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
<<<<<<< HEAD
>>>>>>> 162f157ccc313c23936308015ce597c8cfde28ec
golang.org/x/tools v0.0.0-20190624180213-70d37148ca0c/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
=======
>>>>>>> c2810dbc64957c362c5d3fda51fde652253f679c
google.golang.org/appengine v1.5.0 h1:KxkO13IPW4Lslp2bz+KHP2E3gtFlrIGNThxkZQ3g+4c=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
Expand Down
187 changes: 167 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"syscall"

"github.com/DATA-DOG/godog/colors"
"github.com/ghodss/yaml"
"github.com/gobuffalo/packr"
"github.com/joho/godotenv"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/urfave/cli"

Expand Down Expand Up @@ -81,35 +95,168 @@ func runHandler(log *log.Logger) func(*cli.Context) error {
// Initialize astilectron
var configPath string

// backward compability
if c := ctx.String("config.file"); c != "" {
log.Printf(colors.Bold(colors.Yellow)("Flag --config.file, -c is deprecated, please use args instead. For additional help try 'tomato -help'"))
configPath = c
}
if len(ctx.Args()) == 1 {
configPath = ctx.Args()[0]
}

if len(ctx.Args()) == 1 {
configPath = ctx.Args()[0]
}
if configPath == "" {
return errors.New("This command takes one argument: <config path>\nFor additional help try 'tomato -help'")
}

if configPath == "" {
return errors.New("This command takes one argument: <config path>\nFor additional help try 'tomato -help'")
}
conf, err := config.Retrieve(configPath)
if err != nil {
return errors.Wrap(err, "Failed to retrieve config")
}

if featuresPath := ctx.String("features.path"); featuresPath != "" {
conf.FeaturesPaths = strings.Split(featuresPath, ",")
}

t := tomato.New(conf, log)

if err := t.Verify(); err != nil {
return errors.Wrap(err, "Verification failed")
}

return t.Run()
},
},
}
}

conf, err := config.Retrieve(configPath)
func openbrowser(url string) (err error) {
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return
}

func yamlToJSON(y []byte) (map[string]interface{}, error) {
by, err := yaml.YAMLToJSON(y)
if err != nil {
return nil, err
}

result := make(map[string]interface{})
if err := json.Unmarshal(by, &result); err != nil {
return nil, err
}
return result, nil
}

func setupResponse(w *http.ResponseWriter, req *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}

func editServer(ctx *cli.Context) error {
var g run.Group
var configPath string
if len(ctx.Args()) == 1 {
configPath = ctx.Args()[0]
}
if configPath == "" {
return errors.New("This command takes one argument: <config path>\nFor additional help try 'tomato edit -help'")
}

l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return err
}
printErr := func(err error) {
l.Close()
if err != nil {
return errors.Wrap(err, "Failed to retrieve config")
log.Printf(colors.Bold(colors.Red)("ERR: %v"), err)
}
}
g.Add(func() error {

if featuresPath := ctx.String("features.path"); featuresPath != "" {
conf.FeaturesPaths = strings.Split(featuresPath, ",")
if err := openbrowser("http://" + l.Addr().String() + "/index.html"); err != nil {
return err
}

t := tomato.New(conf, log)
box := packr.NewBox(".")

return http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("client") != "" {
setupResponse(&w, r)
switch r.Method {
case http.MethodGet:
dict, err := box.Find("./dictionary.yml")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dictMap, err := yamlToJSON(dict)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
cfg, err := ioutil.ReadFile(configPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
config, err := yamlToJSON(cfg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

b, err := json.Marshal(map[string]interface{}{
"config": config,
"dictionary": dictMap,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
} else {
if r.URL.Path == "" {
index, err := box.Find("./ui/build/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var buf bytes.Buffer

t := template.Must(template.New("config").Parse(string(index)))
if err := t.ExecuteTemplate(&buf, "config", map[string]string{"ServerURL": "http://" + l.Addr().String()}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if err := t.Verify(); err != nil {
return errors.Wrap(err, "Verification failed")
w.Write(index)
return
}

fs := http.FileServer(http.Dir("./ui/build"))
fs.ServeHTTP(w, r)
}
}))
}, printErr)

term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
g.Add(func() error {
select {
case <-term:
log.Printf(colors.Bold(colors.Yellow)("Received SIGTERM, exiting gracefully..."))
}
return nil
}, printErr)

return t.Run()
}
return g.Run()
}
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REACT_APP_SERVER_URL=https://run.mocky.io/v3/4875a81b-12e2-40a1-ab99-c93c8a37eea8
REACT_APP_SERVER_URL=https://run.mocky.io/v3/68583461-f739-486c-b77e-89de4d4f85ba
Loading