...
1 package cmdopts
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "net/url"
8
9 "github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
10 )
11
12 type SourceCommand struct {
13 owner *Options
14 Ping SourcePingCommand `command:"ping" description:"Try to connect to configured sources, report errors if any and then exit"`
15 Resolve SourceResolveCommand `command:"resolve" description:"Resolve monitored connections for a given sources (all by default)"`
16
17 }
18
19 func NewSourceCommand(owner *Options) *SourceCommand {
20 return &SourceCommand{
21 owner: owner,
22 Ping: SourcePingCommand{owner: owner},
23 Resolve: SourceResolveCommand{owner: owner},
24 }
25 }
26
27 type SourcePingCommand struct {
28 owner *Options
29 }
30
31 func (cmd *SourcePingCommand) Execute(args []string) error {
32 err := cmd.owner.InitSourceReader(context.Background())
33 if err != nil {
34 return err
35 }
36 srcs, err := cmd.owner.SourcesReaderWriter.GetSources()
37 if err != nil {
38 return err
39 }
40 var foundSources sources.Sources
41 if len(args) == 0 {
42 foundSources = srcs
43 } else {
44 for _, name := range args {
45 for _, s := range srcs {
46 if s.Name == name {
47 foundSources = append(foundSources, s)
48 }
49 }
50 }
51 }
52 var e error
53 for _, s := range foundSources {
54 switch s.Kind {
55 case sources.SourcePatroni, sources.SourcePatroniContinuous, sources.SourcePatroniNamespace:
56 _, e = sources.ResolveDatabasesFromPatroni(s)
57 case sources.SourcePostgresContinuous:
58 _, e = sources.ResolveDatabasesFromPostgres(s)
59 default:
60 mdb := &sources.SourceConn{Source: s}
61 e = mdb.Connect(context.Background(), cmd.owner.Sources)
62 }
63 if e != nil {
64 fmt.Printf("FAIL:\t%s (%s)\n", s.Name, e)
65 } else {
66 fmt.Printf("OK:\t%s\n", s.Name)
67 }
68 err = errors.Join(err, e)
69 }
70
71
72
73 cmd.owner.CompleteCommand(map[bool]int32{true: ExitCodeCmdError, false: ExitCodeOK}[err != nil])
74 return nil
75 }
76
77 type SourceResolveCommand struct {
78 owner *Options
79 }
80
81 func (cmd *SourceResolveCommand) Execute(args []string) error {
82 err := cmd.owner.InitSourceReader(context.Background())
83 if err != nil {
84 return err
85 }
86 srcs, err := cmd.owner.SourcesReaderWriter.GetSources()
87 if err != nil {
88 return err
89 }
90 var foundSources sources.Sources
91 if len(args) == 0 {
92 foundSources = srcs
93 } else {
94 for _, name := range args {
95 for _, s := range srcs {
96 if s.Name == name {
97 foundSources = append(foundSources, s)
98 }
99 }
100 }
101 }
102 conns, err := foundSources.ResolveDatabases()
103 if err != nil {
104 return err
105 }
106 var connstr url.URL
107 connstr.Scheme = "postgresql"
108 for _, s := range conns {
109 if s.ConnStr > "" {
110 fmt.Printf("%s=%s\n", s.Name, s.ConnStr)
111 } else {
112 connstr.Host = fmt.Sprintf("%s:%d", s.ConnConfig.ConnConfig.Host, s.ConnConfig.ConnConfig.Port)
113 connstr.User = url.UserPassword(s.ConnConfig.ConnConfig.User, s.ConnConfig.ConnConfig.Password)
114 connstr.Path = s.ConnConfig.ConnConfig.Database
115 fmt.Printf("%s=%s\n", s.Name, connstr.String())
116 }
117 }
118 cmd.owner.CompleteCommand(ExitCodeOK)
119 return nil
120 }
121