printer

package
v0.1.0-beta.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package printer provides a type-safe output formatting system for the AhaSend CLI.

This package implements a ResponseHandler interface that provides complete type safety by using concrete SDK response types instead of interface{} for all operations. The handler is instantiated based on the --output flag and passed to commands.

Supported formats:

  • JSON: Machine-readable format for automation and APIs
  • Table: Human-readable tabular format with proper alignment
  • Plain: Simple key-value format for scripts and debugging
  • CSV: Comma-separated values for data processing

Usage:

handler := printer.GetResponseHandler(cmd)
response, err := client.ListDomains(...)
if err != nil {
	return handler.HandleError(err)
}
return handler.HandleDomainList(response, printer.ListConfig{
	SuccessMessage: "Successfully retrieved domains",
	EmptyMessage:   "No domains found",
	ShowPagination: true,
})

Index

Constants

View Source
const ResponseHandlerKey = responseHandlerKeyType("responseHandler")

Context keys for printer instances

Variables

This section is empty.

Functions

func GetSupportedFormats

func GetSupportedFormats() []string

GetSupportedFormats returns the list of supported output formats

func ValidateFormat

func ValidateFormat(format string) error

ValidateFormat validates that a format is supported

func ValidateOutputFormat

func ValidateOutputFormat(cmd *cobra.Command) error

ValidateOutputFormat validates the --output flag value Commands can use this for early validation if needed

Types

type AuthConfig

type AuthConfig struct {
	SuccessMessage string // Message to show on successful auth operation
}

AuthConfig configures how authentication responses are displayed

type AuthStatus

type AuthStatus struct {
	Profile string             // Currently active profile name
	APIKey  string             // Masked API key (showing only account ID)
	Account *responses.Account // Full account information
	Valid   bool               // Whether the authentication is valid
}

AuthStatus represents the current authentication status

type CancelMessageResponse

type CancelMessageResponse struct {
	MessageID string // ID of the cancelled message
	Success   bool   // Whether cancellation was successful
	Error     string // Error message if failed
}

CancelMessageResponse represents a message cancellation result

type CheckConfig

type CheckConfig struct {
	FoundMessage    string   // Message to show when item is found
	NotFoundMessage string   // Message to show when item is not found
	FieldOrder      []string // Optional field ordering for table display
}

CheckConfig configures how check/verification responses are displayed

type CreateConfig

type CreateConfig struct {
	SuccessMessage string   // Message to show on successful creation
	ItemName       string   // Name of the item being created (e.g., "domain", "webhook")
	FieldOrder     []string // Optional field ordering for table display
}

CreateConfig configures how creation responses are displayed

type DeleteConfig

type DeleteConfig struct {
	SuccessMessage string // Message to show on successful deletion
	ItemName       string // Name of the item being deleted (e.g., "domain", "webhook")
}

DeleteConfig configures how deletion responses are displayed

type ListConfig

type ListConfig struct {
	SuccessMessage string   // Message to show on successful retrieval
	EmptyMessage   string   // Message to show when list is empty
	ShowPagination bool     // Whether to show pagination information
	FieldOrder     []string // Optional field ordering for table display
}

ListConfig configures how paginated list responses are displayed

type ResponseHandler

