This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (137 loc) · 3.9 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"os"
"github.com/integrii/flaggy"
"github.com/somedevv/permit-ssh/cmd"
"github.com/somedevv/permit-ssh/colors"
"github.com/somedevv/permit-ssh/conf"
"github.com/somedevv/permit-ssh/utils"
)
// Version of the program, set at buildtime with -ldflags "-X main.version=X"
var version = ""
var (
// FLAG VARIABLES
user string
key string
ip string
// AWS FLAG VARIABLES
profile string
region string
instance string
// SUBCOMMANDS
remove *flaggy.Subcommand
add *flaggy.Subcommand
list *flaggy.Subcommand
interactive *flaggy.Subcommand
awsset *flaggy.Subcommand
// Configuration
config conf.Config
)
func init() {
// Load the config file
config.GetConf()
//------META------//
flaggy.SetName("permit")
flaggy.SetDescription("Your own SSH key manager and friend, made by somedevv")
flaggy.SetVersion(version)
flaggy.DefaultParser.AdditionalHelpPrepend = "https://github.com/somedevv/permit-ssh"
//------NESTED SUBCOMMANDS------//
//AWS
awsset = flaggy.NewSubcommand("aws")
awsset.String(&profile, "p", "profile", "AWS Profile")
awsset.String(®ion, "r", "region", "AWS Profile")
awsset.String(&instance, "i", "instance", "AWS Instance name")
//---------SUBCOMMANDS---------//
//------DELETE------//
remove = flaggy.NewSubcommand("remove")
remove.String(&user, "u", "user", "The user to remove")
remove.String(&key, "k", "key", "The key to remove")
remove.String(&ip, "ip", "address", "The IP of the server to remove the user")
remove.AttachSubcommand(awsset, 1)
flaggy.AttachSubcommand(remove, 1)
//------ADD------//
add = flaggy.NewSubcommand("add")
add.String(&user, "u", "user", "The user to add")
add.String(&key, "k", "key", "The key to add")
add.String(&ip, "ip", "address", "The IP of the server to add the user")
add.AttachSubcommand(awsset, 1)
flaggy.AttachSubcommand(add, 1)
//------LIST------//
list = flaggy.NewSubcommand("list")
list.AttachSubcommand(awsset, 1)
flaggy.AttachSubcommand(list, 1)
//------INTERACTIVE------//
interactive = flaggy.NewSubcommand("interactive")
flaggy.AttachSubcommand(interactive, 1)
//------PARSE------//
flaggy.Parse()
}
func main() {
if config.DB == "local" {
RunWithLocalDB()
}
}
func RunWithLocalDB() {
db := utils.SetupLocalDB()
if interactive.Used {
cmd.InteractiveLocal(db)
}
if list.Used {
if awsset.Used == true {
if profile == "" && region == "" {
colors.Red.Println("Error: At least AWS profile or region must be set")
os.Exit(1)
}
cmd.ListAWS(profile, region)
} else {
cmd.ListLocal(db)
}
}
if remove.Used {
if ip == "" && awsset.Used == false {
utils.RemoveKeyFromLocalDB(db, user, key)
}
if user != "" && key == "" {
key = utils.SearchUserInLocalDB(db, user)
if key == "" {
colors.Red.Printf("User [%s] not found\n", user)
os.Exit(1)
}
}
if ip != "" && key != "" {
cmd.DeleteWithIP(ip, key)
} else if awsset.Used == true && key != "" {
if profile == "" && region == "" {
colors.Red.Println("Error: At least AWS profile or region must be set")
os.Exit(1)
}
cmd.DeleteWithAWS(profile, region, instance, key)
} else {
colors.Red.Println("Error: At least IP or AWS Instance with user or key must be set")
os.Exit(1)
}
}
if add.Used {
if user != "" && key != "" {
utils.SaveKeyInLocalDB(db, user, key, ip)
} else if user != "" && key == "" {
key = utils.SearchUserInLocalDB(db, user)
}
if awsset.Used == true || ip != "" {
if awsset.Used == true && instance != "" && key != "" {
if profile == "" && region == "" {
colors.Red.Println("Error: At least AWS profile or region must be set")
os.Exit(1)
}
cmd.AddWithAWS(profile, region, instance, key)
} else if ip != "" && key != "" {
cmd.AddWithIP(ip, key)
} else {
colors.Red.Println("Error: At least IP or AWS Instance with user or key must be set")
os.Exit(1)
}
}
}
defer db.Close()
os.Exit(0)
}