|
| 1 | +package broodcmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + "github.com/bugout-dev/bugout-go/cmd/bugout/cmdutils" |
| 9 | + bugout "github.com/bugout-dev/bugout-go/pkg" |
| 10 | +) |
| 11 | + |
| 12 | +func CreateApplicationsCommand() *cobra.Command { |
| 13 | + applicationsCmd := &cobra.Command{ |
| 14 | + Use: "applications", |
| 15 | + Short: "Bugout application operations", |
| 16 | + } |
| 17 | + |
| 18 | + applicationsCreateCmd := CreateApplicationsCreateCommand() |
| 19 | + applicationsGetCmd := CreateApplicationsGetCommand() |
| 20 | + applicationsListCmd := CreateApplicationsListCommand() |
| 21 | + applicationsDeleteCmd := CreateApplicationsDeleteCommand() |
| 22 | + |
| 23 | + applicationsCmd.AddCommand(applicationsCreateCmd, applicationsGetCmd, applicationsListCmd, applicationsDeleteCmd) |
| 24 | + |
| 25 | + return applicationsCmd |
| 26 | +} |
| 27 | + |
| 28 | +func CreateApplicationsCreateCommand() *cobra.Command { |
| 29 | + var token, groupId, name, description string |
| 30 | + applicationsCreateCmd := &cobra.Command{ |
| 31 | + Use: "create", |
| 32 | + Short: "Create a new bugout application", |
| 33 | + PreRunE: cmdutils.TokenArgPopulator, |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + client, err := bugout.ClientFromEnv() |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + applications, applicationsErr := client.Brood.CreateApplication(token, groupId, name, description) |
| 41 | + if applicationsErr != nil { |
| 42 | + return applicationsErr |
| 43 | + } |
| 44 | + |
| 45 | + encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(applications) |
| 46 | + return encodeErr |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + applicationsCreateCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request") |
| 51 | + applicationsCreateCmd.Flags().StringVarP(&groupId, "group", "g", "", "ID of Brood group to create the application under") |
| 52 | + applicationsCreateCmd.Flags().StringVarP(&name, "name", "n", "", "Name of application to create") |
| 53 | + applicationsCreateCmd.Flags().StringVarP(&description, "description", "d", "", "Description of application to create") |
| 54 | + applicationsCreateCmd.MarkFlagRequired("group") |
| 55 | + applicationsCreateCmd.MarkFlagRequired("name") |
| 56 | + |
| 57 | + return applicationsCreateCmd |
| 58 | +} |
| 59 | + |
| 60 | +func CreateApplicationsGetCommand() *cobra.Command { |
| 61 | + var token, applicationId string |
| 62 | + applicationsGetCmd := &cobra.Command{ |
| 63 | + Use: "get", |
| 64 | + Short: "Get an application by ID", |
| 65 | + PreRunE: cmdutils.TokenArgPopulator, |
| 66 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 67 | + client, err := bugout.ClientFromEnv() |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + applications, applicationsErr := client.Brood.GetApplication(token, applicationId) |
| 73 | + if applicationsErr != nil { |
| 74 | + return applicationsErr |
| 75 | + } |
| 76 | + |
| 77 | + encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(applications) |
| 78 | + return encodeErr |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + applicationsGetCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request") |
| 83 | + applicationsGetCmd.Flags().StringVarP(&applicationId, "application", "a", "", "ID of application to get") |
| 84 | + applicationsGetCmd.MarkFlagRequired("application") |
| 85 | + |
| 86 | + return applicationsGetCmd |
| 87 | +} |
| 88 | + |
| 89 | +func CreateApplicationsListCommand() *cobra.Command { |
| 90 | + var token, groupId string |
| 91 | + applicationsListCmd := &cobra.Command{ |
| 92 | + Use: "list", |
| 93 | + Short: "List applications for a given user", |
| 94 | + PreRunE: cmdutils.TokenArgPopulator, |
| 95 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 96 | + client, err := bugout.ClientFromEnv() |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + applications, applicationsErr := client.Brood.ListApplications(token, groupId) |
| 102 | + if applicationsErr != nil { |
| 103 | + return applicationsErr |
| 104 | + } |
| 105 | + |
| 106 | + encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(applications) |
| 107 | + return encodeErr |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + applicationsListCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request") |
| 112 | + applicationsListCmd.Flags().StringVarP(&groupId, "group", "g", "", "Only return applications owned by this group (optional)") |
| 113 | + |
| 114 | + return applicationsListCmd |
| 115 | +} |
| 116 | + |
| 117 | +func CreateApplicationsDeleteCommand() *cobra.Command { |
| 118 | + var token, applicationID string |
| 119 | + applicationsDeleteCmd := &cobra.Command{ |
| 120 | + Use: "delete", |
| 121 | + Short: "Delete a bugout application (by ID)", |
| 122 | + PreRunE: cmdutils.TokenArgPopulator, |
| 123 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 124 | + client, err := bugout.ClientFromEnv() |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + application, applicationErr := client.Brood.DeleteApplication(token, applicationID) |
| 130 | + if applicationErr != nil { |
| 131 | + return applicationErr |
| 132 | + } |
| 133 | + |
| 134 | + encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(application) |
| 135 | + return encodeErr |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + applicationsDeleteCmd.Flags().StringVarP(&token, "token", "t", "", "Bugout access token to use for the request") |
| 140 | + applicationsDeleteCmd.Flags().StringVarP(&applicationID, "id", "i", "", "ID of application to delete") |
| 141 | + applicationsDeleteCmd.MarkFlagRequired("id") |
| 142 | + |
| 143 | + return applicationsDeleteCmd |
| 144 | +} |
0 commit comments