type ResponseHandler interface {
	// Error handling
	HandleError(err error) error

	// Domain responses
	HandleDomainList(response *responses.PaginatedDomainsResponse, config ListConfig) error
	HandleSingleDomain(domain *responses.Domain, config SingleConfig) error

	// Message responses
	HandleMessageList(response *responses.PaginatedMessagesResponse, config ListConfig) error
	HandleSingleMessage(message *responses.Message, config SingleConfig) error
	HandleCreateMessage(response *responses.CreateMessageResponse, config CreateConfig) error
	HandleCancelMessage(response *CancelMessageResponse, config SimpleConfig) error

	// Webhook responses
	HandleWebhookList(response *responses.PaginatedWebhooksResponse, config ListConfig) error
	HandleSingleWebhook(webhook *responses.Webhook, config SingleConfig) error
	HandleCreateWebhook(webhook *responses.Webhook, config CreateConfig) error
	HandleUpdateWebhook(webhook *responses.Webhook, config UpdateConfig) error
	HandleDeleteWebhook(success bool, config DeleteConfig) error
	HandleTriggerWebhook(webhookID string, events []string, config TriggerConfig) error

	// Route responses
	HandleRouteList(response *responses.PaginatedRoutesResponse, config ListConfig) error
	HandleSingleRoute(route *responses.Route, config SingleConfig) error
	HandleCreateRoute(route *responses.Route, config CreateConfig) error
	HandleUpdateRoute(route *responses.Route, config UpdateConfig) error
	HandleDeleteRoute(success bool, config DeleteConfig) error
	HandleTriggerRoute(routeID string, config TriggerConfig) error

	// Suppression responses
	HandleSuppressionList(response *responses.PaginatedSuppressionsResponse, config ListConfig) error
	HandleSingleSuppression(suppression *responses.Suppression, config SingleConfig) error
	HandleCreateSuppression(response *responses.CreateSuppressionResponse, config CreateConfig) error
	HandleDeleteSuppression(success bool, config DeleteConfig) error
	HandleWipeSuppression(count int, config WipeConfig) error
	HandleCheckSuppression(suppression *responses.Suppression, found bool, config CheckConfig) error

	// SMTP responses
	HandleSMTPList(response *responses.PaginatedSMTPCredentialsResponse, config ListConfig) error
	HandleSingleSMTP(credential *responses.SMTPCredential, config SingleConfig) error
	HandleCreateSMTP(credential *responses.SMTPCredential, config CreateConfig) error
	HandleDeleteSMTP(success bool, config DeleteConfig) error
	HandleSMTPSend(result *SMTPSendResult, config SMTPSendConfig) error

	// API Key responses
	HandleAPIKeyList(response *responses.PaginatedAPIKeysResponse, config ListConfig) error
	HandleSingleAPIKey(key *responses.APIKey, config SingleConfig) error
	HandleCreateAPIKey(key *responses.APIKey, config CreateConfig) error
	HandleUpdateAPIKey(key *responses.APIKey, config UpdateConfig) error
	HandleDeleteAPIKey(success bool, config DeleteConfig) error

	// Statistics responses
	HandleDeliverabilityStats(response *responses.DeliverabilityStatisticsResponse, config StatsConfig) error
	HandleBounceStats(response *responses.BounceStatisticsResponse, config StatsConfig) error
	HandleDeliveryTimeStats(response *responses.DeliveryTimeStatisticsResponse, config StatsConfig) error

	// Auth responses
	HandleAuthLogin(success bool, profile string, config AuthConfig) error
	HandleAuthLogout(success bool, config AuthConfig) error
	HandleAuthStatus(status *AuthStatus, config AuthConfig) error
	HandleAuthSwitch(newProfile string, config AuthConfig) error

	// Simple success without data
	HandleSimpleSuccess(message string) error

	// Empty state
	HandleEmpty(message string) error

	// Internal methods for format detection and output writing
	GetFormat() string
	SetWriter(w io.Writer)
}

ResponseHandler defines the interface for type-safe output formatting with concrete types. Every method uses specific SDK response types instead of interface{} for complete type safety.

func GetResponseHandler

func GetResponseHandler(format string, colorOutput bool, writer io.Writer) ResponseHandler

GetResponseHandler creates a new response handler based on the format string

func GetResponseHandlerFromCommand

func GetResponseHandlerFromCommand(cmd *cobra.Command) ResponseHandler

GetResponseHandlerFromCommand retrieves the response handler instance from the command context This is the main function commands should use to get their response handler

type SMTPSendConfig

type SMTPSendConfig struct {
	SuccessMessage string // Message to show on successful send
	TestMode       bool   // Whether this was a test send
}

SMTPSendConfig configures how SMTP send responses are displayed

type SMTPSendResult

type SMTPSendResult struct {
	Success   bool   // Whether the send was successful
	MessageID string // Message ID if successful
	Error     string // Error message if failed
	TestMode  bool   // Whether this was a test send
}

SMTPSendResult represents the result of an SMTP send operation

type SimpleConfig

type SimpleConfig struct {
	SuccessMessage string // Message to show on success
}

SimpleConfig configures simple success responses

type SingleConfig

type SingleConfig struct {
	SuccessMessage string   // Message to show on successful retrieval
	EmptyMessage   string   // Message to show when item is nil
	FieldOrder     []string // Optional field ordering for table display
}

SingleConfig configures how single item responses are displayed

type StatsConfig

type StatsConfig struct {
	Title      string   // Title for the statistics display
	ShowChart  bool     // Whether to show ASCII charts for data
	FieldOrder []string // Optional field ordering for table display
}

StatsConfig configures how statistics responses are displayed

type TriggerConfig

type TriggerConfig struct {
	SuccessMessage string // Message to show on successful trigger
}

TriggerConfig configures how webhook trigger responses are displayed

type UpdateConfig

type UpdateConfig struct {
	SuccessMessage string   // Message to show on successful update
	ItemName       string   // Name of the item being updated (e.g., "domain", "webhook")
	FieldOrder     []string // Optional field ordering for table display
}

UpdateConfig configures how update responses are displayed

type WipeConfig

type WipeConfig struct {
	SuccessMessage string // Message to show on successful wipe
	ItemName       string // Name of the items being wiped (e.g., "suppressions")
}

WipeConfig configures how wipe/bulk deletion responses are displayed

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL