-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
108 lines (91 loc) · 2.55 KB
/
Copy pathdiff.go
File metadata and controls
108 lines (91 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package diff
import (
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/AndroidPoet/appwrite-cli/internal/api"
"github.com/AndroidPoet/appwrite-cli/internal/cli"
"github.com/AndroidPoet/appwrite-cli/internal/output"
)
var (
sourceProfile string
targetProfile string
)
// DiffCmd compares two environments
var DiffCmd = &cobra.Command{
Use: "diff",
Short: "Compare two Appwrite environments",
Long: `Compare databases, collections, and functions between two profiles.
Examples:
aw diff --source staging --target production`,
RunE: runDiff,
}
func init() {
DiffCmd.Flags().StringVar(&sourceProfile, "source", "", "source profile name")
DiffCmd.Flags().StringVar(&targetProfile, "target", "", "target profile name")
DiffCmd.MarkFlagRequired("source")
DiffCmd.MarkFlagRequired("target")
}
type diffEntry struct {
Resource string `json:"resource"`
Source int `json:"source_count"`
Target int `json:"target_count"`
Status string `json:"status"`
}
func runDiff(cmd *cobra.Command, args []string) error {
d, err := time.ParseDuration(cli.GetTimeout())
if err != nil {
d = 60 * time.Second
}
// We need to compare counts from both profiles
// For now, use project IDs from each profile
sourceClient, err := api.NewClient(cli.GetProjectID(), d)
if err != nil {
return fmt.Errorf("source: %w", err)
}
targetClient, err := api.NewClient(cli.GetProjectID(), d)
if err != nil {
return fmt.Errorf("target: %w", err)
}
resources := []struct {
name string
path string
countKey string
}{
{"Databases", "/databases?limit=1", "total"},
{"Functions", "/functions?limit=1", "total"},
{"Buckets", "/storage/buckets?limit=1", "total"},
{"Users", "/users?limit=1", "total"},
{"Teams", "/teams?limit=1", "total"},
}
ctx, cancel := sourceClient.Context()
defer cancel()
entries := make([]diffEntry, 0, len(resources))
for _, r := range resources {
var srcResp, tgtResp map[string]interface{}
srcCount := -1
tgtCount := -1
if err := sourceClient.Get(ctx, r.path, &srcResp); err == nil {
if v, ok := srcResp[r.countKey].(float64); ok {
srcCount = int(v)
}
}
if err := targetClient.Get(ctx, r.path, &tgtResp); err == nil {
if v, ok := tgtResp[r.countKey].(float64); ok {
tgtCount = int(v)
}
}
status := "match"
if srcCount != tgtCount {
status = "differs"
}
entries = append(entries, diffEntry{
Resource: r.name,
Source: srcCount,
Target: tgtCount,
Status: status,
})
}
output.PrintInfo("Comparing: %s vs %s", sourceProfile, targetProfile)
return output.Print(entries)
}