Skip to content

Commit 99677fb

Browse files
committed
resources subcommand for cli
1 parent 0b5779e commit 99677fb

2 files changed

Lines changed: 123 additions & 1 deletion

File tree

cmd/bugout/brood/command.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010

1111
func PopulateBroodCommands(cmd *cobra.Command) {
1212
groupsCmd := CreateGroupsCommand()
13+
resourcesCmd := GenerateResourcesCommand()
1314
pingCmd := CreatePingCommand()
1415
userCmd := CreateUserCommand()
1516
versionCmd := CreateVersionCommand()
1617

17-
cmd.AddCommand(groupsCmd, pingCmd, userCmd, versionCmd)
18+
cmd.AddCommand(groupsCmd, resourcesCmd, pingCmd, userCmd, versionCmd)
1819
}
1920

2021
func CreatePingCommand() *cobra.Command {

cmd/bugout/brood/resources.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package broodcmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/bugout-dev/bugout-go/cmd/bugout/cmdutils"
10+
bugout "github.com/bugout-dev/bugout-go/pkg"
11+
)
12+
13+
func GenerateResourcesCommand() *cobra.Command {
14+
resourcesCmd := &cobra.Command{
15+
Use: "resources",
16+
Short: "Bugout resources operations",
17+
}
18+
19+
resourcesCreateCmd := GenerateResourceCreateCommand()
20+
resourcesDeleteCmd := GenerateResourceDeleteCommand()
21+
resourcesGetCmd := GenerateResourcesGetCommand()
22+
23+
resourcesCmd.AddCommand(resourcesCreateCmd, resourcesDeleteCmd, resourcesGetCmd)
24+
25+
return resourcesCmd
26+
}
27+
28+
func GenerateResourceCreateCommand() *cobra.Command {
29+
var token, applicationId string
30+
resourceCreateCmd := &cobra.Command{
31+
Use: "create [resource]",
32+
Short: "Create new resource",
33+
PreRunE: cmdutils.TokenArgPopulator,
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
if len(args) != 1 {
36+
return fmt.Errorf("One resource argument as json string must be specified")
37+
}
38+
var resourceRaw interface{}
39+
err := json.Unmarshal([]byte(args[0]), &resourceRaw)
40+
if err != nil {
41+
return err
42+
}
43+
44+
client, clientErr := bugout.ClientFromEnv()
45+
if clientErr != nil {
46+
return clientErr
47+
}
48+
49+
resource, err := client.Brood.CreateResource(token, applicationId, resourceRaw)
50+
if err != nil {
51+
return err
52+
}
53+
54+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&resource)
55+
return encodeErr
56+
},
57+
}
58+
59+
resourceCreateCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
60+
resourceCreateCmd.Flags().StringVarP(&applicationId, "application_id", "a", "", "Application ID resource belongs to")
61+
62+
return resourceCreateCmd
63+
}
64+
65+
func GenerateResourceDeleteCommand() *cobra.Command {
66+
var token, resourceId string
67+
resourceDeleteCmd := &cobra.Command{
68+
Use: "delete",
69+
Short: "Delete resource",
70+
PreRunE: cmdutils.TokenArgPopulator,
71+
RunE: func(cmd *cobra.Command, args []string) error {
72+
client, clientErr := bugout.ClientFromEnv()
73+
if clientErr != nil {
74+
return clientErr
75+
}
76+
77+
resource, err := client.Brood.DeleteResource(token, resourceId)
78+
if err != nil {
79+
return nil
80+
}
81+
82+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&resource)
83+
return encodeErr
84+
},
85+
}
86+
87+
resourceDeleteCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
88+
resourceDeleteCmd.Flags().StringVarP(&resourceId, "resource_id", "r", "", "Resource ID")
89+
90+
return resourceDeleteCmd
91+
}
92+
93+
func GenerateResourcesGetCommand() *cobra.Command {
94+
var token, applicationId string
95+
var queryParams map[string]string
96+
resourcesGetCmd := &cobra.Command{
97+
Use: "get",
98+
Short: "Get resources of application",
99+
PreRunE: cmdutils.TokenArgPopulator,
100+
RunE: func(cmd *cobra.Command, args []string) error {
101+
client, clientErr := bugout.ClientFromEnv()
102+
if clientErr != nil {
103+
return clientErr
104+
}
105+
106+
resources, err := client.Brood.GetResources(token, applicationId, queryParams)
107+
if err != nil {
108+
return nil
109+
}
110+
111+
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(&resources)
112+
return encodeErr
113+
},
114+
}
115+
116+
resourcesGetCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request")
117+
resourcesGetCmd.Flags().StringVarP(&applicationId, "application_id", "a", "", "Application ID resource belongs to")
118+
resourcesGetCmd.Flags().StringToStringVarP(&queryParams, "params", "p", nil, "Optional query parameters to filter resources")
119+
120+
return resourcesGetCmd
121+
}

0 commit comments

Comments
 (0)