Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cmd/dedicated_sending_ips.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"fmt"

"github.com/loops-so/cli/internal/config"
"github.com/spf13/cobra"
)

func runDedicatedSendingIPs(cfg *config.Config) ([]string, error) {
return newAPIClient(cfg).GetDedicatedSendingIPs()
}

var dedicatedSendingIPsCmd = &cobra.Command{
Use: "dedicated-sending-ips",
Short: "List dedicated sending IP addresses assigned to your team",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadConfig()
if err != nil {
return err
}

ips, err := runDedicatedSendingIPs(cfg)
if err != nil {
return err
}

if isJSONOutput() {
if ips == nil {
ips = []string{}
}
return printJSON(cmd.OutOrStdout(), ips)
}

if len(ips) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No dedicated sending IPs assigned.")
return nil
}

for _, ip := range ips {
fmt.Fprintln(cmd.OutOrStdout(), ip)
}
return nil
},
}

func init() {
rootCmd.AddCommand(dedicatedSendingIPsCmd)
}
40 changes: 40 additions & 0 deletions cmd/dedicated_sending_ips_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"net/http"
"reflect"
"testing"
)

func TestRunDedicatedSendingIPs(t *testing.T) {
t.Run("returns assigned ips", func(t *testing.T) {
serveJSON(t, http.StatusOK, `["192.0.2.1","192.0.2.2"]`)
ips, err := runDedicatedSendingIPs(cfg(t))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := []string{"192.0.2.1", "192.0.2.2"}
if !reflect.DeepEqual(ips, want) {
t.Errorf("got %+v, want %+v", ips, want)
}
})

t.Run("returns empty slice when none assigned", func(t *testing.T) {
serveJSON(t, http.StatusOK, `[]`)
ips, err := runDedicatedSendingIPs(cfg(t))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(ips) != 0 {
t.Errorf("expected empty slice, got %+v", ips)
}
})

t.Run("returns error on api failure", func(t *testing.T) {
serveJSON(t, http.StatusUnauthorized, `{"error":"Invalid API key"}`)
_, err := runDedicatedSendingIPs(cfg(t))
if err == nil {
t.Fatal("expected error, got nil")
}
})
}