|
| 1 | +// Copyright 2024 Nokia |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/sdcio/data-server/pkg/utils" |
| 23 | + sdcpb "github.com/sdcio/sdc-protos/sdcpb" |
| 24 | + "github.com/spf13/cobra" |
| 25 | +) |
| 26 | + |
| 27 | +// datastoreWatchDeviationCmd represents the watch deviation command |
| 28 | +var datastoreWatchDeviationCmd = &cobra.Command{ |
| 29 | + Use: "watch-dev", |
| 30 | + Short: "watch deviation datastores", |
| 31 | + SilenceUsage: true, |
| 32 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 33 | + ctx, cancel := context.WithCancel(cmd.Context()) |
| 34 | + defer cancel() |
| 35 | + dataClient, err := createDataClient(ctx, addr) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + req := &sdcpb.WatchDeviationRequest{Name: []string{datastoreName}} |
| 40 | + stream, err := dataClient.WatchDeviations(ctx, req) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + count := 0 |
| 45 | + for { |
| 46 | + rsp, err := stream.Recv() |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + switch format { |
| 51 | + case "": |
| 52 | + switch rsp.Event { |
| 53 | + case sdcpb.DeviationEvent_START: |
| 54 | + fmt.Printf("%s: %s: %s\n", |
| 55 | + rsp.GetName(), rsp.GetIntent(), rsp.GetEvent()) |
| 56 | + case sdcpb.DeviationEvent_END: |
| 57 | + fmt.Printf("%s: %s: %s\n", |
| 58 | + rsp.GetName(), rsp.GetIntent(), rsp.GetEvent()) |
| 59 | + |
| 60 | + fmt.Printf("Received %d deviations\n", count) |
| 61 | + count = 0 |
| 62 | + default: |
| 63 | + count++ |
| 64 | + fmt.Printf("%s: %s: %s: %s: %s : %s -> %s\n", |
| 65 | + rsp.GetName(), rsp.GetIntent(), rsp.GetEvent(), |
| 66 | + rsp.GetReason(), utils.ToXPath(rsp.GetPath(), false), |
| 67 | + rsp.GetExpectedValue(), rsp.GetCurrentValue()) |
| 68 | + } |
| 69 | + case "json": |
| 70 | + b, err := json.MarshalIndent(rsp, "", " ") |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + fmt.Println(string(b)) |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +func init() { |
| 81 | + datastoreCmd.AddCommand(datastoreWatchDeviationCmd) |
| 82 | +} |
0 commit comments