1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
João Matos 3273c30d8b
MF-1268 - CLI improvements (#1274)
* Prefix error messages in CLI with a bold "error: ".

Signed-off-by: Joao Matos <joao@tritao.eu>

* Remove duplicated "Usage: " from groups command help.

Signed-off-by: Joao Matos <joao@tritao.eu>

* Add a raw output mode for CLI and use it on logCreated.

Signed-off-by: Joao Matos <joao@tritao.eu>

* Add CLI global flag for user auth token.

Signed-off-by: Joao Matos <joao@tritao.eu>

* Add CLI config flag and parsing logic.

Signed-off-by: Joao Matos <joao@tritao.eu>

* Refactor CLI users commands outside array structure.

Signed-off-by: Joao Matos <joao@tritao.eu>

* Refactor CLI certificates commands using flags.

Signed-off-by: Joao Matos <joao@tritao.eu>

* Refactor CLI things create command using flags.

Signed-off-by: Joao Matos <joao@tritao.eu>

Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
2020-11-01 00:29:06 +01:00

61 lines
1.4 KiB
Go

package cli
import (
"strconv"
"github.com/spf13/cobra"
)
// NewCertsCmd returns certificate command.
func NewCertsCmd() *cobra.Command {
var keySize uint16
var keyType string
var ttl uint32
issueCmd := cobra.Command{
Use: "issue",
Short: "issue <thing_id> [--keysize=2048] [--keytype=rsa] [--ttl=8760]",
Long: `Issues new certificate for a thing`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logUsage(cmd.Short)
return
}
thingID := args[0]
valid := strconv.FormatUint(uint64(ttl), 10)
token := getUserAuthToken()
c, err := sdk.IssueCert(thingID, int(keySize), keyType, valid, token)
if err != nil {
logError(err)
return
}
logJSON(c)
},
}
issueCmd.Flags().Uint16Var(&keySize, "keysize", 2048, "certificate key strength in bits: 2048, 4096 (RSA) or 224, 256, 384, 512 (EC)")
issueCmd.Flags().StringVar(&keyType, "keytype", "rsa", "certificate key type: RSA or EC")
issueCmd.Flags().Uint32Var(&ttl, "ttl", 8760, "certificate time to live in hours")
cmd := cobra.Command{
Use: "certs",
Short: "Certificates management",
Long: `Certificates management: create certificates for things"`,
Run: func(cmd *cobra.Command, args []string) {
logUsage("certs [issue]")
},
}
cmdCerts := []cobra.Command{
issueCmd,
}
for i := range cmdCerts {
cmd.AddCommand(&cmdCerts[i])
}
return &cmd
}