Skip to content

Commit

Permalink
Add "web" Flag for Launching OAuth2l Playground (#103)
Browse files Browse the repository at this point in the history
This is an experimental feature that will install and launch "oauth2l-web", an interactive web-app version of oauth2l.
  • Loading branch information
leorcodes authored Jul 31, 2020
1 parent 0680cf0 commit a5f08de
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion integration/golden/no-command.golden
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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()
Expand Down
102 changes: 102 additions & 0 deletions util/web.go
Original file line number Diff line number Diff line change
@@ -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.")
}

0 comments on commit a5f08de

Please sign in to comment.