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
14 changes: 13 additions & 1 deletion cmd/alias/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Optionally attach a note, display name, or assign to specific mailboxes.`,
Example: ` # Create a random alias
sl alias create --random

# Create a word-based random alias
sl alias create --random --mode word

# Create a UUID-based random alias
sl alias create --random --mode uuid

# Create a random alias with a note
sl alias create --random --note "Used for newsletters"

Expand Down Expand Up @@ -56,6 +62,7 @@ var (
createNote string
createName string
createHostname string
createMode string
createJSON bool
createJQ string
)
Expand All @@ -68,6 +75,7 @@ func init() {
createCmd.Flags().StringVar(&createNote, "note", "", "Note for the alias")
createCmd.Flags().StringVar(&createName, "name", "", "Display name for the alias")
createCmd.Flags().StringVar(&createHostname, "hostname", "", "Associate alias with a hostname/website")
createCmd.Flags().StringVar(&createMode, "mode", "", "Random alias generation mode: uuid or word")
createCmd.Flags().BoolVar(&createJSON, "json", false, "Output as JSON")
createCmd.Flags().StringVar(&createJQ, "jq", "", "Apply jq expression to JSON output")
}
Expand All @@ -80,8 +88,12 @@ func runCreate(cmd *cobra.Command, args []string) error {

client := api.NewClient(key, auth.GetAPIBase())

if createMode != "" && createMode != "uuid" && createMode != "word" {
return fmt.Errorf("--mode must be 'uuid' or 'word'")
}

if createRandom {
alias, rawJSON, err := client.CreateRandomAlias(createNote, createHostname)
alias, rawJSON, err := client.CreateRandomAlias(createNote, createHostname, createMode)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/alias/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func runOptions(cmd *cobra.Command, args []string) error {
return err
}

client := api.NewClient(key)
client := api.NewClient(key, auth.GetAPIBase())

opts, rawJSON, err := client.GetAliasOptions(optionsHostname)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/domain/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func runView(cmd *cobra.Command, args []string) error {
return err
}

client := api.NewClient(key)
client := api.NewClient(key, auth.GetAPIBase())
domain, rawJSON, err := client.GetCustomDomain(id)
if err != nil {
return err
Expand Down
13 changes: 11 additions & 2 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,23 @@ type CreateRandomAliasRequest struct {
}

// CreateRandomAlias creates a random alias.
func (c *Client) CreateRandomAlias(note, hostname string) (*Alias, []byte, error) {
// hostname associates the alias with a specific website.
// mode can be "uuid" or "word" (empty string uses server default).
func (c *Client) CreateRandomAlias(note, hostname, mode string) (*Alias, []byte, error) {
var reqBody interface{}
if note != "" {
reqBody = &CreateRandomAliasRequest{Note: note}
}
path := "/api/alias/random/new"
params := url.Values{}
if hostname != "" {
path += "?hostname=" + url.QueryEscape(hostname)
params.Set("hostname", hostname)
}
if mode != "" {
params.Set("mode", mode)
}
if len(params) > 0 {
path += "?" + params.Encode()
}
body, status, err := c.do("POST", path, reqBody)
if err != nil {
Expand Down
33 changes: 32 additions & 1 deletion internal/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestClient_CreateRandomAlias(t *testing.T) {
defer srv.Close()

c := newTestClient(t, srv)
alias, _, err := c.CreateRandomAlias("test note", "")
alias, _, err := c.CreateRandomAlias("test note", "", "")
if err != nil {
t.Fatalf("CreateRandomAlias: %v", err)
}
Expand All @@ -351,6 +351,37 @@ func TestClient_CreateRandomAlias(t *testing.T) {
}
}

func TestClient_CreateRandomAlias_WithMode(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/alias/random/new" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
mode := r.URL.Query().Get("mode")
if mode != "word" {
t.Errorf("expected mode=word, got %q", mode)
}
w.WriteHeader(201)
alias := Alias{ID: 101, Email: "word-random@sl.local"}
_ = json.NewEncoder(w).Encode(alias)
}))
defer srv.Close()

c := newTestClient(t, srv)
alias, _, err := c.CreateRandomAlias("", "", "word")
if err != nil {
t.Fatalf("CreateRandomAlias with mode: %v", err)
}
if alias.ID != 101 {
t.Errorf("expected ID 101, got %d", alias.ID)
}
if alias.Email != "word-random@sl.local" {
t.Errorf("expected email word-random@sl.local, got %q", alias.Email)
}
}

func TestClient_GetStats(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/stats" {
Expand Down
Loading