From 3eea11cb168ef4df5a148014201a30ed2e6414f7 Mon Sep 17 00:00:00 2001 From: Andy Zhao Date: Wed, 12 Aug 2020 13:49:28 -0700 Subject: [PATCH] Fix web app installation logic (#105) -Fixed issue with using incorrect home directory as default web directory. -Print actual docker error messages during launch, if any. -Updated readme. --- README.md | 12 +++++++----- main.go | 16 ++++++++++------ util/web.go | 49 ++++++++++++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index df36dbc..c3a0855 100644 --- a/README.md +++ b/README.md @@ -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 @@ -389,17 +391,17 @@ Path to Curl CLI. For optional use with "curl" command. $ oauth2l curl --curlcli /usr/bin/curl --type sso --email me@google.com --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 diff --git a/main.go b/main.go index 1b01423..2c018b5 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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" { diff --git a/util/web.go b/util/web.go index 23cac83..ede0206 100644 --- a/util/web.go +++ b/util/web.go @@ -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. @@ -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 @@ -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())