diff --git a/integration/golden/no-command.golden b/integration/golden/no-command.golden index 0aadd68..6b9894b 100644 --- a/integration/golden/no-command.golden +++ b/integration/golden/no-command.golden @@ -1 +1 @@ -Please specify one command of: curl, fetch, header, info, reset or test +Please specify one command of: curl, fetch, header, info, reset, test or web diff --git a/main.go b/main.go index 6059d73..d3135b4 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,7 @@ type commandOptions struct { Info infoOptions `command:"info" description:"Display info about an OAuth access token."` Test infoOptions `command:"test" description:"Tests an OAuth access token. Returns 0 for valid token."` Reset resetOptions `command:"reset" description:"Resets the cache."` + Web webOptions `command:"web" description:"Launches a local instance of the OAuth2l Playground web app. This feature is experimental."` } // Common options for "fetch", "header", and "curl" commands. @@ -114,6 +115,12 @@ type resetOptions struct { Cache *string `long:"cache" description:"Path to the credential cache file to remove. Defaults to ~/.oauth2l."` } +// Options for "web" command +type webOptions struct { + // Stop the web app + Stop bool `long:"stop" description:"Stops the OAuth2l Playground."` +} + // Reads and returns content of JSON file. func readJSON(file string) (string, error) { if file != "" { @@ -370,6 +377,13 @@ func main() { } os.Exit(task(token)) + } else if cmd == "web" { + if opts.Web.Stop { + util.WebStop() + } else { + util.Web() + } + } else if cmd == "reset" { setCacheLocation(opts.Reset.Cache) util.Reset() diff --git a/util/web.go b/util/web.go new file mode 100644 index 0000000..ebd90ad --- /dev/null +++ b/util/web.go @@ -0,0 +1,102 @@ +// +// Copyright 2018 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package util + +import ( + "fmt" + "log" + "os" + "os/exec" + "runtime" + "strings" +) + +const ( + defaultServer = "http://localhost:3000/" +) + +var location string = "~/.oauth2l-web" + +// Runs the frontend/backend for OAuth2l Playground +func Web() { + _, err := os.Stat("~/.oauth2l-web") + if os.IsNotExist(err) { + var decision string + fmt.Println("The Web feature will be installed in ~/.oauth2l-web. Would you like to change the directory? (y/n)") + fmt.Scanln(&decision) + decision = strings.ToLower(decision) + if decision == "y" || decision == "yes" { + fmt.Println("Enter new directory location") + fmt.Scanln(&location) + } + fmt.Println("Installing...") + cmd := exec.Command("git", "clone", "https://github.com/googleinterns/oauth2l-web.git", location) + clonErr := cmd.Run() + if clonErr != nil { + log.Fatal(clonErr.Error()) + } else { + fmt.Println("Web feature installed") + } + } + cmd := exec.Command("docker-compose", "up", "-d", "--build") + cmd.Dir = location + + dockErr := cmd.Run() + + if dockErr != nil { + fmt.Println("Check to see if Docker is running!") + log.Fatal(dockErr.Error()) + + } else { + openWeb() + } +} + +// opens the website on the default browser +func openWeb() error { + var cmd string + + switch runtime.GOOS { + case "darwin": + cmd = "open" + case "linux": + cmd = "xdg-open" + case "windows": + cmd = "start" + default: + cmd = "Not currently supported" + } + + return exec.Command(cmd, defaultServer).Start() +} + +// closes the containers and removes stopped containers +func WebStop() { + cmd := exec.Command("docker-compose", "stop") + cmd.Dir = location + err := cmd.Run() + if err != nil { + log.Fatal(err.Error()) + } + + remContainer := exec.Command("docker-compose", "rm", "-f") + remContainer.Dir = location + remErr := remContainer.Run() + if remErr != nil { + log.Fatal(err.Error()) + } + + fmt.Println("OAuth2l Playground was stopped.") +}