forked from parseablehq/pb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
259 lines (229 loc) · 6.71 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2024 Parseable, Inc
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"errors"
"fmt"
"os"
pb "pb/cmd"
"pb/pkg/analytics"
"pb/pkg/config"
"sync"
"github.com/spf13/cobra"
)
var wg sync.WaitGroup
// populated at build time
var (
Version string
Commit string
)
var (
versionFlag = "version"
versionFlagShort = "v"
)
func defaultInitialProfile() config.Profile {
return config.Profile{
URL: "https://demo.parseable.com",
Username: "admin",
Password: "admin",
}
}
// Root command
var cli = &cobra.Command{
Use: "pb",
Short: "\nParseable command line interface",
Long: "\npb is the command line interface for Parseable",
PersistentPreRunE: analytics.CheckAndCreateULID,
RunE: func(command *cobra.Command, _ []string) error {
if p, _ := command.Flags().GetBool(versionFlag); p {
pb.PrintVersion(Version, Commit)
return nil
}
return errors.New("no command or flag supplied")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if os.Getenv("PB_ANALYTICS") == "disable" {
return
}
wg.Add(1)
go func() {
defer wg.Done()
analytics.PostRunAnalytics(cmd, "cli", args)
}()
},
}
var profile = &cobra.Command{
Use: "profile",
Short: "Manage different Parseable targets",
Long: "\nuse profile command to configure different Parseable instances. Each profile takes a URL and credentials.",
PersistentPreRunE: combinedPreRun,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if os.Getenv("PB_ANALYTICS") == "disable" {
return
}
wg.Add(1)
go func() {
defer wg.Done()
analytics.PostRunAnalytics(cmd, "profile", args)
}()
},
}
var user = &cobra.Command{
Use: "user",
Short: "Manage users",
Long: "\nuser command is used to manage users.",
PersistentPreRunE: combinedPreRun,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if os.Getenv("PB_ANALYTICS") == "disable" {
return
}
wg.Add(1)
go func() {
defer wg.Done()
analytics.PostRunAnalytics(cmd, "user", args)
}()
},
}
var role = &cobra.Command{
Use: "role",
Short: "Manage roles",
Long: "\nrole command is used to manage roles.",
PersistentPreRunE: combinedPreRun,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if os.Getenv("PB_ANALYTICS") == "disable" {
return
}
wg.Add(1)
go func() {
defer wg.Done()
analytics.PostRunAnalytics(cmd, "role", args)
}()
},
}
var stream = &cobra.Command{
Use: "stream",
Short: "Manage streams",
Long: "\nstream command is used to manage streams.",
PersistentPreRunE: combinedPreRun,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if os.Getenv("PB_ANALYTICS") == "disable" {
return
}
wg.Add(1)
go func() {
defer wg.Done()
analytics.PostRunAnalytics(cmd, "stream", args)
}()
},
}
var query = &cobra.Command{
Use: "query",
Short: "Run SQL query on a log stream",
Long: "\nRun SQL query on a log stream. Default output format is json. Use -i flag to open interactive table view.",
PersistentPreRunE: combinedPreRun,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if os.Getenv("PB_ANALYTICS") == "disable" {
return
}
wg.Add(1)
go func() {
defer wg.Done()
analytics.PostRunAnalytics(cmd, "query", args)
}()
},
}
func main() {
profile.AddCommand(pb.AddProfileCmd)
profile.AddCommand(pb.RemoveProfileCmd)
profile.AddCommand(pb.ListProfileCmd)
profile.AddCommand(pb.DefaultProfileCmd)
user.AddCommand(pb.AddUserCmd)
user.AddCommand(pb.RemoveUserCmd)
user.AddCommand(pb.ListUserCmd)
user.AddCommand(pb.SetUserRoleCmd)
role.AddCommand(pb.AddRoleCmd)
role.AddCommand(pb.RemoveRoleCmd)
role.AddCommand(pb.ListRoleCmd)
stream.AddCommand(pb.AddStreamCmd)
stream.AddCommand(pb.RemoveStreamCmd)
stream.AddCommand(pb.ListStreamCmd)
stream.AddCommand(pb.StatStreamCmd)
query.AddCommand(pb.QueryCmd)
query.AddCommand(pb.SavedQueryList)
cli.AddCommand(profile)
cli.AddCommand(query)
cli.AddCommand(stream)
cli.AddCommand(user)
cli.AddCommand(role)
cli.AddCommand(pb.TailCmd)
cli.AddCommand(pb.AutocompleteCmd)
// Set as command
pb.VersionCmd.Run = func(_ *cobra.Command, _ []string) {
pb.PrintVersion(Version, Commit)
}
cli.AddCommand(pb.VersionCmd)
// set as flag
cli.Flags().BoolP(versionFlag, versionFlagShort, false, "Print version")
cli.CompletionOptions.HiddenDefaultCmd = true
// create a default profile if file does not exist
if previousConfig, err := config.ReadConfigFromFile(); os.IsNotExist(err) {
conf := config.Config{
Profiles: map[string]config.Profile{"demo": defaultInitialProfile()},
DefaultProfile: "demo",
}
err = config.WriteConfigToFile(&conf)
if err != nil {
fmt.Printf("failed to write to file %v\n", err)
os.Exit(1)
}
} else {
// Only update the "demo" profile without overwriting other profiles
demoProfile, exists := previousConfig.Profiles["demo"]
if exists {
// Update fields in the demo profile only
demoProfile.URL = "http://demo.parseable.com"
demoProfile.Username = "admin"
demoProfile.Password = "admin"
previousConfig.Profiles["demo"] = demoProfile
} else {
// Add the "demo" profile if it doesn't exist
previousConfig.Profiles["demo"] = defaultInitialProfile()
previousConfig.DefaultProfile = "demo" // Optional: set as default if needed
}
// Write the updated configuration back to file
err = config.WriteConfigToFile(previousConfig)
if err != nil {
fmt.Printf("failed to write to existing file %v\n", err)
os.Exit(1)
}
}
err := cli.Execute()
if err != nil {
os.Exit(1)
}
wg.Wait()
}
// Wrapper to combine existing pre-run logic and ULID check
func combinedPreRun(cmd *cobra.Command, args []string) error {
err := pb.PreRunDefaultProfile(cmd, args)
if err != nil {
return fmt.Errorf("error initializing default profile: %w", err)
}
if err := analytics.CheckAndCreateULID(cmd, args); err != nil {
return fmt.Errorf("error while creating ulid: %v", err)
}
return nil
}