Skip to content

Commit

Permalink
Add config map
Browse files Browse the repository at this point in the history
ref #1
  • Loading branch information
babarot committed Nov 21, 2017
1 parent f291876 commit 7f80900
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 12 deletions.
118 changes: 118 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

type Config struct {
Services []Service `json:"service"`
}

type Service struct {
URL string `json:"url"`
Env Env `json:"env"`
}

type Env struct {
Credentials string `json:"GOOGLE_APPLICATION_CREDENTIALS"`
ClientID string `json:"IAP_CLIENT_ID"`
Binary string `json:"IAP_CURL_BIN"`
}

func configDir() (string, error) {
var dir string

switch runtime.GOOS {
default:
dir = filepath.Join(os.Getenv("HOME"), ".config")
case "windows":
dir = os.Getenv("APPDATA")
if dir == "" {
dir = filepath.Join(os.Getenv("USERPROFILE"), "Application Data")
}
}
dir = filepath.Join(dir, "iap_curl")

err := os.MkdirAll(dir, 0700)
if err != nil {
return dir, fmt.Errorf("cannot create directory: %v", err)
}

return dir, nil
}

func (cfg *Config) LoadFile(file string) error {
_, err := os.Stat(file)
if err == nil {
raw, _ := ioutil.ReadFile(file)
if err := json.Unmarshal(raw, cfg); err != nil {
return err
}
return nil
}

if !os.IsNotExist(err) {
return err
}
f, err := os.Create(file)
if err != nil {
return err
}

// Insert sample config map as a default
if len(cfg.Services) == 0 {
cfg.Services = []Service{Service{
URL: "https://iap-protected-app-url",
Env: Env{
Credentials: "/path/to/google-credentials.json",
ClientID: "foobar.apps.googleusercontent.com",
Binary: "curl",
},
}}
}

return json.NewEncoder(f).Encode(cfg)
}

func (cfg *Config) GetEnv(url string) (env Env) {
for _, service := range cfg.Services {
if strings.Contains(service.URL, url) {
return service.Env
}
}
return env
}

func (cfg *Config) GetURLs() (list []string) {
for _, service := range cfg.Services {
list = append(list, service.URL)
}
return
}

func (cfg *Config) Edit() error {
dir, _ := configDir()
json := filepath.Join(dir, "config.json")
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
command := fmt.Sprintf("%s %s", editor, json)
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", command)
} else {
cmd = exec.Command("sh", "-c", command)
}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
return cmd.Run()
}
65 changes: 53 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)

const (
Expand All @@ -13,42 +15,81 @@ const (
IAPCurlBinary = "IAP_CURL_BIN"
)

var helpText string = `Usage: curl
const helpText string = `Usage: curl
Extended options:
--list-urls List service URLs
--edit-config Edit config file
`

var (
credentials string
clientID string
binary string

cfg Config
)

func main() {
dir, _ := configDir()
json := filepath.Join(dir, "config.json")

err := cfg.LoadFile(json)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

credentials = os.Getenv(GoogleApplicationCredentials)
clientID = os.Getenv(IAPClientID)
binary = os.Getenv(IAPCurlBinary)

os.Exit(run(os.Args[1:]))
}

func run(args []string) int {
var (
creds = os.Getenv(GoogleApplicationCredentials)
clientID = os.Getenv(IAPClientID)
binary = os.Getenv(IAPCurlBinary)
)

if len(args) > 0 {
if args[0] == "-h" || args[0] == "--help" {
switch args[0] {
case "-h", "--help":
fmt.Fprint(os.Stderr, helpText)
return 1
case "--list-urls":
fmt.Println(strings.Join(cfg.GetURLs(), "\n"))
return 0
case "--edit-config":
err := cfg.Edit()
if err == nil {
return 0
}
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
return 1
}

env := cfg.GetEnv(args[0])
if credentials == "" {
credentials = env.Credentials
}
if clientID == "" {
clientID = env.ClientID
}
if binary == "" {
binary = env.Binary
}
}

if creds == "" {
if credentials == "" {
fmt.Fprintf(os.Stderr, "Error: %s is missing\n", GoogleApplicationCredentials)
return 1
}

if clientID == "" {
fmt.Fprintf(os.Stderr, "Error: %s is missing\n", IAPClientID)
return 1
}

if binary == "" {
binary = "curl"
}

token, err := getToken(creds, clientID)
token, err := getToken(credentials, clientID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
return 1
Expand Down

0 comments on commit 7f80900

Please sign in to comment.