Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
feat: add support for common v2 endpoints (ping/metrics/version/config)
Browse files Browse the repository at this point in the history
This commit adds v2 (Ireland/Jakarta) support for four common endpoints:
- version
- metrics
- ping
- config

For information about the available commands, use `edgex-cli -h`

There are a number of ways to use the commands:
1. `edgex-cli version` displays version of each active microservice.
The version commands just shows the version string. The other commands
use a pretty-printed JSON structure.

2. `edgex-cli version -d` shows debug output - the URL used and the result

3. `edgex-cli version -j` gets the raw JSON output for one service (by default core-data)

It's also possible to specify the service to use. : `-m/--metadata`, `-c/--command`,
 `-n/--notification`, `-s/--scheduler` or `--data` (which is the default).
This can also be combined with `-j` or `-d`

Example:
- `$ edgex-cli metrics -j -m`
- `$ edgex-cli ping -d --command`

Help is available with
```
$ edgex-cli help
$ edgex-cli version --help
$ edgex-cli version --h
```

Signed-off-by: Siggi Skulason <siggi.skulason@canonical.com>
  • Loading branch information
Siggi Skulason committed Sep 17, 2021
1 parent d0bd38e commit feebb32
Show file tree
Hide file tree
Showing 50 changed files with 616 additions and 3,060 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
edgex-cli
edgex
edgex-cli
go.sum
coverage.out

Expand Down
21 changes: 11 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@ ifndef GOBIN
endif

BINARY=edgex-cli

VERSION=$(shell cat ./VERSION 2>/dev/null || echo 0.0.0)
GOFLAGS=-ldflags "-X github.com/edgexfoundry/edgex-cli/cmd/version.Version=$(VERSION)"
TIME=$(shell date)
GOFLAGS=-ldflags "-X 'github.com/edgexfoundry/edgex-cli.BuildVersion=$(VERSION)' -X 'github.com/edgexfoundry/edgex-cli.BuildTime=$(TIME)'"
ARTIFACT_ROOT?=bin

build:
@echo "GOPATH=$(GOPATH)"
$(GO) build -o $(BINARY) $(GOFLAGS)
$(GO) build -o ${ARTIFACT_ROOT}/$(BINARY) $(GOFLAGS) ./cmd/edgex-cli

tidy:
go mod tidy

# initial impl. Feel free to override. Please keep ARTIFACT_ROOT coming from env though. CI/CD pipeline relies on this
build-all:
@echo "GOPATH=$(GOPATH)"

GOOS=linux GOARCH=amd64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY)-linux-amd64 $(GOFLAGS)
GOOS=linux GOARCH=arm64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY)-linux-arm64 $(GOFLAGS)
GOOS=darwin GOARCH=amd64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY)-mac $(GOFLAGS)
GOOS=windows GOARCH=amd64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY).exe $(GOFLAGS)
GOOS=linux GOARCH=amd64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY)-linux-amd64 $(GOFLAGS) ./cmd/edgex-cli
GOOS=linux GOARCH=arm64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY)-linux-arm64 $(GOFLAGS) ./cmd/edgex-cli
GOOS=darwin GOARCH=amd64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY)-mac $(GOFLAGS) ./cmd/edgex-cli
GOOS=windows GOARCH=amd64 $(GO) build -o ${ARTIFACT_ROOT}/$(BINARY).exe $(GOFLAGS) ./cmd/edgex-cli

tar -czvf ${ARTIFACT_ROOT}/$(BINARY)-linux-amd64-$(VERSION).tar.gz Attribution.txt LICENSE res/sample-configuration.toml ${ARTIFACT_ROOT}/$(BINARY)-linux-amd64 --transform 's/-linux-amd64//'
tar -czvf ${ARTIFACT_ROOT}/$(BINARY)-linux-arm64-$(VERSION).tar.gz Attribution.txt LICENSE res/sample-configuration.toml ${ARTIFACT_ROOT}/$(BINARY)-linux-arm64 --transform 's/-linux-arm64//'
Expand All @@ -49,9 +53,6 @@ test:
install:
@echo "GOBIN=$(GOBIN)"
$(GO) install $(GOFLAGS)
mkdir -p $(HOME)/.edgex-cli
cp ./res/sample-configuration.toml $(HOME)/.edgex-cli/configuration.toml
@echo "Configuration file $(HOME)/.edgex-cli/configuration.toml created"

uninstall:
@echo "GOBIN=$(GOBIN)"
Expand Down
223 changes: 1 addition & 222 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,225 +5,4 @@

A command line interface to interact with EdgeX microservices. Replaces the need to manually construct complex CURL commands and/or maintain developer scripts.

