~rockorager/mjmap

d54badae8152b4db6eec8b03a7bd7c5ff1724aa7 — Tim Culverhouse 6 months ago 90224a5 main
config: implement token-cmd

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
1 files changed, 19 insertions(+), 0 deletions(-)

M config.go
M config.go => config.go +19 -0
@@ 3,7 3,9 @@ package main
import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"strings"

	"git.sr.ht/~emersion/go-scfg"
	"github.com/kyoh86/xdg"


@@ 13,6 15,7 @@ type account struct {
	endpoint string
	username string
	token    string
	tokenCmd string
}

func loadConfig() (*account, error) {


@@ 49,8 52,24 @@ func loadConfig() (*account, error) {
			if err := key.ParseParams(&acct.username); err != nil {
				return nil, err
			}
		case "token-cmd":
			if err := key.ParseParams(&acct.tokenCmd); err != nil {
				return nil, err
			}
		}
	}

	if acct.tokenCmd != "" && acct.token == "" {
		cmd := exec.Command("sh", "-c", acct.tokenCmd)
		out, err := cmd.Output()
		if err != nil {
			return nil, err
		}
		lines := strings.Split(string(out), "\n")
		if len(lines) == 0 {
			return nil, fmt.Errorf("No output from token-cmd")
		}
		acct.token = lines[0]
	}

	return acct, nil