mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* things, twins, and logger lint fixed Signed-off-by: aryan <aryangodara03@gmail.com> * all services updated, auth jwt not working, ineffectual assignment issue Signed-off-by: aryan <aryangodara03@gmail.com> * handle error from grpc server in endpointtest Signed-off-by: aryan <aryangodara03@gmail.com> * temp commit, auth/jwt needs to be resolved Signed-off-by: aryan <aryangodara03@gmail.com> * revert back to jwt v4 temporarily Signed-off-by: aryan <aryangodara03@gmail.com> * updated jwt tokenizer Signed-off-by: aryan <aryangodara03@gmail.com> * resolve EOF error for httptest requests Signed-off-by: aryan <aryangodara03@gmail.com> * fix auth jwt, update to registeredclaims Signed-off-by: aryan <aryangodara03@gmail.com> * fix ineffective assignment, auth/api/grpc endpoint failing Signed-off-by: aryan <aryangodara03@gmail.com> * temp commit, remove later Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc server setup Signed-off-by: aryan <aryangodara03@gmail.com> * resolve golangci tests, remove debug statements Signed-off-by: aryan <aryangodara03@gmail.com> * update golangci version and modify linters used Signed-off-by: aryan <aryangodara03@gmail.com> * fix failing tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc server for setup tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix logging and errors inlined Signed-off-by: aryan <aryangodara03@gmail.com> * fix remarks, update grpc setup_test Signed-off-by: aryan <aryangodara03@gmail.com> * fix setup_test Signed-off-by: aryan <aryangodara03@gmail.com> * update setup_test grpc Signed-off-by: aryan <aryangodara03@gmail.com> * fix data race Signed-off-by: aryan <aryangodara03@gmail.com> * update setup_test grpc Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc setup down to single simple function Signed-off-by: aryan <aryangodara03@gmail.com> * fix linting issues Signed-off-by: aryan <aryangodara03@gmail.com> * resolve pr comments Signed-off-by: aryan <aryangodara03@gmail.com> * fix tests, handle returned errors, go mod tidy vendor Signed-off-by: aryan <aryangodara03@gmail.com> * fix errors from new linters Signed-off-by: aryan <aryangodara03@gmail.com> --------- Signed-off-by: aryan <aryangodara03@gmail.com>
242 lines
5.0 KiB
Go
242 lines
5.0 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cli
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
mfxsdk "github.com/mainflux/mainflux/pkg/sdk/go"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var cmdThings = []cobra.Command{
|
|
{
|
|
Use: "create <JSON_thing> <user_auth_token>",
|
|
Short: "Create thing",
|
|
Long: `Create new thing, generate his UUID and store it`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
|
|
var thing mfxsdk.Thing
|
|
if err := json.Unmarshal([]byte(args[0]), &thing); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
id, err := sdk.CreateThing(thing, args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logCreated(id)
|
|
},
|
|
},
|
|
{
|
|
Use: "get [all | <thing_id>] <user_auth_token>",
|
|
Short: "Get things",
|
|
Long: `Get all things or get thing by id. Things can be filtered by name or metadata
|
|
all - lists all things
|
|
<thing_id> - shows thing with provided <thing_id>`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
metadata, err := convertMetadata(Metadata)
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
pageMetadata := mfxsdk.PageMetadata{
|
|
Name: "",
|
|
Offset: uint64(Offset),
|
|
Limit: uint64(Limit),
|
|
Metadata: metadata,
|
|
}
|
|
if args[0] == all {
|
|
l, err := sdk.Things(pageMetadata, args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
logJSON(l)
|
|
return
|
|
}
|
|
t, err := sdk.Thing(args[0], args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logJSON(t)
|
|
},
|
|
},
|
|
{
|
|
Use: "delete <thing_id> <user_auth_token>",
|
|
Short: "Delete thing",
|
|
Long: `Removes thing from database`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
|
|
if err := sdk.DeleteThing(args[0], args[1]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logOK()
|
|
},
|
|
},
|
|
{
|
|
Use: "identify <thing_key>",
|
|
Short: "Identify thing",
|
|
Long: "Validates thing's key and returns its ID",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 1 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
|
|
i, err := sdk.IdentifyThing(args[0])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logJSON(i)
|
|
},
|
|
},
|
|
{
|
|
Use: "update <JSON_string> <user_auth_token>",
|
|
Short: "Update thing",
|
|
Long: `Update thing record`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
|
|
var thing mfxsdk.Thing
|
|
if err := json.Unmarshal([]byte(args[0]), &thing); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
if err := sdk.UpdateThing(thing, args[1]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logOK()
|
|
},
|
|
},
|
|
{
|
|
Use: "connect <thing_id> <channel_id> <user_auth_token>",
|
|
Short: "Connect thing",
|
|
Long: `Connect thing to the channel`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 3 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
|
|
connIDs := mfxsdk.ConnectionIDs{
|
|
ChannelIDs: []string{args[1]},
|
|
ThingIDs: []string{args[0]},
|
|
}
|
|
if err := sdk.Connect(connIDs, args[2]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logOK()
|
|
},
|
|
},
|
|
{
|
|
Use: "disconnect <thing_id> <channel_id> <user_auth_token>",
|
|
Short: "Disconnect thing",
|
|
Long: `Disconnect thing to the channel`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 3 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
|
|
if err := sdk.DisconnectThing(args[0], args[1], args[2]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logOK()
|
|
},
|
|
},
|
|
{
|
|
Use: "connections <thing_id> <user_auth_token>",
|
|
Short: "Connected list",
|
|
Long: `List of Channels connected to Thing`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
pm := mfxsdk.PageMetadata{
|
|
Offset: uint64(Offset),
|
|
Limit: uint64(Limit),
|
|
Disconnected: false,
|
|
}
|
|
cl, err := sdk.ChannelsByThing(args[0], pm, args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logJSON(cl)
|
|
},
|
|
},
|
|
{
|
|
Use: "not-connected <thing_id> <user_auth_token>",
|
|
Short: "Not-connected list",
|
|
Long: `List of Channels not connected to a Thing`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Use)
|
|
return
|
|
}
|
|
pm := mfxsdk.PageMetadata{
|
|
Offset: uint64(Offset),
|
|
Limit: uint64(Limit),
|
|
Disconnected: true,
|
|
}
|
|
cl, err := sdk.ChannelsByThing(args[0], pm, args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
|
|
logJSON(cl)
|
|
},
|
|
},
|
|
}
|
|
|
|
// NewThingsCmd returns things command.
|
|
func NewThingsCmd() *cobra.Command {
|
|
cmd := cobra.Command{
|
|
Use: "things [create | get | update | delete | connect | disconnect | connections | not-connected]",
|
|
Short: "Things management",
|
|
Long: `Things management: create, get, update or delete Thing, connect or disconnect Thing from Channel and get the list of Channels connected or disconnected from a Thing`,
|
|
}
|
|
|
|
for i := range cmdThings {
|
|
cmd.AddCommand(&cmdThings[i])
|
|
}
|
|
|
|
return &cmd
|
|
}
|