Skip to content

Commit

Permalink
NOISSUE - Add Magistrala CLI (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Gateru <[email protected]>
  • Loading branch information
felixgateru authored Jan 14, 2025
1 parent 96158cd commit 0bcb846
Show file tree
Hide file tree
Showing 17 changed files with 2,484 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ jobs:
- "pkg/sdk/**"
- "pkg/events/**"
cli:
- "cli/**"
- "cmd/cli/**"
- "pkg/sdk/**"
consumers:
- "consumers/**"
- "cmd/postgres-writer/**"
Expand Down Expand Up @@ -109,6 +114,11 @@ jobs:
run: |
go test --race -v -count=1 -coverprofile=coverage/bootstrap.out ./bootstrap/...
- name: Run cli tests
if: steps.changes.outputs.cli == 'true' || steps.changes.outputs.workflow == 'true'
run: |
go test --race -v -count=1 -coverprofile=coverage/cli.out ./cli/...
- name: Run consumers tests
if: steps.changes.outputs.consumers == 'true' || steps.changes.outputs.workflow == 'true'
run: |
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

MG_DOCKER_IMAGE_NAME_PREFIX ?= ghcr.io/absmach/magistrala
BUILD_DIR = build
SERVICES = bootstrap provision re postgres-writer postgres-reader timescale-writer timescale-reader
SERVICES = bootstrap provision re postgres-writer postgres-reader timescale-writer timescale-reader cli
DOCKERS = $(addprefix docker_,$(SERVICES))
DOCKERS_DEV = $(addprefix docker_dev_,$(SERVICES))
CGO_ENABLED ?= 0
Expand Down
216 changes: 216 additions & 0 deletions cli/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package cli

import (
"encoding/json"

mgsdk "github.com/absmach/magistrala/pkg/sdk"
"github.com/spf13/cobra"
)

var cmdBootstrap = []cobra.Command{
{
Use: "create <JSON_config> <domain_id> <user_auth_token>",
Short: "Create config",
Long: `Create new Client Bootstrap Config to the user identified by the provided key`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
logUsageCmd(*cmd, cmd.Use)
return
}

var cfg mgsdk.BootstrapConfig
if err := json.Unmarshal([]byte(args[0]), &cfg); err != nil {
logErrorCmd(*cmd, err)
return
}

id, err := sdk.AddBootstrap(cfg, args[1], args[2])
if err != nil {
logErrorCmd(*cmd, err)
return
}

logCreatedCmd(*cmd, id)
},
},
{
Use: "get [all | <client_id>] <domain_id> <user_auth_token>",
Short: "Get config",
Long: `Get Client Config with given ID belonging to the user identified by the given key.
all - lists all config
<client_id> - view config of <client_id>`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
logUsageCmd(*cmd, cmd.Use)
return
}
pageMetadata := mgsdk.PageMetadata{
Offset: Offset,
Limit: Limit,
State: State,
Name: Name,
}
if args[0] == "all" {
l, err := sdk.Bootstraps(pageMetadata, args[1], args[2])
if err != nil {
logErrorCmd(*cmd, err)
return
}
logJSONCmd(*cmd, l)
return
}

c, err := sdk.ViewBootstrap(args[0], args[1], args[2])
if err != nil {
logErrorCmd(*cmd, err)
return
}

logJSONCmd(*cmd, c)
},
},
{
Use: "update [config <JSON_config> | connection <id> <channel_ids> | certs <id> <client_cert> <client_key> <ca> ] <domain_id> <user_auth_token>",
Short: "Update config",
Long: `Updates editable fields of the provided Config.
config <JSON_config> - Updates editable fields of the provided Config.
connection <id> <channel_ids> - Updates connections performs update of the channel list corresponding Client is connected to.
channel_ids - '["channel_id1", ...]'
certs <id> <client_cert> <client_key> <ca> - Update bootstrap config certificates.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 4 {
logUsageCmd(*cmd, cmd.Use)
return
}
if args[0] == "config" {
var cfg mgsdk.BootstrapConfig
if err := json.Unmarshal([]byte(args[1]), &cfg); err != nil {
logErrorCmd(*cmd, err)
return
}

if err := sdk.UpdateBootstrap(cfg, args[1], args[2]); err != nil {
logErrorCmd(*cmd, err)
return
}

logOKCmd(*cmd)
return
}
if args[0] == "connection" {
var ids []string
if err := json.Unmarshal([]byte(args[2]), &ids); err != nil {
logErrorCmd(*cmd, err)
return
}
if err := sdk.UpdateBootstrapConnection(args[1], ids, args[3], args[4]); err != nil {
logErrorCmd(*cmd, err)
return
}

logOKCmd(*cmd)
return
}
if args[0] == "certs" {
cfg, err := sdk.UpdateBootstrapCerts(args[0], args[1], args[2], args[3], args[4], args[5])
if err != nil {
logErrorCmd(*cmd, err)
return
}

logJSONCmd(*cmd, cfg)
return
}
logUsageCmd(*cmd, cmd.Use)
},
},
{
Use: "remove <client_id> <domain_id> <user_auth_token>",
Short: "Remove config",
Long: `Removes Config with specified key that belongs to the user identified by the given key`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
logUsageCmd(*cmd, cmd.Use)
return
}

if err := sdk.RemoveBootstrap(args[0], args[1], args[2]); err != nil {
logErrorCmd(*cmd, err)
return
}

logOKCmd(*cmd)
},
},
{
Use: "bootstrap [<external_id> <external_key> | secure <external_id> <external_key> <crypto_key> ]",
Short: "Bootstrap config",
Long: `Returns Config to the Client with provided external ID using external key.
secure - Retrieves a configuration with given external ID and encrypted external key.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
logUsageCmd(*cmd, cmd.Use)
return
}
if args[0] == "secure" {
c, err := sdk.BootstrapSecure(args[1], args[2], args[3])
if err != nil {
logErrorCmd(*cmd, err)
return
}

logJSONCmd(*cmd, c)
return
}
c, err := sdk.Bootstrap(args[0], args[1])
if err != nil {
logErrorCmd(*cmd, err)
return
}

logJSONCmd(*cmd, c)
},
},
{
Use: "whitelist <JSON_config> <domain_id> <user_auth_token>",
Short: "Whitelist config",
Long: `Whitelist updates client state config with given id from the authenticated user`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
logUsageCmd(*cmd, cmd.Use)
return
}

var cfg mgsdk.BootstrapConfig
if err := json.Unmarshal([]byte(args[0]), &cfg); err != nil {
logErrorCmd(*cmd, err)
return
}

if err := sdk.Whitelist(cfg.ClientID, cfg.State, args[1], args[2]); err != nil {
logErrorCmd(*cmd, err)
return
}

logOKCmd(*cmd)
},
},
}

// NewBootstrapCmd returns bootstrap command.
func NewBootstrapCmd() *cobra.Command {
cmd := cobra.Command{
Use: "bootstrap [create | get | update | remove | bootstrap | whitelist]",
Short: "Bootstrap management",
Long: `Bootstrap management: create, get, update, delete or whitelist Bootstrap config`,
}

for i := range cmdBootstrap {
cmd.AddCommand(&cmdBootstrap[i])
}

return &cmd
}
Loading

0 comments on commit 0bcb846

Please sign in to comment.