```
______ _ __ __ _____ _ _____
| ____| | | \ \ / / / ____|| | |_ _|
| |__ __| | __ _ ___ \ V / ______ | | | | | |
| __| / _` | / _` | / _ \ > < |______| | | | | | |
| |____| (_| || (_| || __/ / . \ | |____ | |____ _| |_
|______|\__,_| \__, | \___|/_/ \_\ \_____||______||_____|
__/ |
|___/
EdgeX CLI version: 0.0.1
https://www.edgexfoundry.org/
Usage:
edgex-cli [command]
Available Commands:
addressable Addressable command command
command `Command` command
db Purges entire EdgeX Database. [USE WITH CAUTION]
device Device command
deviceservice Device service command
event Event command
help Help about any command
interval Interval command
notification Notification command
profile Device profile command.
reading Reading command
status Checks the current status of each microservice.
subscription Subscription command
version Version command
Flags:
--config-file string configuration file
-h, --help help for edgex-cli
--no-pager Do not pipe output into a pager.
-u, --url Print URL(s) used by the entered command.
-v, --verbose Print entire HTTP response.
Use "edgex-cli [command] --help" for more information about a command.
```

## Installation

In order to run this tool, you will need a **locally running EdgeX instance**, accessible via localhost,
and Go 1.12 or higher installed on your machine.

* Clone the git repo:

```
$ git clone https://github.com/edgexfoundry/edgex-cli
```

* Change directory:

```
$ cd edgex-cli
```

* Install the CLI:

```
$ make install
```
Install also makes a copy of the default configuration and copies it to $HOME/.edgex-cli/configuration.toml.
If your EdgeX instance is not running on localhost, minimally you will need to replace localhost with the correct IP address.
You can now use the CLI by entering `edgex-cli` anywhere on your machine provided your $GOBIN is on your $PATH.


#### Interactive Mode

Some commands leverage interactive-mode which opens an editor and allows you to provide information that would
normally be difficult with just command line arguments. For example, creating an Event requires a lot of information,
also Events contain zero or more readings. Using interactive mode, you can easily create an Event with many readings and
customize each reading. You can choose the editor that is used by setting the environment variable `EDITOR`. The default
editor is `Vi` for `Unix` operating systems(MacOS, Linux, etc), `Notepad` for Windows OS. The default editor is used if
no `EDITOR` is specified. Some examples of editors:

- vi
- vim
- nano
- emacs
- notepad
- vscode
- atom


### CLI Developers

To try out your changes you have two options, one using 'make build', the other 'go run'. Also, we share how to launch tests.

* Build and run:

```
$ make build
$ ./edgex-cli
```

* Use `go run`:

```
$ go run main.go [COMMAND]
```
* Build artifacts and create tar files for different OS
```
make build-all
```
* Running tests:

```
$ make test
```

This will generate the file coverage.out in the repository root directory. To view the results, execute:

```
$ go tool cover -html=coverage.out
```

#### Code Organization
All CLI go code lives under the "cmd" directory. Its sub-directories map to the supported toplevel commands, such as
addressable, command, db, device, ... version. To obtain a full list of supported commands type 'edgex-cli --help'.

Our convention has been to use the -f flag to pass in a file argument. -n is typically used to provide a name.
Both command specific and global flags exist.
Refer to the Cobra (https://godoc.org/github.com/spf13/cobra) and Viper (https://godoc.org/github.com/spf13/viper) documentation for additional help.


#### Sample Templates
The "samples" directory holds templates for device profile (createDP.json and yaml),
device (createDevice.toml), intervals (createInterval.toml and json), and for updateInterval.
CLI in Interactive mode opens the relevant template in the configured editor.

Edit these should data structures change.
For legacy reasons we support multiple formats in the case of some objects. Going forward most likely only json format will be supported.

## Supported commands and sub-commands

```
addressable
list A list of all addressable
```
```
command
get Issue GET command
list A list of device supported commands
put Issue PUT command
```
```
db
purge - Purges entire EdgeX Database. [USE WITH CAUTION]
```

```
device
add Add devices
adminstate Device admin state
list A list of all devices
operstate Update deviceName operating state
rm Removes device by name or ID
update Update a device
```

```
deviceservice
add Add a device service
list Lists existing devices services
rm Removes device service by name or ID
update Update device service
```
```
event
add Create an event
count Returns the count of core-data events
list A list of Events
rm Removes event by its id or removes all events generated by given device
scrub Remove all (pushed) events and their associated readings [USE WITH CAUTION]
```
```
interval
add Add interval
list A list of all intervals
rm Removes interval by name or id
update Update interval
```
```
notification
add Add notification
list A list of all notifications
rm Removes notification by slug or age
```
```
profile
add Add profiles
list Returns a list of device profiles
rm Remove profile by name or ID
update Update device profile
```
```
reading
count Returns the count of core-data readings
list A list of readings across devices or pertaining to a specified device
```
```
status Checks the current status of each microservice
```
```
subscription
add Add subscription
list A list of all subscriptions
rm Removes subscription by --slug or id.
```
```
version Version command
```
```
watcher
add Add watcher(s)
list A list of watchers
rm Remove watcher(s) by ID(s)
```
`edgex-cli` is being updated to support the V2 API and the documentation will be updated in due course. See the [EdgeXFoundry CLI documentation](https://docs.edgexfoundry.org/1.3/getting-started/tools/Ch-CommandLineInterface/) for more information about the V1.3 (Hanoi) client.
25 changes: 25 additions & 0 deletions cmd/edgex-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 main

import (
"github.com/edgexfoundry/edgex-cli/internal/cmd"
)

func main() {
cmd.Execute()
}
Loading

0 comments on commit feebb32

Please sign in to comment.