1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Manuel Imperiale 09cbc3f14b
MF-1551 - Fix Cobra usage commands and clean unnecessary struct types (#1558)
* MF-1551 - Fix Cobra usage commands and clean unnecessary struct types

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use linux syntax for cmd usage description

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix cmd.Use

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2022-02-11 13:22:57 +01:00

91 lines
1.6 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package cli
import (
"time"
"github.com/spf13/cobra"
)
var cmdAPIKeys = []cobra.Command{
{
Use: "issue <duration> <user_auth_token>",
Short: "Issue key",
Long: `Issues a new Key`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Use)
return
}
d, err := time.ParseDuration(args[0])
if err != nil {
logError(err)
return
}
resp, err := sdk.Issue(args[1], d)
if err != nil {
logError(err)
return
}
logJSON(resp)
},
},
{
Use: "revoke <key_id> <user_auth_token>",
Short: "Revoke key",
Long: `Removes API key from database`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Use)
return
}
if err := sdk.Revoke(args[0], args[1]); err != nil {
logError(err)
return
}
logOK()
},
},
{
Use: "retrieve <key_id> <user_auth_token>",
Short: "Retrieve key",
Long: `Retrieves API key with given id`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Use)
return
}
rk, err := sdk.RetrieveKey(args[0], args[1])
if err != nil {
logError(err)
return
}
logJSON(rk)
},
},
}
// NewKeysCmd returns keys command.
func NewKeysCmd() *cobra.Command {
cmd := cobra.Command{
Use: "keys [issue | revoke | retrieve]",
Short: "Keys management",
Long: `Keys management: issue, revoke, or retrieve API key.`,
}
for i := range cmdAPIKeys {
cmd.AddCommand(&cmdAPIKeys[i])
}
return &cmd
}