This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/edgexfoundry/edgex-cli into…
… add-core-command � Conflicts: � internal/cmd/common.go
- Loading branch information
Showing
12 changed files
with
713 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,23 @@ | ||
## PR Checklist | ||
Please check if your PR fulfills the following requirements: | ||
|
||
- [ ] Tests for the changes have been added (for bug fixes / features) | ||
- [ ] Docs have been added / updated (for bug fixes / features) | ||
|
||
**If your build fails** due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/edgex-cli/blob/master/.github/Contributing.md. | ||
|
||
## What is the current behavior? | ||
<!-- Please describe the current behavior and link to a relevant issue. --> | ||
|
||
|
||
## Issue Number: | ||
<!-- Expected Commit Message Description (imported automatically by GitHub) --> | ||
<!-- Must conform to [conventional commits guidelines](https://github.com/edgexfoundry/edgex-cli/blob/main/.github/Contributing.md) --> | ||
<!-- Expected Commit message must contain Closes/Fixes #IssueNumber statement when there is a related issue --> | ||
|
||
<!-- Add additional detailed description of need for change if no related issue --> | ||
|
||
## What is the new behavior? | ||
**If your build fails** due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/edgex-cli/blob/main/.github/Contributing.md | ||
|
||
## PR Checklist | ||
Please check if your PR fulfills the following requirements: | ||
|
||
## Does this PR introduce a breaking change? | ||
<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. --> | ||
|
||
- [ ] Yes | ||
- [ ] No | ||
|
||
## New Imports | ||
<!-- Are there any new imports or modules? If so, what are they used for and why? --> | ||
|
||
- [ ] Yes | ||
- [ ] No | ||
|
||
## Specific Instructions | ||
Are there any specific instructions or things that should be known prior to reviewing? | ||
- [ ] I am not introducing a breaking change (if you are, flag in conventional commit message with `BREAKING CHANGE:` describing the break) | ||
- [ ] I am not introducing a new dependency (add notes below if you are) | ||
- [ ] I have added unit tests for the new feature or bug fix (if not, why?) | ||
- [ ] I have fully tested (add details below) this the new feature or bug fix (if not, why?) | ||
- [ ] I have opened a PR for the related docs change (if not, why?) | ||
<link to docs PR> | ||
|
||
## Testing Instructions | ||
<!-- How can the reviewers test your change? --> | ||
|
||
## Other information | ||
## New Dependency Instructions (If applicable) | ||
<!-- Please follow [vetting instructions](https://wiki.edgexfoundry.org/display/FA/Vetting+Process+for+3rd+Party+Dependencies) and place results here --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/* | ||
* Copyright (C) 2021 Canonical Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0' | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"text/tabwriter" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var eventLimit, eventOffset int | ||
var eventDevice, eventProfile, eventSource, readingsValueType string | ||
var eventAge int | ||
var numberOfReadings int | ||
|
||
func init() { | ||
eventCmd := initEventCommand() | ||
initListEventCommand(eventCmd) | ||
initCountEventCommand(eventCmd) | ||
initRmEventCommand(eventCmd) | ||
initAddEventCommand(eventCmd) | ||
} | ||
|
||
func initEventCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "event", | ||
Short: "Add, remove and list events", | ||
Long: ``, | ||
SilenceUsage: true, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
return cmd | ||
} | ||
|
||
func initListEventCommand(cmd *cobra.Command) { | ||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List events", | ||
Long: `List all events, optionally specifying a limit and offset`, | ||
RunE: handleListEvents, | ||
SilenceUsage: true, | ||
} | ||
|
||
cmd.AddCommand(listCmd) | ||
addFormatFlags(listCmd) | ||
addVerboseFlag(listCmd) | ||
listCmd.Flags().IntVarP(&eventLimit, "limit", "l", 50, "The number of items to return. Specifying -1 will return all remaining items") | ||
listCmd.Flags().IntVarP(&eventOffset, "offset", "o", 0, "The number of items to skip") | ||
} | ||
|
||
func initCountEventCommand(cmd *cobra.Command) { | ||
var countCmd = &cobra.Command{ | ||
Use: "count", | ||
Short: "Count available events", | ||
Long: `Count the number of events in core data, optionally filtering by device name`, | ||
RunE: handleCountEvents, | ||
SilenceUsage: true, | ||
} | ||
|
||
countCmd.Flags().StringVarP(&eventDevice, "device", "d", "", "Device name") | ||
cmd.AddCommand(countCmd) | ||
addFormatFlags(countCmd) | ||
} | ||
|
||
func initRmEventCommand(cmd *cobra.Command) { | ||
var rmCmd = &cobra.Command{ | ||
Use: "rm", | ||
Short: "Remove events", | ||
Long: `Remove events, specifying either device name or maximum event age in milliseconds | ||
'edgex-cli event rm --device {devicename}' removes all events for the specified device | ||
'edgex-cli event rm --age {ms}' removes all events generated in the last {ms} milliseconds`, | ||
RunE: handleRmEvents, | ||
SilenceUsage: true, | ||
} | ||
|
||
rmCmd.Flags().StringVarP(&eventDevice, "device", "d", "", "Device name") | ||
rmCmd.Flags().IntVarP(&eventAge, "age", "a", 0, "Event age (in milliseconds)") | ||
cmd.AddCommand(rmCmd) | ||
} | ||
|
||
func initAddEventCommand(cmd *cobra.Command) { | ||
var addCmd = &cobra.Command{ | ||
Use: "add", | ||
Short: "Create an event", | ||
Long: `Create an event with a specified number of random readings`, | ||
RunE: handleAddEvents, | ||
SilenceUsage: true, | ||
} | ||
addCmd.Flags().StringVarP(&eventDevice, "device", "d", "", "Device name") | ||
addCmd.Flags().StringVarP(&eventProfile, "profile", "p", "", "Profile name") | ||
addCmd.Flags().StringVarP(&readingsValueType, "type", "t", "string", "Readings value type [bool | string | uint8 | uint16 | uint32 | uint64 | int8 | int16 | int32 | int64 | float32 | float64 ]") | ||
addCmd.Flags().StringVarP(&eventSource, "source", "s", "", "Event source name (ResourceName or CommandName)") | ||
addCmd.Flags().IntVarP(&numberOfReadings, "readings", "r", 1, "Number of sample readings to create") | ||
addCmd.MarkFlagRequired("device") | ||
addCmd.MarkFlagRequired("profile") | ||
addCmd.MarkFlagRequired("source") | ||
cmd.AddCommand(addCmd) | ||
} | ||
|
||
func handleAddEvents(cmd *cobra.Command, args []string) error { | ||
id, err := getCoreDataService().CreateEvent(eventDevice, eventProfile, eventSource, readingsValueType, numberOfReadings) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Event " + id + " created") | ||
return nil | ||
} | ||
|
||
func handleRmEvents(cmd *cobra.Command, args []string) error { | ||
err := getCoreDataService().RemoveEvents(eventDevice, eventAge) | ||
return err | ||
} | ||
|
||
func handleCountEvents(cmd *cobra.Command, args []string) error { | ||
if json { | ||
var json string | ||
var err error | ||
|
||
if eventDevice != "" { | ||
json, _, err = getCoreDataService().CountEventsByDeviceJSON(eventDevice) | ||
} else { | ||
json, _, err = getCoreDataService().CountEventsJSON() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
fmt.Print(json) | ||
|
||
} else { | ||
count, err := getCoreDataService().CountEvents(eventDevice) | ||
if err != nil { | ||
return err | ||
} | ||
if eventDevice != "" { | ||
fmt.Printf("Total %s events: %v\n", eventDevice, count.Count) | ||
} else { | ||
fmt.Printf("Total events: %v\n", count.Count) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func handleListEvents(cmd *cobra.Command, args []string) error { | ||
var err error | ||
|
||
if json { | ||
var json string | ||
|
||
json, _, err = getCoreDataService().ListAllEventsJSON(eventOffset, eventLimit) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print(json) | ||
|
||
} else { | ||
events, err := getCoreDataService().ListAllEvents(eventOffset, eventLimit) | ||
if err != nil { | ||
return err | ||
} | ||
if len(events) == 0 { | ||
fmt.Println("No events available") | ||
return nil | ||
} | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 1, 1, 2, ' ', 0) | ||
if verbose { | ||
fmt.Fprintln(w, "Origin\tDevice\tProfile\tSource\tId\tVersionable\tReadings") | ||
for _, event := range events { | ||
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\n", | ||
time.Unix(0, event.Origin).Format(time.RFC822), | ||
event.DeviceName, | ||
event.ProfileName, | ||
event.SourceName, | ||
event.Id, | ||
event.Versionable, | ||
event.Readings) | ||
} | ||
|
||
} else { | ||
fmt.Fprintln(w, "Origin\tDevice\tProfile\tSource\tNumber of readings") | ||
for _, event := range events { | ||
tm := time.Unix(0, event.Origin) | ||
sTime := tm.Format(time.RFC822) | ||
nReadings := 0 | ||
if event.Readings != nil { | ||
nReadings = len(event.Readings) | ||
} | ||
fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%d\n", | ||
sTime, event.DeviceName, event.ProfileName, event.SourceName, nReadings) | ||
} | ||
} | ||
w.Flush() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.