Repository navigation
Add inbound forward-rules commands and threads list --search #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mklocek
wants to merge
1
commit into
main
Choose a base branch
from
add-inbound-forwarding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package forwardrules | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/mailtrap/mailtrap-cli/internal/client" | ||
| "github.com/mailtrap/mailtrap-cli/internal/cmdutil" | ||
| "github.com/mailtrap/mailtrap-cli/internal/output" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const conditionsUsage = "Conditions as a JSON array" | ||
|
|
||
| const destinationsUsage = "Destination email addresses (comma-separated)" | ||
|
|
||
| var ( | ||
| validMatchTypes = []string{"sender", "recipient", "header"} | ||
| validOperators = []string{"equal", "not_equal", "contains", "starts_with", "ends_with", "empty", "not_empty"} | ||
| ) | ||
|
|
||
| type conditionInput struct { | ||
| MatchType string `json:"match_type"` | ||
| Operator string `json:"operator"` | ||
| Value *string `json:"value,omitempty"` | ||
| HeaderKey *string `json:"header_key,omitempty"` | ||
| } | ||
|
|
||
| func parseConditions(raw string) ([]conditionInput, error) { | ||
| if !strings.HasPrefix(strings.TrimSpace(raw), "[") { | ||
| return nil, fmt.Errorf("invalid --conditions: must be a JSON array") | ||
| } | ||
|
|
||
| dec := json.NewDecoder(bytes.NewReader([]byte(raw))) | ||
| dec.DisallowUnknownFields() | ||
|
|
||
| conditions := []conditionInput{} | ||
| if err := dec.Decode(&conditions); err != nil { | ||
| return nil, fmt.Errorf("invalid --conditions JSON: %w", err) | ||
| } | ||
| if _, err := dec.Token(); err != io.EOF { | ||
| return nil, fmt.Errorf("invalid --conditions JSON: unexpected data after the array") | ||
| } | ||
|
|
||
| for i, cond := range conditions { | ||
| if !slices.Contains(validMatchTypes, cond.MatchType) { | ||
| return nil, fmt.Errorf("invalid --conditions: entry %d: match_type must be one of %s", i+1, strings.Join(validMatchTypes, ", ")) | ||
| } | ||
| if !slices.Contains(validOperators, cond.Operator) { | ||
| return nil, fmt.Errorf("invalid --conditions: entry %d: operator must be one of %s", i+1, strings.Join(validOperators, ", ")) | ||
| } | ||
| } | ||
|
|
||
| return conditions, nil | ||
| } | ||
|
|
||
| func buildDestinations(emails []string) []ForwardRuleDestination { | ||
| destinations := make([]ForwardRuleDestination, 0, len(emails)) | ||
| for _, email := range emails { | ||
| destinations = append(destinations, ForwardRuleDestination{Email: email}) | ||
| } | ||
| return destinations | ||
| } | ||
|
|
||
| func NewCmdCreate(f *cmdutil.Factory) *cobra.Command { | ||
| var ( | ||
| inboxID string | ||
| name string | ||
| conditions string | ||
| destinations []string | ||
| ) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create a forward rule", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := cmdutil.RequireFlag("inbox-id", inboxID); err != nil { | ||
| return err | ||
| } | ||
| if err := cmdutil.RequireFlag("name", name); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| body := map[string]interface{}{"name": name} | ||
| if cmd.Flags().Changed("conditions") { | ||
| parsed, err := parseConditions(conditions) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| body["conditions"] = parsed | ||
| } | ||
| if cmd.Flags().Changed("destinations") { | ||
| body["destinations"] = buildDestinations(destinations) | ||
| } | ||
|
|
||
| c, err := f.NewClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| path := fmt.Sprintf("/api/inbound/inboxes/%s/forward_rules", inboxID) | ||
|
|
||
| var resp forwardRuleResponse | ||
| if err := c.Post(context.Background(), client.BaseGeneral, path, body, &resp); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return output.Print(f.IOStreams.Out, cmdutil.GetOutputFormat(), resp.Data, forwardRuleColumns) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&inboxID, "inbox-id", "", "Inbox ID (required)") | ||
| cmd.Flags().StringVar(&name, "name", "", "Rule name (required)") | ||
| cmd.Flags().StringVar(&conditions, "conditions", "", conditionsUsage) | ||
| cmd.Flags().StringSliceVar(&destinations, "destinations", nil, destinationsUsage) | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package forwardrules | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/mailtrap/mailtrap-cli/internal/client" | ||
| "github.com/mailtrap/mailtrap-cli/internal/cmdutil" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewCmdDelete(f *cmdutil.Factory) *cobra.Command { | ||
| var ( | ||
| inboxID string | ||
| ruleID string | ||
| ) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "delete", | ||
| Short: "Delete a forward rule", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := cmdutil.RequireFlag("inbox-id", inboxID); err != nil { | ||
| return err | ||
| } | ||
| if err := cmdutil.RequireFlag("id", ruleID); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| c, err := f.NewClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| path := fmt.Sprintf("/api/inbound/inboxes/%s/forward_rules/%s", inboxID, ruleID) | ||
|
|
||
| if err := c.Delete(context.Background(), client.BaseGeneral, path, nil); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Fprintln(f.IOStreams.Out, "Forward rule deleted successfully.") | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&inboxID, "inbox-id", "", "Inbox ID (required)") | ||
| cmd.Flags().StringVar(&ruleID, "id", "", "Forward rule ID (required)") | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package forwardrules | ||
|
|
||
| import ( | ||
| "github.com/mailtrap/mailtrap-cli/internal/cmdutil" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // NewCmdForwardRules creates the `inbound forward-rules` command group. | ||
| func NewCmdForwardRules(f *cmdutil.Factory) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "forward-rules", | ||
| Short: "Manage inbound forward rules", | ||
| } | ||
|
|
||
| cmd.AddCommand(NewCmdList(f)) | ||
| cmd.AddCommand(NewCmdGet(f)) | ||
| cmd.AddCommand(NewCmdCreate(f)) | ||
| cmd.AddCommand(NewCmdUpdate(f)) | ||
| cmd.AddCommand(NewCmdDelete(f)) | ||
|
|
||
| return cmd | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.