-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdInfo.go
117 lines (100 loc) · 2.65 KB
/
cmdInfo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"fmt"
"os"
"text/tabwriter"
"time"
pbApic "github.com/protosio/protos/apic/proto"
"github.com/urfave/cli/v2"
)
var cmdInfo *cli.Command = &cli.Command{
Name: "info",
Usage: "Various commands for displaying information about the user and local instance",
Subcommands: []*cli.Command{
{
Name: "devices",
Usage: "List user devices",
Action: func(c *cli.Context) error {
return listDevices()
},
},
{
Name: "user",
Usage: "Display information about the user",
Action: func(c *cli.Context) error {
return userInfo()
},
},
{
Name: "sshkey",
Usage: "Display local SSH key",
Subcommands: []*cli.Command{
{
Name: "public",
Usage: "Display public SSH key",
Action: func(c *cli.Context) error {
return publishSSHKey()
},
},
{
Name: "private",
Usage: "Display private SSH key",
Action: func(c *cli.Context) error {
return privateSSHKey()
},
},
},
},
},
}
func listDevices() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetUserDevices(ctx, &pbApic.GetUserDevicesRequest{})
if err != nil {
return fmt.Errorf("failed to list user devices: %w", err)
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 2, ' ', 0)
defer w.Flush()
fmt.Fprintf(w, " %s\t%s\t%s\t", "Name", "WG Public Key", "ID")
fmt.Fprintf(w, "\n %s\t%s\t%s\t", "----", "-------------", "--")
for _, device := range resp.Devices {
fmt.Fprintf(w, "\n %s\t%s\t%s\t", device.Name, device.PublicKey, device.PublicKeyWireguard)
}
fmt.Fprint(w, "\n")
return nil
}
func userInfo() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetUserInfo(ctx, &pbApic.GetUserInfoRequest{})
if err != nil {
return fmt.Errorf("failed to list user devices: %w", err)
}
fmt.Printf("Name: %s\n", resp.Name)
fmt.Printf("Username: %s\n", resp.Username)
fmt.Printf("Is admin: %t\n", resp.IsAdmin)
return nil
}
func publishSSHKey() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetLocalSSHKey(ctx, &pbApic.GetLocalSSHKeyRequest{})
if err != nil {
return fmt.Errorf("failed to get local SSH key: %w", err)
}
fmt.Println(resp.Public)
return nil
}
func privateSSHKey() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetLocalSSHKey(ctx, &pbApic.GetLocalSSHKeyRequest{})
if err != nil {
return fmt.Errorf("failed to get local SSH key: %w", err)
}
fmt.Println(resp.Private)
return nil
}