This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (73 loc) · 1.83 KB
/
main.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Copyright (c) 2020 SSH Communications Security Inc.
//
// All rights reserved.
//
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/SSHcom/privx-sdk-go/api"
"github.com/SSHcom/privx-sdk-go/config"
"github.com/SSHcom/privx-sdk-go/oauth"
)
var commands = map[string]func(client *api.Client){
"login": cmdLogin,
"get": cmdGet,
}
func main() {
log.SetFlags(0)
flag.Usage = func() {
fmt.Fprintf(os.Stderr,
"Usage: %s [options] COMMAND [command options] [ARG]...\n",
os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nCommands:\n")
for key := range commands {
fmt.Fprintf(os.Stderr, " - %s\n", key)
}
fmt.Fprintf(os.Stderr,
"\nType %s COMMAND -h for help about COMMAND\n",
os.Args[0])
}
apiEndpoint := flag.String("api", "", "API endpoint URL")
configFile := flag.String("config", config.Default(), "configuration file")
verbose := flag.Bool("v", false, "verbose output")
flag.Parse()
config, err := config.Read(*configFile)
if err != nil {
log.Fatalf("Failed to read config file '%s': %s", *configFile, err)
}
// Command line overrides.
if len(*apiEndpoint) > 0 {
config.API.Endpoint = *apiEndpoint
}
// Construct API client.
auth, err := oauth.NewClient(config.Auth, config.API.Endpoint,
config.API.Certificate.X509, *verbose)
if err != nil {
log.Fatal(err)
}
client, err := api.NewClient(auth, config.API.Endpoint,
config.API.Certificate.X509, *verbose)
if err != nil {
log.Fatal(err)
}
if len(flag.Args()) == 0 {
fmt.Fprintf(os.Stderr, "No command specified.\n")
return
}
os.Args = flag.Args()
fn, ok := commands[flag.Arg(0)]
if !ok {
fmt.Printf("Unknown command: %s\n", flag.Arg(0))
os.Exit(1)
}
flag.CommandLine = flag.NewFlagSet(
fmt.Sprintf("privx-secrets %s", os.Args[0]),
flag.ExitOnError)
fn(client)
}