refactor: Replace interface{} with any type alias across codebase

- Replace interface{} with any in api_integration_test.go makeRequest method and test response maps
- Replace interface{} with any in logging.go WithField method and all logging function signatures
- Replace interface{} with any in middleware etag.go sync.Map.Range callback
- Replace interface{} with any in middleware redis_ratelimit.go RedisClientInterface and result parsing
- Replace interface{} with any in middleware redis_ratelimit_
This commit is contained in:
nutcas3
2026-04-25 06:31:02 +03:00
parent cfd5ca63cf
commit 6cf546712b
12 changed files with 117 additions and 129 deletions
@@ -207,7 +207,7 @@ func setupTest(t *testing.T) *TestSetup {
} }
// Helper function to make authenticated requests // Helper function to make authenticated requests
func (ts *TestSetup) makeRequest(method, path string, body interface{}, token string) *httptest.ResponseRecorder { func (ts *TestSetup) makeRequest(method, path string, body any, token string) *httptest.ResponseRecorder {
var reqBody *bytes.Buffer var reqBody *bytes.Buffer
if body != nil { if body != nil {
jsonBody, _ := json.Marshal(body) jsonBody, _ := json.Marshal(body)
@@ -243,7 +243,7 @@ func TestAuthenticationEndpoints(t *testing.T) {
w := ts.makeRequest("POST", "/v1/auth/login", loginData, "") w := ts.makeRequest("POST", "/v1/auth/login", loginData, "")
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{} var response map[string]any
err := json.Unmarshal(w.Body.Bytes(), &response) err := json.Unmarshal(w.Body.Bytes(), &response)
require.NoError(t, err) require.NoError(t, err)
assert.Contains(t, response, "access_token") assert.Contains(t, response, "access_token")
@@ -293,7 +293,7 @@ func TestRBACPermissions(t *testing.T) {
}) })
t.Run("Viewer cannot write to plugins", func(t *testing.T) { t.Run("Viewer cannot write to plugins", func(t *testing.T) {
pluginData := map[string]interface{}{ pluginData := map[string]any{
"name": "test-plugin", "name": "test-plugin",
"version": "1.0.0", "version": "1.0.0",
} }
@@ -302,7 +302,7 @@ func TestRBACPermissions(t *testing.T) {
}) })
t.Run("Admin can write to plugins", func(t *testing.T) { t.Run("Admin can write to plugins", func(t *testing.T) {
pluginData := map[string]interface{}{ pluginData := map[string]any{
"name": "test-plugin", "name": "test-plugin",
"version": "1.0.0", "version": "1.0.0",
} }
@@ -342,14 +342,14 @@ func TestPluginEndpoints(t *testing.T) {
w := ts.makeRequest("GET", "/v1/plugins", nil, ts.AdminToken) w := ts.makeRequest("GET", "/v1/plugins", nil, ts.AdminToken)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{} var response map[string]any
err := json.Unmarshal(w.Body.Bytes(), &response) err := json.Unmarshal(w.Body.Bytes(), &response)
require.NoError(t, err) require.NoError(t, err)
assert.Contains(t, response, "plugins") assert.Contains(t, response, "plugins")
}) })
t.Run("Install plugin without permissions", func(t *testing.T) { t.Run("Install plugin without permissions", func(t *testing.T) {
pluginData := map[string]interface{}{ pluginData := map[string]any{
"name": "test-plugin", "name": "test-plugin",
"version": "1.0.0", "version": "1.0.0",
} }
@@ -372,14 +372,14 @@ func TestAutomationEndpoints(t *testing.T) {
w := ts.makeRequest("GET", "/v1/automation", nil, ts.AdminToken) w := ts.makeRequest("GET", "/v1/automation", nil, ts.AdminToken)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{} var response map[string]any
err := json.Unmarshal(w.Body.Bytes(), &response) err := json.Unmarshal(w.Body.Bytes(), &response)
require.NoError(t, err) require.NoError(t, err)
assert.Contains(t, response, "automations") assert.Contains(t, response, "automations")
}) })
t.Run("Create automation without permissions", func(t *testing.T) { t.Run("Create automation without permissions", func(t *testing.T) {
automationData := map[string]interface{}{ automationData := map[string]any{
"name": "test-automation", "name": "test-automation",
"description": "Test automation", "description": "Test automation",
"type": "scheduled", "type": "scheduled",
@@ -389,7 +389,7 @@ func TestAutomationEndpoints(t *testing.T) {
}) })
t.Run("Create automation with operator permissions", func(t *testing.T) { t.Run("Create automation with operator permissions", func(t *testing.T) {
automationData := map[string]interface{}{ automationData := map[string]any{
"name": "test-automation", "name": "test-automation",
"description": "Test automation", "description": "Test automation",
"type": "scheduled", "type": "scheduled",
@@ -410,7 +410,7 @@ func TestConfigurationEndpoints(t *testing.T) {
}) })
t.Run("Update configuration without permissions", func(t *testing.T) { t.Run("Update configuration without permissions", func(t *testing.T) {
configData := map[string]interface{}{ configData := map[string]any{
"section": "test", "section": "test",
"key": "test.config", "key": "test.config",
"value": "test-value", "value": "test-value",
@@ -421,7 +421,7 @@ func TestConfigurationEndpoints(t *testing.T) {
}) })
t.Run("Update configuration with admin permissions", func(t *testing.T) { t.Run("Update configuration with admin permissions", func(t *testing.T) {
configData := map[string]interface{}{ configData := map[string]any{
"section": "test", "section": "test",
"key": "test.config", "key": "test.config",
"value": "test-value", "value": "test-value",
@@ -441,7 +441,7 @@ func TestChaosEndpoints(t *testing.T) {
w := ts.makeRequest("GET", "/v1/chaos", nil, ts.AdminToken) w := ts.makeRequest("GET", "/v1/chaos", nil, ts.AdminToken)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{} var response map[string]any
err := json.Unmarshal(w.Body.Bytes(), &response) err := json.Unmarshal(w.Body.Bytes(), &response)
require.NoError(t, err) require.NoError(t, err)
assert.Contains(t, response, "active") assert.Contains(t, response, "active")
@@ -449,7 +449,7 @@ func TestChaosEndpoints(t *testing.T) {
}) })
t.Run("Run chaos experiment without permissions", func(t *testing.T) { t.Run("Run chaos experiment without permissions", func(t *testing.T) {
chaosData := map[string]interface{}{ chaosData := map[string]any{
"name": "test-chaos", "name": "test-chaos",
"type": "pod-delete", "type": "pod-delete",
"target": "test-pod", "target": "test-pod",
@@ -460,7 +460,7 @@ func TestChaosEndpoints(t *testing.T) {
}) })
t.Run("Run chaos experiment with admin permissions", func(t *testing.T) { t.Run("Run chaos experiment with admin permissions", func(t *testing.T) {
chaosData := map[string]interface{}{ chaosData := map[string]any{
"name": "test-chaos", "name": "test-chaos",
"type": "pod-delete", "type": "pod-delete",
"target": "test-pod", "target": "test-pod",
@@ -486,7 +486,7 @@ func TestErrorHandling(t *testing.T) {
}) })
t.Run("Missing required fields", func(t *testing.T) { t.Run("Missing required fields", func(t *testing.T) {
pluginData := map[string]interface{}{ pluginData := map[string]any{
"version": "1.0.0", "version": "1.0.0",
// Missing name field // Missing name field
} }
+14 -14
View File
@@ -11,9 +11,9 @@ var Logger *logrus.Logger
// Config holds logging configuration // Config holds logging configuration
type Config struct { type Config struct {
Level string Level string
Format string // "json" or "text" Format string // "json" or "text"
Output string // "stdout" or "stderr" Output string // "stdout" or "stderr"
} }
// DefaultConfig returns default logging configuration // DefaultConfig returns default logging configuration
@@ -76,7 +76,7 @@ func InitWithDefaults() error {
} }
// WithField returns a logger with a single field // WithField returns a logger with a single field
func WithField(key string, value interface{}) *logrus.Entry { func WithField(key string, value any) *logrus.Entry {
return Logger.WithField(key, value) return Logger.WithField(key, value)
} }
@@ -91,51 +91,51 @@ func WithError(err error) *logrus.Entry {
} }
// Info logs an info message // Info logs an info message
func Info(args ...interface{}) { func Info(args ...any) {
Logger.Info(args...) Logger.Info(args...)
} }
// Infof logs a formatted info message // Infof logs a formatted info message
func Infof(format string, args ...interface{}) { func Infof(format string, args ...any) {
Logger.Infof(format, args...) Logger.Infof(format, args...)
} }
// Warn logs a warning message // Warn logs a warning message
func Warn(args ...interface{}) { func Warn(args ...any) {
Logger.Warn(args...) Logger.Warn(args...)
} }
// Warnf logs a formatted warning message // Warnf logs a formatted warning message
func Warnf(format string, args ...interface{}) { func Warnf(format string, args ...any) {
Logger.Warnf(format, args...) Logger.Warnf(format, args...)
} }
// Error logs an error message // Error logs an error message
func Error(args ...interface{}) { func Error(args ...any) {
Logger.Error(args...) Logger.Error(args...)
} }
// Errorf logs a formatted error message // Errorf logs a formatted error message
func Errorf(format string, args ...interface{}) { func Errorf(format string, args ...any) {
Logger.Errorf(format, args...) Logger.Errorf(format, args...)
} }
// Fatal logs a fatal message and exits // Fatal logs a fatal message and exits
func Fatal(args ...interface{}) { func Fatal(args ...any) {
Logger.Fatal(args...) Logger.Fatal(args...)
} }
// Fatalf logs a formatted fatal message and exits // Fatalf logs a formatted fatal message and exits
func Fatalf(format string, args ...interface{}) { func Fatalf(format string, args ...any) {
Logger.Fatalf(format, args...) Logger.Fatalf(format, args...)
} }
// Debug logs a debug message // Debug logs a debug message
func Debug(args ...interface{}) { func Debug(args ...any) {
Logger.Debug(args...) Logger.Debug(args...)
} }
// Debugf logs a formatted debug message // Debugf logs a formatted debug message
func Debugf(format string, args ...interface{}) { func Debugf(format string, args ...any) {
Logger.Debugf(format, args...) Logger.Debugf(format, args...)
} }
+5 -5
View File
@@ -67,7 +67,7 @@ func (g *ETagGenerator) GenerateETag(data ETagData) string {
} }
// Include timestamp (rounded to second for consistency) // Include timestamp (rounded to second for consistency)
timestampBytes := []byte(fmt.Sprintf("%d", data.Timestamp.Unix())) timestampBytes := fmt.Appendf(nil, "%d", data.Timestamp.Unix())
h.Write(timestampBytes) h.Write(timestampBytes)
// Include user context for personalized responses // Include user context for personalized responses
@@ -111,7 +111,7 @@ func (g *ETagGenerator) CacheETag(key, etag string) {
// InvalidateCache removes ETag entries matching a pattern // InvalidateCache removes ETag entries matching a pattern
func (g *ETagGenerator) InvalidateCache(pattern string) { func (g *ETagGenerator) InvalidateCache(pattern string) {
g.cacheStorage.Range(func(key, value interface{}) bool { g.cacheStorage.Range(func(key, value any) bool {
if keyStr, ok := key.(string); ok { if keyStr, ok := key.(string); ok {
if strings.Contains(keyStr, pattern) { if strings.Contains(keyStr, pattern) {
g.cacheStorage.Delete(key) g.cacheStorage.Delete(key)
@@ -271,7 +271,7 @@ func CacheInvalidationMiddleware() gin.HandlerFunc {
// Invalidate cache for POST, PUT, PATCH, DELETE requests // Invalidate cache for POST, PUT, PATCH, DELETE requests
if c.Request.Method == "POST" || c.Request.Method == "PUT" || if c.Request.Method == "POST" || c.Request.Method == "PUT" ||
c.Request.Method == "PATCH" || c.Request.Method == "DELETE" { c.Request.Method == "PATCH" || c.Request.Method == "DELETE" {
// Invalidate ETags for this resource pattern // Invalidate ETags for this resource pattern
pattern := c.Request.URL.Path pattern := c.Request.URL.Path
@@ -297,8 +297,8 @@ func etagMatches(headerValue, currentETag string) bool {
} }
// Parse the header value (can contain multiple ETags) // Parse the header value (can contain multiple ETags)
etags := strings.Split(headerValue, ",") etags := strings.SplitSeq(headerValue, ",")
for _, etag := range etags { for etag := range etags {
etag = strings.TrimSpace(etag) etag = strings.TrimSpace(etag)
// Remove quotes for comparison // Remove quotes for comparison
etag = strings.Trim(etag, "\"") etag = strings.Trim(etag, "\"")
@@ -13,7 +13,7 @@ import (
// RedisClientInterface defines the interface for Redis operations // RedisClientInterface defines the interface for Redis operations
type RedisClientInterface interface { type RedisClientInterface interface {
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd Eval(ctx context.Context, script string, keys []string, args ...any) *redis.Cmd
Del(ctx context.Context, keys ...string) *redis.IntCmd Del(ctx context.Context, keys ...string) *redis.IntCmd
ZCount(ctx context.Context, key, min, max string) *redis.IntCmd ZCount(ctx context.Context, key, min, max string) *redis.IntCmd
} }
@@ -90,7 +90,7 @@ func (r *RedisRateLimiter) Allow(ctx context.Context, key string) (*RateLimitRes
} }
// Parse Lua script result // Parse Lua script result
resultSlice, ok := result.([]interface{}) resultSlice, ok := result.([]any)
if !ok || len(resultSlice) < 3 { if !ok || len(resultSlice) < 3 {
return nil, fmt.Errorf("invalid redis result format") return nil, fmt.Errorf("invalid redis result format")
} }
@@ -163,7 +163,7 @@ func (r *RedisRateLimiter) AllowTokenBucket(ctx context.Context, key string) (*R
return nil, fmt.Errorf("redis token bucket check failed: %w", err) return nil, fmt.Errorf("redis token bucket check failed: %w", err)
} }
resultSlice, ok := result.([]interface{}) resultSlice, ok := result.([]any)
if !ok || len(resultSlice) < 3 { if !ok || len(resultSlice) < 3 {
return nil, fmt.Errorf("invalid redis result format") return nil, fmt.Errorf("invalid redis result format")
} }
@@ -207,10 +207,7 @@ func (r *RedisRateLimiter) GetStats(ctx context.Context, key string) (*RateLimit
return nil, fmt.Errorf("failed to get rate limit stats: %w", err) return nil, fmt.Errorf("failed to get rate limit stats: %w", err)
} }
remaining := r.limit - int64(count) remaining := max(r.limit-int64(count), 0)
if remaining < 0 {
remaining = 0
}
return &RateLimitResult{ return &RateLimitResult{
Allowed: remaining > 0, Allowed: remaining > 0,
@@ -18,8 +18,8 @@ type MockRedisClient struct {
mock.Mock mock.Mock
} }
func (m *MockRedisClient) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd { func (m *MockRedisClient) Eval(ctx context.Context, script string, keys []string, args ...any) *redis.Cmd {
argsList := make([]interface{}, 0, len(keys)+len(args)+1) argsList := make([]any, 0, len(keys)+len(args)+1)
argsList = append(argsList, script) argsList = append(argsList, script)
for _, key := range keys { for _, key := range keys {
argsList = append(argsList, key) argsList = append(argsList, key)
@@ -55,11 +55,11 @@ func TestRedisRateLimiter_Allow(t *testing.T) {
mockClient := new(MockRedisClient) mockClient := new(MockRedisClient)
// Mock Redis response based on test case // Mock Redis response based on test case
var mockResult []interface{} var mockResult []any
if tt.expected { if tt.expected {
mockResult = []interface{}{int64(1), int64(tt.limit - 1), int64(time.Now().Unix() + 60)} mockResult = []any{int64(1), int64(tt.limit - 1), int64(time.Now().Unix() + 60)}
} else { } else {
mockResult = []interface{}{int64(0), int64(0), int64(time.Now().Unix() + 60), int64(30)} mockResult = []any{int64(0), int64(0), int64(time.Now().Unix() + 60), int64(30)}
} }
cmd := redis.NewCmd(context.Background()) cmd := redis.NewCmd(context.Background())
@@ -115,7 +115,7 @@ func TestRedisRateLimitMiddleware(t *testing.T) {
// Mock successful rate limit check // Mock successful rate limit check
cmd := redis.NewCmd(context.Background()) cmd := redis.NewCmd(context.Background())
cmd.SetVal([]interface{}{int64(1), int64(4), int64(time.Now().Unix() + 60)}) cmd.SetVal([]any{int64(1), int64(4), int64(time.Now().Unix() + 60)})
mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(cmd) mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(cmd)
// Create rate limiter with mock client // Create rate limiter with mock client
@@ -267,10 +267,10 @@ func TestMultiTierRateLimiter(t *testing.T) {
// Mock different tier responses // Mock different tier responses
freeCmd := redis.NewCmd(context.Background()) freeCmd := redis.NewCmd(context.Background())
freeCmd.SetVal([]interface{}{int64(1), int64(9), int64(time.Now().Unix() + 60)}) freeCmd.SetVal([]any{int64(1), int64(9), int64(time.Now().Unix() + 60)})
premiumCmd := redis.NewCmd(context.Background()) premiumCmd := redis.NewCmd(context.Background())
premiumCmd.SetVal([]interface{}{int64(1), int64(99), int64(time.Now().Unix() + 60)}) premiumCmd.SetVal([]any{int64(1), int64(99), int64(time.Now().Unix() + 60)})
mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(freeCmd).Once() mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(freeCmd).Once()
mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(premiumCmd).Once() mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(premiumCmd).Once()
+7 -7
View File
@@ -20,9 +20,9 @@ type MessageQueue struct {
// Message represents a message to be sent/received // Message represents a message to be sent/received
type Message struct { type Message struct {
Type string `json:"type"` Type string `json:"type"`
Payload map[string]interface{} `json:"payload"` Payload map[string]any `json:"payload"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
} }
// NewMessageQueue creates a new message queue connection // NewMessageQueue creates a new message queue connection
@@ -162,10 +162,10 @@ func (mq *MessageQueue) PublishToExchange(exchange, routingKey string, msg Messa
err = mq.channel.PublishWithContext( err = mq.channel.PublishWithContext(
ctx, ctx,
exchange, // exchange exchange, // exchange
routingKey, // routing key routingKey, // routing key
false, // mandatory false, // mandatory
false, // immediate false, // immediate
amqp.Publishing{ amqp.Publishing{
ContentType: "application/json", ContentType: "application/json",
DeliveryMode: amqp.Persistent, DeliveryMode: amqp.Persistent,
@@ -147,8 +147,8 @@ func TestAutomationService_Create(t *testing.T) {
_, service := setupAutomationTest(t) _, service := setupAutomationTest(t)
t.Run("Create automation successfully", func(t *testing.T) { t.Run("Create automation successfully", func(t *testing.T) {
definition := map[string]interface{}{ definition := map[string]any{
"actions": []map[string]interface{}{ "actions": []map[string]any{
{ {
"type": "restart", "type": "restart",
"target": "service", "target": "service",
@@ -313,7 +313,7 @@ func BenchmarkAutomationService_List(b *testing.B) {
db, service := setupAutomationTest(&testing.T{}) db, service := setupAutomationTest(&testing.T{})
// Create test automations // Create test automations
for i := 0; i < 100; i++ { for i := range 100 {
automation := models.Automation{ automation := models.Automation{
Name: fmt.Sprintf("automation-%d", i), Name: fmt.Sprintf("automation-%d", i),
Description: "Test automation", Description: "Test automation",
@@ -110,7 +110,7 @@ func TestPluginService_Install(t *testing.T) {
_, service := setupPluginTest(t) _, service := setupPluginTest(t)
t.Run("Install new plugin successfully", func(t *testing.T) { t.Run("Install new plugin successfully", func(t *testing.T) {
configJSON, _ := json.Marshal(map[string]interface{}{"key": "value"}) configJSON, _ := json.Marshal(map[string]any{"key": "value"})
input := InstallPluginInput{ input := InstallPluginInput{
Name: "new-plugin", Name: "new-plugin",
Version: "1.0.0", Version: "1.0.0",
@@ -235,7 +235,7 @@ func BenchmarkPluginService_List(b *testing.B) {
db, service := setupPluginTest(&testing.T{}) db, service := setupPluginTest(&testing.T{})
// Create test plugins // Create test plugins
for i := 0; i < 100; i++ { for i := range 100 {
plugin := models.Plugin{ plugin := models.Plugin{
Name: fmt.Sprintf("plugin-%d", i), Name: fmt.Sprintf("plugin-%d", i),
Version: "1.0.0", Version: "1.0.0",
@@ -43,7 +43,7 @@ func CheckConnectivityHandler(client *es2.ES2Client) gin.HandlerFunc {
_, err := client.GetProfileStatus(context.Background(), req) _, err := client.GetProfileStatus(context.Background(), req)
connected := err == nil connected := err == nil
var errMsg interface{} var errMsg any
if err != nil { if err != nil {
errMsg = err.Error() errMsg = err.Error()
} }
@@ -71,7 +71,7 @@ func OrderProfileHandlerWithRepo(client *es2.ES2Client, repo repository.ProfileR
// Send webhook notification for successful download // Send webhook notification for successful download
if webhookClient != nil { if webhookClient != nil {
go webhookClient.SendProfileDownloaded(context.Background(), order.ICCID, map[string]interface{}{ go webhookClient.SendProfileDownloaded(context.Background(), order.ICCID, map[string]any{
"imsi": order.IMSI, "imsi": order.IMSI,
"profileType": order.ProfileType, "profileType": order.ProfileType,
"status": downloadResp.ExecutionStatus, "status": downloadResp.ExecutionStatus,
@@ -141,10 +141,7 @@ func GetProfileHandlerWithRepo(client *es2.ES2Client, repo repository.ProfileRep
func ListProfilesHandlerWithRepo(repo repository.ProfileRepository) gin.HandlerFunc { func ListProfilesHandlerWithRepo(repo repository.ProfileRepository) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
page := parsePositiveInt(c.Query("page"), 1) page := parsePositiveInt(c.Query("page"), 1)
limit := parsePositiveInt(c.Query("limit"), 20) limit := min(parsePositiveInt(c.Query("limit"), 20), 100)
if limit > 100 {
limit = 100
}
Logger.Info().Int("page", page).Int("limit", limit).Msg("Listing eSIM profiles") Logger.Info().Int("page", page).Int("limit", limit).Msg("Listing eSIM profiles")
@@ -194,7 +191,7 @@ func DeleteProfileHandlerWithRepo(client *es2.ES2Client, repo repository.Profile
// Send webhook notification for profile deletion // Send webhook notification for profile deletion
if webhookClient != nil { if webhookClient != nil {
go webhookClient.SendProfileDeleted(context.Background(), profileID, map[string]interface{}{ go webhookClient.SendProfileDeleted(context.Background(), profileID, map[string]any{
"executionStatus": deleteResp.ExecutionStatus, "executionStatus": deleteResp.ExecutionStatus,
"statusMessage": deleteResp.StatusMessage, "statusMessage": deleteResp.StatusMessage,
}) })
@@ -29,18 +29,18 @@ func NewWebhookClient(webhookURL, apiKey string) *WebhookClient {
// WebhookEvent represents a webhook notification event // WebhookEvent represents a webhook notification event
type WebhookEvent struct { type WebhookEvent struct {
EventType string `json:"event_type"` EventType string `json:"event_type"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
ProfileICCID string `json:"profile_iccid"` ProfileICCID string `json:"profile_iccid"`
Data map[string]interface{} `json:"data"` Data map[string]any `json:"data"`
} }
// Event types // Event types
const ( const (
EventProfileDownloaded = "profile.downloaded" EventProfileDownloaded = "profile.downloaded"
EventProfileActivated = "profile.activated" EventProfileActivated = "profile.activated"
EventProfileDeactivated = "profile.deactivated" EventProfileDeactivated = "profile.deactivated"
EventProfileDeleted = "profile.deleted" EventProfileDeleted = "profile.deleted"
EventProfileStatusChange = "profile.status_changed" EventProfileStatusChange = "profile.status_changed"
EventDownloadFailed = "download.failed" EventDownloadFailed = "download.failed"
) )
@@ -80,34 +80,34 @@ func (w *WebhookClient) SendNotification(ctx context.Context, event *WebhookEven
} }
// SendProfileDownloaded sends a notification when a profile is downloaded // SendProfileDownloaded sends a notification when a profile is downloaded
func (w *WebhookClient) SendProfileDownloaded(ctx context.Context, iccid string, data map[string]interface{}) error { func (w *WebhookClient) SendProfileDownloaded(ctx context.Context, iccid string, data map[string]any) error {
event := &WebhookEvent{ event := &WebhookEvent{
EventType: EventProfileDownloaded, EventType: EventProfileDownloaded,
Timestamp: time.Now(), Timestamp: time.Now(),
ProfileICCID: iccid, ProfileICCID: iccid,
Data: data, Data: data,
} }
return w.SendNotification(ctx, event) return w.SendNotification(ctx, event)
} }
// SendProfileActivated sends a notification when a profile is activated // SendProfileActivated sends a notification when a profile is activated
func (w *WebhookClient) SendProfileActivated(ctx context.Context, iccid string, data map[string]interface{}) error { func (w *WebhookClient) SendProfileActivated(ctx context.Context, iccid string, data map[string]any) error {
event := &WebhookEvent{ event := &WebhookEvent{
EventType: EventProfileActivated, EventType: EventProfileActivated,
Timestamp: time.Now(), Timestamp: time.Now(),
ProfileICCID: iccid, ProfileICCID: iccid,
Data: data, Data: data,
} }
return w.SendNotification(ctx, event) return w.SendNotification(ctx, event)
} }
// SendProfileDeleted sends a notification when a profile is deleted // SendProfileDeleted sends a notification when a profile is deleted
func (w *WebhookClient) SendProfileDeleted(ctx context.Context, iccid string, data map[string]interface{}) error { func (w *WebhookClient) SendProfileDeleted(ctx context.Context, iccid string, data map[string]any) error {
event := &WebhookEvent{ event := &WebhookEvent{
EventType: EventProfileDeleted, EventType: EventProfileDeleted,
Timestamp: time.Now(), Timestamp: time.Now(),
ProfileICCID: iccid, ProfileICCID: iccid,
Data: data, Data: data,
} }
return w.SendNotification(ctx, event) return w.SendNotification(ctx, event)
} }
@@ -115,10 +115,10 @@ func (w *WebhookClient) SendProfileDeleted(ctx context.Context, iccid string, da
// SendDownloadFailed sends a notification when a profile download fails // SendDownloadFailed sends a notification when a profile download fails
func (w *WebhookClient) SendDownloadFailed(ctx context.Context, iccid string, errorMessage string) error { func (w *WebhookClient) SendDownloadFailed(ctx context.Context, iccid string, errorMessage string) error {
event := &WebhookEvent{ event := &WebhookEvent{
EventType: EventDownloadFailed, EventType: EventDownloadFailed,
Timestamp: time.Now(), Timestamp: time.Now(),
ProfileICCID: iccid, ProfileICCID: iccid,
Data: map[string]interface{}{ Data: map[string]any{
"error": errorMessage, "error": errorMessage,
}, },
} }
+2 -8
View File
@@ -1,9 +1,9 @@
package types package types
import ( import (
"slices"
"fmt" "fmt"
"net/url" "net/url"
"slices"
"strings" "strings"
) )
@@ -51,13 +51,7 @@ func (c *CLIConfig) Validate() []error {
// Validate theme // Validate theme
validThemes := []string{"default", "light", "dark", "high-contrast"} validThemes := []string{"default", "light", "dark", "high-contrast"}
if c.Theme != "" { if c.Theme != "" {
valid := false valid := slices.Contains(validThemes, c.Theme)
for _, t := range validThemes {
if c.Theme == t {
valid = true
break
}
}
if !valid { if !valid {
errors = append(errors, fmt.Errorf("invalid theme '%s', must be one of: %v", c.Theme, validThemes)) errors = append(errors, fmt.Errorf("invalid theme '%s', must be one of: %v", c.Theme, validThemes))
} }