mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* MF-354 - Add Go SDK This PR adds Go SDK. It also refactors `cli` to use new SDK. Signed-off-by: drasko <drasko.draskovic@gmail.com> * Use http consts. Add doc. Signed-off-by: drasko <drasko.draskovic@gmail.com> * Insline const Signed-off-by: drasko <drasko.draskovic@gmail.com> * Add initial SDK tests Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix SDK to return values (not HTTP rsp) Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix CLI and test Signed-off-by: drasko <drasko.draskovic@gmail.com> * fix typos, add header Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix doc Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix doc, add comment Signed-off-by: drasko <drasko.draskovic@gmail.com> * Inline error checks Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix typos Signed-off-by: drasko <drasko.draskovic@gmail.com> * Inline errs Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix typo Signed-off-by: drasko <drasko.draskovic@gmail.com> * Change fnc parameter name Signed-off-by: drasko <drasko.draskovic@gmail.com> * Rename getters to Go standard Signed-off-by: drasko <drasko.draskovic@gmail.com> * Use struct and interface Signed-off-by: drasko <drasko.draskovic@gmail.com> * Simplify sdk struct Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix README Signed-off-by: drasko <drasko.draskovic@gmail.com>
142 lines
2.8 KiB
Go
142 lines
2.8 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package cli
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const thingsEP = "things"
|
|
|
|
var cmdThings = []cobra.Command{
|
|
cobra.Command{
|
|
Use: "create",
|
|
Short: "create <JSON_thing> <user_auth_token>",
|
|
Long: `Create new thing, generate his UUID and store it`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Short)
|
|
return
|
|
}
|
|
id, err := sdk.CreateThing(args[0], args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
dump(id)
|
|
},
|
|
},
|
|
cobra.Command{
|
|
Use: "get",
|
|
Short: "get all/<thing_id> <user_auth_token>",
|
|
Long: `Get all things or thing by id`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Short)
|
|
return
|
|
}
|
|
if args[0] == "all" {
|
|
l, err := sdk.Things(args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
dump(l)
|
|
return
|
|
}
|
|
t, err := sdk.Thing(args[0], args[1])
|
|
if err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
dump(t)
|
|
},
|
|
},
|
|
cobra.Command{
|
|
Use: "delete",
|
|
Short: "delete <thing_id> <user_auth_token>",
|
|
Long: `Removes thing from database`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 2 {
|
|
logUsage(cmd.Short)
|
|
return
|
|
}
|
|
if err := sdk.DeleteThing(args[0], args[1]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
logOK()
|
|
},
|
|
},
|
|
cobra.Command{
|
|
Use: "update",
|
|
Short: "update <thing_id> <JSON_string> <user_auth_token>",
|
|
Long: `Update thing record`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 3 {
|
|
logUsage(cmd.Short)
|
|
return
|
|
}
|
|
if err := sdk.UpdateThing(args[0], args[1], args[2]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
logOK()
|
|
},
|
|
},
|
|
cobra.Command{
|
|
Use: "connect",
|
|
Short: "connect <thing_id> <channel_id> <user_auth_token>",
|
|
Long: `Connect thing to the channel`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 3 {
|
|
logUsage(cmd.Short)
|
|
return
|
|
}
|
|
if err := sdk.ConnectThing(args[0], args[1], args[2]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
logOK()
|
|
},
|
|
},
|
|
cobra.Command{
|
|
Use: "disconnect",
|
|
Short: "disconnect <thing_id> <channel_id> <user_auth_token>",
|
|
Long: `Disconnect thing to the channel`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 3 {
|
|
logUsage(cmd.Short)
|
|
return
|
|
}
|
|
if err := sdk.DisconnectThing(args[0], args[1], args[2]); err != nil {
|
|
logError(err)
|
|
return
|
|
}
|
|
logOK()
|
|
},
|
|
},
|
|
}
|
|
|
|
func NewThingsCmd() *cobra.Command {
|
|
cmd := cobra.Command{
|
|
Use: "things",
|
|
Short: "things <options>",
|
|
Long: `Things handling: create, delete or update things`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
logUsage(cmd.Short)
|
|
},
|
|
}
|
|
|
|
for i := range cmdThings {
|
|
cmd.AddCommand(&cmdThings[i])
|
|
}
|
|
|
|
return &cmd
|
|
}
|