Skip to content

Commit b17f8a0

Browse files
author
Guillaume Lefranc
committed
Add topology command
1 parent 7d97353 commit b17f8a0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

topology.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77

88
"github.com/go-sql-driver/mysql"
9+
"github.com/spf13/cobra"
910
"github.com/tanji/mariadb-tools/dbhelper"
1011
)
1112

@@ -14,6 +15,10 @@ type topologyError struct {
1415
Msg string
1516
}
1617

18+
func init() {
19+
rootCmd.AddCommand(topologyCmd)
20+
}
21+
1722
func (e topologyError) Error() string {
1823
return fmt.Sprintf("%v [#%v]", e.Msg, e.Code)
1924
}
@@ -148,3 +153,25 @@ func printTopology() {
148153
logprintf("DEBUG: Server [%d] %s %s", k, v.URL, v.State)
149154
}
150155
}
156+
157+
var topologyCmd = &cobra.Command{
158+
Use: "topology",
159+
Short: "Print replication topology",
160+
Long: `Print the replication topology by detecting master and slaves`,
161+
Run: func(cmd *cobra.Command, args []string) {
162+
repmgrFlagCheck()
163+
err := topologyInit()
164+
if err != nil {
165+
log.Fatalln(err)
166+
}
167+
for _, v := range servers {
168+
fmt.Println(v.URL, v.State)
169+
}
170+
},
171+
PostRun: func(cmd *cobra.Command, args []string) {
172+
// Close connections on exit.
173+
for _, server := range servers {
174+
defer server.Conn.Close()
175+
}
176+
},
177+
}

0 commit comments

Comments
 (0)