forked from Lorquas/rhc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
853 lines (773 loc) · 25.1 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"time"
"git.sr.ht/~spc/go-log"
"golang.org/x/term"
"github.com/briandowns/spinner"
systemd "github.com/coreos/go-systemd/v22/dbus"
"github.com/redhatinsights/rhc/internal/http"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
const redColor = "\u001B[31m"
const greenColor = "\u001B[32m"
const blueColor = "\u001B[36m"
const endColor = "\u001B[0m"
// Colorful prefixes
const ttyConnectedPrefix = greenColor + "●" + endColor
const ttyDisconnectedPrefix = redColor + "●" + endColor
const ttyInfoPrefix = blueColor + "●" + endColor
const ttyErrorPrefix = redColor + "!" + endColor
// Black & white prefixes. Unicode characters
const bwConnectedPrefix = "✓"
const bwDisconnectedPrefix = "𐄂"
const bwErrorPrefix = "!"
const bwInfoPrefix = "·"
type LogMessage struct {
level log.Level
message error
}
// showProgress calls function and, when it is possible display spinner with
// some progress message.
func showProgress(progressMessage string, isColorful bool, function func() error) error {
var s *spinner.Spinner
if isColorful {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = progressMessage
s.Start()
// Stop spinner after running function
defer func() { s.Stop() }()
}
return function()
}
// getColorPreferences tries to get color preferences form context
func getColorPreferences(ctx *cli.Context) (connectedPrefix string, disconnectedPrefix string,
errorPrefix string, infoPrefix string, isColorful bool) {
noColor := ctx.Bool("no-color")
if noColor {
isColorful = false
connectedPrefix = bwConnectedPrefix
disconnectedPrefix = bwDisconnectedPrefix
errorPrefix = bwErrorPrefix
infoPrefix = bwInfoPrefix
} else {
isColorful = true
connectedPrefix = ttyConnectedPrefix
disconnectedPrefix = ttyDisconnectedPrefix
errorPrefix = ttyErrorPrefix
infoPrefix = ttyInfoPrefix
}
return
}
// showTimeDuration shows table with duration of each sub-action
func showTimeDuration(durations map[string]time.Duration) {
if log.CurrentLevel() >= log.LevelDebug {
fmt.Println()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(w, "STEP\tDURATION\t")
for step, duration := range durations {
_, _ = fmt.Fprintf(w, "%v\t%v\t\n", step, duration.Truncate(time.Millisecond))
}
_ = w.Flush()
}
}
// showErrorMessages shows table with all error messages gathered during action
func showErrorMessages(action string, errorMessages map[string]LogMessage) error {
if hasPriorityErrors(errorMessages, log.CurrentLevel()) {
fmt.Println()
fmt.Printf("The following errors were encountered during %s:\n\n", action)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(w, "TYPE\tSTEP\tERROR\t")
for step, logMsg := range errorMessages {
if logMsg.level <= log.CurrentLevel() {
_, _ = fmt.Fprintf(w, "%v\t%v\t%v\n", logMsg.level, step, logMsg.message)
}
}
_ = w.Flush()
if hasPriorityErrors(errorMessages, log.LevelError) {
return cli.Exit("", 1)
}
}
return nil
}
// getConfProfile will retrieve the profile the system is configured
func getConfProfile(client *http.Client) (map[string]interface{}, error) {
var profile map[string]interface{}
url, err := GuessAPIURL()
if err != nil {
return nil, err
}
res, err := client.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("cannot get system profile: %s", res.Status)
}
// Read the response body
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &profile); err != nil {
return nil, fmt.Errorf("cannot unmarshal profile: %s", res.Status)
}
return profile, nil
}
// showConfProfile shows a list of the system profile enabled features gathered from API
// https://github.com/RedHatInsights/config-manager/blob/master/internal/http/v2/openapi.json
func showConfProfile(p *map[string]interface{}) {
services := []string{}
for service, status := range *p {
switch status.(type) {
case bool:
if service == "active" {
service = "remote configuration"
}
services = append(services, service)
}
}
output := strings.Join(services, `, `)
fmt.Printf("%v", output)
}
// registerRHSM tries to register system against Red Hat Subscription Management server (candlepin server)
func registerRHSM(ctx *cli.Context) (string, error) {
uuid, err := getConsumerUUID()
if err != nil {
return "Unable to get consumer UUID", cli.Exit(err, 1)
}
var successMsg string
_, _, _, _, isColorful := getColorPreferences(ctx)
if uuid == "" {
username := ctx.String("username")
password := ctx.String("password")
organization := ctx.String("organization")
activationKeys := ctx.StringSlice("activation-key")
if len(activationKeys) == 0 {
if username == "" {
password = ""
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Username: ")
_ = scanner.Scan()
username = strings.TrimSpace(scanner.Text())
}
if password == "" {
fmt.Print("Password: ")
data, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "Unable to read password", cli.Exit(err, 1)
}
password = string(data)
fmt.Printf("\n\n")
}
}
var s *spinner.Spinner
if isColorful {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Connecting to Red Hat Subscription Management..."
s.Start()
defer s.Stop()
}
var err error
if len(activationKeys) > 0 {
err = registerActivationKey(
organization,
ctx.StringSlice("activation-key"),
ctx.String("server"))
} else {
var orgs []string
if organization != "" {
_, err = registerUsernamePassword(username, password, organization, ctx.String("server"))
} else {
orgs, err = registerUsernamePassword(username, password, "", ctx.String("server"))
/* When organization was not specified using CLI option --organization, and it is
required, because user is member of more than one organization, then ask for
the organization. */
if len(orgs) > 0 {
// Stop spinner to be able to display message and ask for organization
if isColorful {
s.Stop()
}
// Ask for organization and display hint with list of organizations
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Available Organizations:")
writer := tabwriter.NewWriter(os.Stdout, 0, 2, 2, ' ', 0)
for i, org := range orgs {
_, _ = fmt.Fprintf(writer, "%v\t", org)
if (i+1)%4 == 0 {
_, _ = fmt.Fprint(writer, "\n")
}
}
_ = writer.Flush()
fmt.Print("\nOrganization: ")
_ = scanner.Scan()
organization = strings.TrimSpace(scanner.Text())
fmt.Printf("\n")
// Start spinner again
if isColorful {
s.Start()
}
// Try to register once again with given organization
_, err = registerUsernamePassword(username, password, organization, ctx.String("server"))
}
}
}
if err != nil {
return "Unable to register system to RHSM", cli.Exit(err, 1)
}
successMsg = "Connected to Red Hat Subscription Management"
} else {
successMsg = "This system is already connected to Red Hat Subscription Management"
}
return successMsg, nil
}
// connectAction tries to register system against Red Hat Subscription Management,
// gather the profile information that the system will configure
// connect system to Red Hat Insights and it also tries to start rhcd service
func connectAction(ctx *cli.Context) error {
uid := os.Getuid()
if uid != 0 {
return cli.Exit(fmt.Errorf("error: non-root user cannot connect system"), 1)
}
var start time.Time
durations := make(map[string]time.Duration)
errorMessages := make(map[string]LogMessage)
hostname, err := os.Hostname()
if err != nil {
return cli.Exit(err, 1)
}
fmt.Printf("Connecting %v to %v.\nThis might take a few seconds.\n\n", hostname, Provider)
connectedPrefix, disconnectedPrefix, errorPrefix, infoPrefix, isColorful := getColorPreferences(ctx)
/* 1. Register to RHSM, because we need to get consumer certificate. This blocks following action */
start = time.Now()
var returnedMsg string
returnedMsg, err = registerRHSM(ctx)
if err != nil {
errorMessages["rhsm"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot connect to Red Hat Subscription Management: %w",
err)}
fmt.Printf(errorPrefix + " Cannot connect to Red Hat Subscription Management\n")
} else {
fmt.Printf(connectedPrefix + " " + returnedMsg + "\n")
}
durations["rhsm"] = time.Since(start)
/* 2. Register insights-client */
if errors, exist := errorMessages["rhsm"]; exist {
if errors.level == log.LevelError {
fmt.Printf(disconnectedPrefix + " Skipping connection to Red Hat Insights\n")
}
} else {
start = time.Now()
err = showProgress(" Connecting to Red Hat Insights...", isColorful, registerInsights)
if err != nil {
errorMessages["insights"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot connect to Red Hat Insights: %w",
err)}
fmt.Printf(errorPrefix + " Cannot connect to Red Hat Insights\n")
} else {
fmt.Printf(connectedPrefix + " Connected to Red Hat Insights\n")
}
durations["insights"] = time.Since(start)
}
/* 3. Start rhcd daemon */
if errors, exist := errorMessages["rhsm"]; exist {
if errors.level == log.LevelError {
fmt.Printf(disconnectedPrefix+" Skipping activation of %v daemon\n", BrandName)
}
} else {
start = time.Now()
progressMessage := fmt.Sprintf(" Activating the %v daemon", BrandName)
err = showProgress(progressMessage, isColorful, activate)
if err != nil {
errorMessages[BrandName] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot activate daemon: %w",
err)}
fmt.Printf(errorPrefix+" Cannot activate the %v daemon\n", BrandName)
} else {
fmt.Printf(connectedPrefix+" Activated the %v daemon\n", BrandName)
}
durations[BrandName] = time.Since(start)
}
/* 4. Show Configuration Profile */
if len(errorMessages) == 0 {
// Update the configuration file
// Call D-bus to get the CA directory from rhsm
if err = getRHSMConfigOption("rhsm.ca_cert_dir", &config.CADir); err != nil {
errorMessages[BrandName] = LogMessage{
level: log.LevelWarn,
message: fmt.Errorf("cannot get rhsm configuration: %w",
err)}
}
// Generate a new http client configured with mutual TLS certificate
tlsConfig, err := config.CreateTLSClientConfig()
if err != nil {
errorMessages[BrandName] = LogMessage{
level: log.LevelWarn,
message: fmt.Errorf("cannot configure TLS: %w",
err)}
}
client := http.NewHTTPClient(tlsConfig)
// Get the user profile
profile, err := getConfProfile(client)
if err != nil {
errorMessages[BrandName] = LogMessage{
level: log.LevelWarn,
message: fmt.Errorf("cannot get the user profile: %w",
err)}
} else {
fmt.Printf(infoPrefix + " Enabled console.redhat.com services: ")
showConfProfile(&profile)
fmt.Printf("\n")
}
fmt.Printf("\nSuccessfully connected to Red Hat!\n")
}
/* 5. Show footer message */
fmt.Printf("\nManage your connected systems: https://red.ht/connector\n")
/* 6. Optionally display duration time of each sub-action */
showTimeDuration(durations)
err = showErrorMessages("connect", errorMessages)
if err != nil {
return err
}
return nil
}
// disconnectAction tries to stop rhscd service, disconnect from Red Hat Insights and finally
// it unregister system from Red Hat Subscription Management
func disconnectAction(ctx *cli.Context) error {
uid := os.Getuid()
if uid != 0 {
return cli.Exit(fmt.Errorf("error: non-root user cannot disconnect system"), 1)
}
var start time.Time
durations := make(map[string]time.Duration)
errorMessages := make(map[string]LogMessage)
hostname, err := os.Hostname()
if err != nil {
return cli.Exit(err, 1)
}
fmt.Printf("Disconnecting %v from %v.\nThis might take a few seconds.\n\n", hostname, Provider)
_, disconnectedPrefix, errorPrefix, _, isColorful := getColorPreferences(ctx)
/* 1. Deactivate rhcd daemon */
start = time.Now()
progressMessage := fmt.Sprintf(" Deactivating the %v daemon", BrandName)
err = showProgress(progressMessage, isColorful, deactivate)
if err != nil {
errorMessages[BrandName] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot deactivate daemon: %w",
err)}
fmt.Printf(errorPrefix+" Cannot deactivate the %v daemon\n", BrandName)
} else {
fmt.Printf(disconnectedPrefix+" Deactivated the %v daemon\n", BrandName)
}
durations[BrandName] = time.Since(start)
/* 2. Disconnect from Red Hat Insights */
start = time.Now()
err = showProgress(" Disconnecting from Red Hat Insights...", isColorful, unregisterInsights)
if err != nil {
errorMessages["insights"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot disconnect from Red Hat Insights: %w",
err)}
fmt.Printf(errorPrefix + " Cannot disconnect from Red Hat Insights\n")
} else {
fmt.Print(disconnectedPrefix + " Disconnected from Red Hat Insights\n")
}
durations["insights"] = time.Since(start)
/* 3. Unregister system from Red Hat Subscription Management */
err = showProgress(" Disconnecting from Red Hat Subscription Management...", isColorful, unregister)
if err != nil {
errorMessages["rhsm"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot disconnect from Red Hat Subscription Management: %w",
err)}
fmt.Printf(errorPrefix + " Cannot disconnect from Red Hat Subscription Management\n")
} else {
fmt.Printf(disconnectedPrefix + " Disconnected from Red Hat Subscription Management\n")
}
durations["rhsm"] = time.Since(start)
fmt.Printf("\nManage your connected systems: https://red.ht/connector\n")
showTimeDuration(durations)
err = showErrorMessages("disconnect", errorMessages)
if err != nil {
return err
}
return nil
}
// canonicalFactAction tries to gather canonical facts about system,
// and it prints JSON with facts to stdout.
func canonicalFactAction(_ *cli.Context) error {
// NOTE: CLI context is not useful for anything
facts, err := GetCanonicalFacts()
if err != nil {
return cli.Exit(err, 1)
}
data, err := json.MarshalIndent(facts, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
// SystemStatus is structure holding information about system status
// When more file format is supported, then add more tags for fields
// like xml:"hostname"
type SystemStatus struct {
SystemHostname string `json:"hostname"`
HostnameError error `json:"hostname_error,omitempty"`
RHSMConnected bool `json:"rhsm_connected"`
RHSMError error `json:"rhsm_error,omitempty"`
InsightsConnected bool `json:"insights_connected"`
InsightsError error `json:"insights_error,omitempty"`
RHCDRunning bool `json:"rhcd_running"`
RHCDError error `json:"rhcd_error,omitempty"`
}
// printJSONStatus tries to print the system status as JSON to stdout.
// When marshaling of systemStatus fails, then error is returned
func printJSONStatus(systemStatus *SystemStatus) error {
data, err := json.MarshalIndent(systemStatus, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
// statusAction tries to print status of system. It means that it gives
// answer on following questions:
// 1. Is system registered to Red Hat Subscription Management?
// 2. Is system connected to Red Hat Insights?
// 3. Is rhcd.service running?
// Status can be printed as human-readable text or machine-readable JSON document.
// Format is influenced by --format json CLI option stored in CLI context
func statusAction(ctx *cli.Context) (err error) {
var systemStatus SystemStatus
machineReadable := false
var machineReadablePrintFunc func(systemStatus *SystemStatus) error
// Only JSON file format is supported ATM
format := ctx.String("format")
if format != "" {
switch format {
case "json":
machineReadable = true
machineReadablePrintFunc = printJSONStatus
default:
err := fmt.Errorf(
"unsuported machine-readable format: %s (supported formats: %s)",
format, "\"json\"",
)
return cli.Exit(err, 1)
}
}
connectedPrefix, disconnectedPrefix, errorPrefix, _, isColorful := getColorPreferences(ctx)
// When printing of status is requested, then print machine-readable file format
// at the end of this function
if machineReadable {
defer func(systemStatus *SystemStatus) {
err = machineReadablePrintFunc(systemStatus)
// When it was not possible to print status to machine-readable format, then
// change returned error to CLI exit error to be able to set exit code to
// a non-zero value
if err != nil {
err = cli.Exit(
fmt.Errorf("unable to print status as %s document: %s", format, err.Error()),
1)
}
}(&systemStatus)
// Disable all colors and animations
isColorful = false
}
hostname, err := os.Hostname()
if err != nil {
if machineReadable {
systemStatus.HostnameError = err
} else {
return cli.Exit(err, 1)
}
}
if machineReadable {
systemStatus.SystemHostname = hostname
} else {
fmt.Printf("Connection status for %v:\n\n", hostname)
}
/* 1. Get Status of RHSM */
uuid, err := getConsumerUUID()
if err != nil {
return cli.Exit(err, 1)
}
if uuid == "" {
if machineReadable {
systemStatus.RHSMConnected = false
} else {
fmt.Printf(disconnectedPrefix + " Not connected to Red Hat Subscription Management\n")
}
} else {
if machineReadable {
systemStatus.RHSMConnected = true
} else {
fmt.Printf(connectedPrefix + " Connected to Red Hat Subscription Management\n")
}
}
/* 2. Get status of insights-client */
var s *spinner.Spinner
if isColorful {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Checking Red Hat Insights..."
s.Start()
}
isRegistered, err := insightsIsRegistered()
if isColorful {
s.Stop()
}
if isRegistered {
if machineReadable {
systemStatus.InsightsConnected = true
} else {
fmt.Print(connectedPrefix + " Connected to Red Hat Insights\n")
}
} else {
if err == nil {
if machineReadable {
systemStatus.InsightsConnected = false
} else {
fmt.Print(disconnectedPrefix + " Not connected to Red Hat Insights\n")
}
} else {
if machineReadable {
systemStatus.InsightsConnected = false
systemStatus.InsightsError = err
} else {
fmt.Printf(errorPrefix+" Cannot execute insights-client: %v\n", err)
}
}
}
/* 3. Get status of rhcd */
conn, err := systemd.NewSystemConnection()
if err != nil {
systemStatus.RHCDRunning = false
systemStatus.RHCDError = err
return cli.Exit(err, 1)
}
defer conn.Close()
unitName := ShortName + "d.service"
properties, err := conn.GetUnitProperties(unitName)
if err != nil {
systemStatus.RHCDRunning = false
systemStatus.RHCDError = err
return cli.Exit(err, 1)
}
activeState := properties["ActiveState"]
if activeState.(string) == "active" {
if machineReadable {
systemStatus.RHCDRunning = true
} else {
fmt.Printf(connectedPrefix+" The %v daemon is active\n", BrandName)
}
} else {
if machineReadable {
systemStatus.RHCDRunning = false
} else {
fmt.Printf(disconnectedPrefix+" The %v daemon is inactive\n", BrandName)
}
}
if !machineReadable {
fmt.Printf("\nManage your connected systems: https://red.ht/connector\n")
}
return nil
}
// mainAction is triggered in the case, when no sub-command is specified
func mainAction(c *cli.Context) error {
type GenerationFunc func() (string, error)
var generationFunc GenerationFunc
if c.Bool("generate-man-page") {
generationFunc = c.App.ToMan
} else if c.Bool("generate-markdown") {
generationFunc = c.App.ToMarkdown
} else {
cli.ShowAppHelpAndExit(c, 0)
}
data, err := generationFunc()
if err != nil {
return cli.Exit(err, 1)
}
fmt.Println(data)
return nil
}
// beforeAction is triggered before other actions are triggered
func beforeAction(c *cli.Context) error {
/* Load the configuration values from the config file specified*/
filePath := c.String("config")
if filePath != "" {
inputSource, err := altsrc.NewTomlSourceFromFile(filePath)
if err != nil {
return err
}
if err := altsrc.ApplyInputSourceValues(c, inputSource, c.App.Flags); err != nil {
return err
}
}
config = Conf{
LogLevel: c.String(cliLogLevel),
CertFile: c.String(cliCertFile),
KeyFile: c.String(cliKeyFile),
}
level, err := log.ParseLevel(config.LogLevel)
if err != nil {
return cli.Exit(err, 1)
}
log.SetLevel(level)
// When environment variable NO_COLOR or --no-color CLI option is set, then do not display colors
// and animations too. The NO_COLOR environment variable have to have value "1" or "true",
// "True", "TRUE" to take effect
// When no-color is not set, then try to detect if the output goes to some file. In this case
// colors nor animations will not be printed to file.
if !isTerminal(os.Stdout.Fd()) {
err := c.Set("no-color", "true")
if err != nil {
log.Debug("Unable to set no-color flag to \"true\"")
}
}
return nil
}
var config = Conf{}
func main() {
app := cli.NewApp()
app.Name = ShortName
app.Version = Version
app.Usage = "control the system's connection to " + Provider
app.Description = "The " + app.Name + " command controls the system's connection to " + Provider + ".\n\n" +
"To connect the system using an activation key:\n" +
"\t" + app.Name + " connect --organization ID --activation-key KEY\n\n" +
"To connect the system using a username and password:\n" +
"\t" + app.Name + " connect --username USERNAME --password PASSWORD\n\n" +
"To disconnect the system:\n" +
"\t" + app.Name + " disconnect\n\n" +
"Run '" + app.Name + " command --help' for more details."
log.SetFlags(0)
log.SetPrefix("")
defaultConfigFilePath, err := ConfigPath()
if err != nil {
log.Fatal(err)
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "generate-man-page",
Hidden: true,
},
&cli.BoolFlag{
Name: "generate-markdown",
Hidden: true,
},
&cli.BoolFlag{
Name: "no-color",
Hidden: false,
Value: false,
EnvVars: []string{"NO_COLOR"},
},
&cli.StringFlag{
Name: "config",
Hidden: true,
Value: defaultConfigFilePath,
TakesFile: true,
Usage: "Read config values from `FILE`",
},
altsrc.NewStringFlag(&cli.StringFlag{
Name: cliCertFile,
Hidden: true,
Usage: "Use `FILE` as the client certificate",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: cliKeyFile,
Hidden: true,
Usage: "Use `FILE` as the client's private key",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: cliLogLevel,
Value: "info",
Hidden: true,
Usage: "Set the logging output level to `LEVEL`",
}),
}
app.Commands = []*cli.Command{
{
Name: "connect",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Usage: "register with `USERNAME`",
Aliases: []string{"u"},
},
&cli.StringFlag{
Name: "password",
Usage: "register with `PASSWORD`",
Aliases: []string{"p"},
},
&cli.StringFlag{
Name: "organization",
Usage: "register with `ID`",
Aliases: []string{"o"},
},
&cli.StringSliceFlag{
Name: "activation-key",
Usage: "register with `KEY`",
Aliases: []string{"a"},
},
&cli.StringFlag{
Name: "server",
Hidden: true,
Usage: "register against `URL`",
},
},
Usage: "Connects the system to " + Provider,
UsageText: fmt.Sprintf("%v connect [command options]", app.Name),
Description: fmt.Sprintf("The connect command connects the system to Red Hat Subscription Management, Red Hat Insights and %v and activates the %v daemon that enables %v to interact with the system. For details visit: https://red.ht/connector", Provider, BrandName, Provider),
Action: connectAction,
},
{
Name: "disconnect",
Usage: "Disconnects the system from " + Provider,
UsageText: fmt.Sprintf("%v disconnect", app.Name),
Description: fmt.Sprintf("The disconnect command disconnects the system from Red Hat Subscription Management, Red Hat Insights and %v and deactivates the %v daemon. %v will no longer be able to interact with the system.", Provider, BrandName, Provider),
Action: disconnectAction,
},
{
Name: "canonical-facts",
Hidden: true,
Usage: "Prints canonical facts about the system.",
UsageText: fmt.Sprintf("%v canonical-facts", app.Name),
Description: fmt.Sprintf("The canonical-facts command prints data that uniquely identifies the system in the %v inventory service. Use only as directed for debugging purposes.", Provider),
Action: canonicalFactAction,
},
{
Name: "status",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Usage: "prints status in machine-readable format (supported formats: \"json\")",
Aliases: []string{"f"},
},
},
Usage: "Prints status of the system's connection to " + Provider,
UsageText: fmt.Sprintf("%v status", app.Name),
Description: fmt.Sprintf("The status command prints the state of the connection to Red Hat Subscription Management, Red Hat Insights and %v.", Provider),
Action: statusAction,
},
}
app.EnableBashCompletion = true
app.BashComplete = BashComplete
app.Action = mainAction
app.Before = beforeAction
if err := app.Run(os.Args); err != nil {
log.Error(err)
}
}