@@ -18,7 +18,10 @@ package info
1818import (
1919 "context"
2020 "fmt"
21+ "maps"
22+ "slices"
2123
24+ liqov1beta1 "github.com/liqotech/liqo/apis/core/v1beta1"
2225 "github.com/liqotech/liqo/pkg/liqoctl/factory"
2326)
2427
@@ -41,9 +44,10 @@ var LocalInfoQueryShortcuts = map[string]string{
4144type Options struct {
4245 * factory.Factory
4346
44- Verbose bool
45- Format OutputFormat
46- GetQuery string
47+ Verbose bool
48+ Format OutputFormat
49+ GetQuery string
50+ ClustersInfo map [liqov1beta1.ClusterID ]* liqov1beta1.ForeignCluster
4751}
4852
4953// NewOptions returns a new Options struct.
@@ -80,9 +84,11 @@ func (o *Options) RunInfo(ctx context.Context, checkers []Checker) error {
8084 return nil
8185 // If query specified try to retrieve the field from the output
8286 case o .GetQuery != "" :
83- output , err = o .sPrintField (o .GetQuery , checkers , LocalInfoQueryShortcuts )
87+ data := o .collectDataFromCheckers (checkers )
88+ output , err = o .sPrintField (o .GetQuery , data , LocalInfoQueryShortcuts )
8489 default :
85- output , err = o .sPrintMachineReadable (checkers )
90+ data := o .collectDataFromCheckers (checkers )
91+ output , err = o .sPrintMachineReadable (data )
8692 }
8793
8894 if err != nil {
@@ -95,6 +101,71 @@ func (o *Options) RunInfo(ctx context.Context, checkers []Checker) error {
95101}
96102
97103// RunPeerInfo execute the `info peer` command.
98- func (o * Options ) RunPeerInfo (_ context.Context , _ []string ) error {
99- panic ("Not implemented" )
104+ func (o * Options ) RunPeerInfo (ctx context.Context , checkers []MultiClusterChecker , clusterIDs []string ) error {
105+ // Check whether Liqo is installed in the current cluster
106+ if err := o .installationCheck (ctx ); err != nil {
107+ return err
108+ }
109+
110+ if err := o .getForeignClusters (ctx , clusterIDs ); err != nil {
111+ return err
112+ }
113+
114+ // Start collecting the data via the checkers
115+ for i := range checkers {
116+ checkers [i ].Collect (ctx , * o )
117+ for _ , err := range checkers [i ].GetCollectionErrors () {
118+ o .Printer .Warning .Println (err )
119+ }
120+ }
121+
122+ var err error
123+ var output string
124+ switch {
125+ // If no format is specified, format and print a user-friendly output
126+ case o .Format == "" && o .GetQuery == "" :
127+ clustersCounter := 0
128+ nPeers := len (o .ClustersInfo )
129+ for clusterID := range o .ClustersInfo {
130+ for i := range checkers {
131+ o .Printer .BoxSetTitle (checkers [i ].GetTitle ())
132+ o .Printer .BoxPrintln (checkers [i ].FormatForClusterID (clusterID , * o ))
133+ }
134+
135+ clustersCounter ++
136+ if clustersCounter < nPeers {
137+ fmt .Printf ("\n \n " )
138+ }
139+ }
140+ return nil
141+ // If query specified try to retrieve the field from the output
142+ case o .GetQuery != "" :
143+ data , _ := o .collectDataFromMultiClusterCheckers (checkers )
144+
145+ selectedClusterIDs := slices .Collect (maps .Keys (o .ClustersInfo ))
146+ // Get the cluster selected by the query
147+ query , selectedCluster := o .getClusterFromQuery (o .GetQuery , selectedClusterIDs )
148+
149+ // Get the field from the cluster data
150+ if showData , ok := data [liqov1beta1 .ClusterID (selectedCluster )]; ok {
151+ output , err = o .sPrintField (query , showData , nil )
152+ } else {
153+ err = fmt .Errorf (
154+ "cluster %q in query %q is not among the requested clusters" ,
155+ selectedCluster ,
156+ o .GetQuery ,
157+ )
158+ }
159+ default :
160+ data , _ := o .collectDataFromMultiClusterCheckers (checkers )
161+ output , err = o .sPrintMachineReadable (data )
162+ }
163+
164+ if err != nil {
165+ o .Printer .Error .Println (err )
166+ } else {
167+ fmt .Println (output )
168+ }
169+
170+ return err
100171}
0 commit comments