Skip to content

Commit

Permalink
nearly working
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Dec 3, 2017
1 parent dfba36d commit c86a853
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
15 changes: 14 additions & 1 deletion cmd/golly/golly.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"syscall"

"github.com/matthewmueller/golly/internal/chrome/downloader"
"github.com/matthewmueller/golly/internal/compiler/util"

kingpin "gopkg.in/alecthomas/kingpin.v2"
Expand Down Expand Up @@ -81,8 +82,20 @@ func run(ctx context.Context) error {
}
filePath := path.Join(cwd, *runFile)

root, err := util.GollyPath()
if err != nil {
return errors.Wrapf(err, "error getting root path")
}

chromePath, err := downloader.Find(path.Join(root, "chrome"))
if err != nil {
return errors.Wrapf(err, "unable to get chrome path")
}
log.Infof("chromepath=%s", chromePath)
// return errors.New("the Google Chrome path needs to be set")

result, err := api.Run(ctx, &api.RunSettings{
ChromePath: os.Getenv("GOLLY_CHROME_PATH"),
ChromePath: chromePath,
FilePath: filePath,
})
if err != nil {
Expand Down
35 changes: 32 additions & 3 deletions internal/chrome/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"os/exec"
"path"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -40,6 +41,32 @@ type Settings struct {
Addr string
}

var defaultFlags = []string{
"--disable-background-networking",
"--disable-background-timer-throttling",
"--disable-client-side-phishing-detection",
"--disable-default-apps",
"--disable-extensions",
"--disable-hang-monitor",
"--disable-popup-blocking",
"--disable-prompt-on-repost",
"--disable-sync",
"--disable-translate",
"--metrics-recording-only",
"--no-first-run",
"--remote-debugging-port=0",
"--safebrowsing-disable-auto-update",

"--enable-automation",
"--password-store=basic",
"--use-mock-keychain",

"--headless",
"--disable-gpu",
"--hide-scrollbars",
"--mute-audio",
}

var errStopped = errors.New("stopped")

// New chrome
Expand All @@ -58,11 +85,13 @@ func New(parent context.Context, settings *Settings) (*Chrome, error) {
// default flags
flags := append(
settings.Flags,
"--headless",
"--disable-gpu",
"--remote-debugging-port="+addr.Port(),
defaultFlags...,
)

// user data dir
tmp := os.TempDir()
flags = append(flags, "--user-data-dir="+path.Join(tmp))

// create the command
cmd := exec.CommandContext(ctx, settings.ExecutablePath, flags...)

Expand Down
95 changes: 95 additions & 0 deletions internal/chrome/downloader/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package downloader

import (
"fmt"
"net/http"
"os"
"path"
"runtime"

"github.com/mholt/archiver"
"github.com/pkg/errors"
)

// ChromiumRevision chromium revision
const ChromiumRevision = "518818"

// DownloadHost download host
const DownloadHost = "https://storage.googleapis.com"

var downloadURLs = map[string]string{
"linux": "%s/chromium-browser-snapshots/Linux_x64/%s/chrome-linux.zip",
"mac": "%s/chromium-browser-snapshots/Mac/%s/chrome-mac.zip",
"win32": "%s/chromium-browser-snapshots/Win/%s/chrome-win32.zip",
"win64": "%s/chromium-browser-snapshots/Win_x64/%s/chrome-win32.zip",
}

// Find locally or remotely
func Find(dir string) (string, error) {
filepath, err := chromiumPath(dir)
if err != nil {
return "", errors.Wrapf(err, "error getting chromium path")
}

if _, err := os.Stat(filepath); !os.IsNotExist(err) {
return filepath, nil
}

if err := download(dir); err != nil {
return "", errors.Wrapf(err, "error downloading")
}

return filepath, nil
}

// Download path
func download(dir string) error {
var url, platform string

switch runtime.GOOS {
case "darwin":
platform = "mac"
url = fmt.Sprintf(downloadURLs[platform], DownloadHost, ChromiumRevision)
case "linux":
platform = "linux"
url = fmt.Sprintf(downloadURLs[platform], DownloadHost, ChromiumRevision)
case "windows":
platform = "windows"
// TODO: 32 vs 64
url = fmt.Sprintf(downloadURLs["win64"], DownloadHost, ChromiumRevision)
default:
return errors.New("unsupported operating system")
}

res, err := http.Get(url)
if err != nil {
return errors.Wrapf(err, "error getting download")
}
defer res.Body.Close()

folderPath := path.Join(dir, platform+"-"+ChromiumRevision)
if err := os.MkdirAll(folderPath, 0775); err != nil {
return errors.Wrapf(err, "error making directory")
}

return archiver.Zip.Read(res.Body, folderPath)
}

// Path to the executable
// TODO: test on windows
func chromiumPath(dir string) (string, error) {
var platform string
switch runtime.GOOS {
case "darwin":
platform = "mac"
return path.Join(dir, platform+"-"+ChromiumRevision, "chrome-mac", "Chromium.app", "Contents", "MacOS", "Chromium"), nil
case "linux":
platform = "linux"
return path.Join(dir, platform+"-"+ChromiumRevision, "chrome-linux", "chrome"), nil
case "windows":
platform = "windows"
return path.Join(dir, platform+"-"+ChromiumRevision, "chrome-win32", "chrome.exe"), nil
default:
return "", errors.New("unsupported operating system")
}
}

0 comments on commit c86a853

Please sign in to comment.