File: main.go

package info (click to toggle)
golang-github-hashicorp-terraform-svchost 0.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: makefile: 5; sh: 4
file content (64 lines) | stat: -rw-r--r-- 1,331 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

// This is a simple program that implements the "helper program" protocol
// for the svchost/auth package for unit testing purposes.

func main() {
	args := os.Args

	if len(args) < 3 {
		die("not enough arguments\n")
	}

	host := args[2]
	switch args[1] {
	case "get":
		switch host {
		case "example.com":
			fmt.Print(`{"token":"example-token"}`)
		case "other-cred-type.example.com":
			fmt.Print(`{"username":"alfred"}`) // unrecognized by main program
		case "fail.example.com":
			die("failing because you told me to fail\n")
		default:
			fmt.Print("{}") // no credentials available
		}
	case "store":
		dataSrc, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			die("invalid input: %s", err)
		}
		var data map[string]interface{}
		err = json.Unmarshal(dataSrc, &data)

		switch host {
		case "example.com":
			if data["token"] != "example-token" {
				die("incorrect token value to store")
			}
		default:
			die("can't store credentials for %s", host)
		}
	case "forget":
		switch host {
		case "example.com":
			// okay!
		default:
			die("can't forget credentials for %s", host)
		}
	default:
		die("unknown subcommand %q\n", args[1])
	}
}

func die(f string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, fmt.Sprintf(f, args...))
	os.Exit(1)
}