Skip to content

Commit

Permalink
feat: create flag env
Browse files Browse the repository at this point in the history
  • Loading branch information
daltondiaz committed Jul 18, 2023
1 parent 3b8a8a1 commit 92495c6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ Check if the plugin of rabbitmq-manager is available in your RabbitMQ

Rabbit Tools for now only has two functions show List of Queue or Purge Queue

### Flags


#### Env

```
--env=Prefix
```

You pass in flag env the prefix to use more than one configuration. Example:

```
./rabbit-tools list all --env=PROD
```

The configuration look like:

```
PROD_RABBIT_URL=http://your_url/api/queues/your_virtual_host
PROD_RABBIT_USER=your_user
PROD_RABBIT_PASS=your_pass
```


### List

```
Expand Down
5 changes: 4 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (

"github.com/daltondiaz/rabbit-tools/internal"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
Expand All @@ -12,7 +13,8 @@ var listCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
queue := args[0]
result := internal.GetQueues(queue)
env, _ := cmd.Flags().GetString("env")
result := internal.GetQueues(queue, env)
var prettyResult internal.PrettyModelResult
prettyResult.Title = "List of Queues"
prettyResult.Header = table.Row{"Queue", "Messages"}
Expand All @@ -30,4 +32,5 @@ var listCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(listCmd)
listCmd.PersistentFlags().String("env", "", "Prefix of environment in config.env")
}
4 changes: 3 additions & 1 deletion cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ var purgeCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
queue := args[0]
internal.PurgeQueue(queue)
env, _ := cmd.Flags().GetString("env")
internal.PurgeQueue(queue, env)
},
}

func init(){
rootCmd.AddCommand(purgeCmd)
rootCmd.PersistentFlags().String("env","", "Prefix of environment in config.env")
}
4 changes: 2 additions & 2 deletions internal/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type ParamAccess struct {
User string
}

func GetQueues(queue string) map[string]float64 {
func GetQueues(queue string, env string) map[string]float64 {

params := LoadEnvVariables()
params := LoadEnvVariables(env)
client := &http.Client{}
req, err := http.NewRequest("GET", params.Url, nil)
req.SetBasicAuth(params.User, params.Pass)
Expand Down
16 changes: 12 additions & 4 deletions internal/load_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ import (
"github.com/joho/godotenv"
)

func LoadEnvVariables() ParamAccess {
func LoadEnvVariables(env string) ParamAccess {

err := godotenv.Load("./config.env")
if err != nil {
log.Fatal("Error loading .env file")
}
url := "RABBIT_URL"
user := "RABBIT_USER"
pass := "RABBIT_PASS"
if env != "" {
url = env + "_" + url
user = env + "_" + user
pass = env + "_" + pass
}
var params ParamAccess
params.Url = os.Getenv("RABBIT_URL")
params.User = os.Getenv("RABBIT_USER")
params.Pass = os.Getenv("RABBIT_PASS")
params.Url = os.Getenv(url)
params.User = os.Getenv(user)
params.Pass = os.Getenv(pass)
return params
}
6 changes: 3 additions & 3 deletions internal/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

// Do purge of the queues found from queue argument
func PurgeQueue(queue string) {
params := LoadEnvVariables()
func PurgeQueue(queue string, env string) {
params := LoadEnvVariables(env)
client := &http.Client{}
queues := GetQueues(queue)
queues := GetQueues(queue, env)
sum := 0.0
for queueName, value := range queues {
url := (params.Url + "/" + queueName + "/contents")
Expand Down

0 comments on commit 92495c6

Please sign in to comment.