Skip to content

Commit

Permalink
Fix web app installation logic (#105)
Browse files Browse the repository at this point in the history
-Fixed issue with using incorrect home directory as default web directory.
-Print actual docker error messages during launch, if any.
-Updated readme.
  • Loading branch information
andyrzhao authored Aug 12, 2020
1 parent f5ea072 commit 3eea11c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ $ oauth2l reset

### web

Runs the web application and opens a new window to show it. If the web application is not installed it is installed in `~/.oauth2l-web` by default.
Locally deploys and launches the OAuth2l Playground web application in a browser. If the web application packages are not yet installed, it will be installed under `~/.oauth2l-web` by default. See Command Options section for all supported options for the web command.

Note that a local installation of Docker and docker-compose tool is required in order to support this feature. For most platforms, Docker can be installed by following the instructions [here](https://docs.docker.com/get-docker/). For Google workstations, follow special installation procedures at "go/installdocker". The web feature is currently experimental and will be improved in the future.

```bash
$ oauth2l web
Expand Down Expand Up @@ -389,17 +391,17 @@ Path to Curl CLI. For optional use with "curl" command.
$ oauth2l curl --curlcli /usr/bin/curl --type sso --email [email protected] --scope cloud-platform --url https://pubsub.googleapis.com/v1/projects/my-project-id/topics
```

## web --stop
### web --stop

Stops the OAuth2l Playground.
Stops the OAuth2l Playground web app.

```bash
$ oauth2l web --stop
```

## web --directory
### web --directory

Downloads OAuth2l-web in a specfic directory.
Installs OAuth2l-web packages to a specfic directory. If this option is used, it should be provided again for future executions of the web command, such as stopping and restarting the web app.

```
$ oauth2l web --directory your/new/directory
Expand Down
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ func setCacheLocation(cache *string) {
}
}

// Overrides default web directory if configured.
func setWebDirectory(directory string) {
if directory != "" {
util.WebDirectory = directory
}
}

// Extracts the common fetch options based on chosen command.
func getCommonFetchOptions(cmdOpts commandOptions, cmd string) commonFetchOptions {
var commonOpts commonFetchOptions
Expand Down Expand Up @@ -378,14 +385,11 @@ func main() {

os.Exit(task(token))
} else if cmd == "web" {
var directory string = "~/.oauth2l-web"
if len(opts.Web.Directory) > 0 {
directory = opts.Web.Directory
}
setWebDirectory(opts.Web.Directory)
if opts.Web.Stop {
util.WebStop(directory)
util.WebStop()
} else {
util.Web(directory)
util.Web()
}

} else if cmd == "reset" {
Expand Down
49 changes: 30 additions & 19 deletions util/web.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2018 Google Inc.
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,44 +15,55 @@
package util

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/google/oauth2l/sgauth"
)

const (
defaultServer = "http://localhost:3000/"
defaultServer = "http://localhost:3000/"
defaultWebPackageName = ".oauth2l-web"
)

var WebDirectory string = filepath.Join(sgauth.GuessUnixHomeDir(), defaultWebPackageName)

// Runs the frontend/backend for OAuth2l Playground
func Web(directory string) {
_, err := os.Stat(directory)
func Web() {
_, err := os.Stat(WebDirectory)
if os.IsNotExist(err) {
fmt.Println("Installing...")
cmd := exec.Command("git", "clone", "https://github.com/googleinterns/oauth2l-web.git", directory)
clonErr := cmd.Run()
if clonErr != nil {
fmt.Println("Please enter a valid directory")
log.Fatal(clonErr.Error())
cmd := exec.Command("git", "clone", "https://github.com/googleinterns/oauth2l-web.git", WebDirectory)
cmdErr := cmd.Run()
if cmdErr != nil {
fmt.Println("Failed to install web feature.")
log.Fatal(cmdErr.Error())
} else {
fmt.Println("Web feature installed")
}
}
cmd := exec.Command("docker-compose", "up", "-d", "--build")
cmd.Dir = directory
dockErr := cmd.Run()
if dockErr != nil {
fmt.Println("Please ensure that Docker is installed.")
log.Fatal(dockErr.Error())
cmd.Dir = WebDirectory

// Capture actual error message from docker command, if there is any
var stderr bytes.Buffer
cmd.Stderr = &stderr

cmdErr := cmd.Run()
if cmdErr != nil {
fmt.Println(stderr.String())
log.Fatal(cmdErr.Error())
} else {
openWeb()
}
}

// opens the website on the default browser
// Opens the website on the default browser
func openWeb() error {
var cmd string

Expand All @@ -70,17 +81,17 @@ func openWeb() error {
return exec.Command(cmd, defaultServer).Start()
}

// closes the containers and removes stopped containers
func WebStop(directory string) {
// Closes the containers and removes stopped containers
func WebStop() {
cmd := exec.Command("docker-compose", "stop")
cmd.Dir = directory
cmd.Dir = WebDirectory
err := cmd.Run()
if err != nil {
log.Fatal(err.Error())
}

remContainer := exec.Command("docker-compose", "rm", "-f")
remContainer.Dir = directory
remContainer.Dir = WebDirectory
remErr := remContainer.Run()
if remErr != nil {
log.Fatal(err.Error())
Expand Down

0 comments on commit 3eea11c

Please sign in to comment.