-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
195 lines (165 loc) · 4.93 KB
/
main.go
File metadata and controls
195 lines (165 loc) · 4.93 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/go-resty/resty/v2"
"go.yaml.in/yaml/v4"
)
const (
apiURL = "https://api.acepanel.net/template/import"
batchSize = 10
)
func main() {
apiKey := os.Getenv("API_KEY")
if apiKey == "" {
fmt.Println("Error: API_KEY environment variable is not set")
os.Exit(1)
}
// Get current working directory
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting current directory: %v\n", err)
os.Exit(1)
}
// Find all data.yml files
var templates []map[string]any
entries, err := os.ReadDir(cwd)
if err != nil {
fmt.Printf("Error reading directory: %v\n", err)
os.Exit(1)
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
dataPath := filepath.Join(cwd, entry.Name(), "data.yml")
if _, err := os.Stat(dataPath); os.IsNotExist(err) {
continue
}
// Read and parse data.yml
data, err := os.ReadFile(dataPath)
if err != nil {
fmt.Printf("Error reading %s: %v\n", dataPath, err)
continue
}
var template map[string]any
if err := yaml.Unmarshal(data, &template); err != nil {
fmt.Printf("Error parsing %s: %v\n", dataPath, err)
continue
}
// Also parse into Node to preserve YAML key ordering
var docNode yaml.Node
if err := yaml.Unmarshal(data, &docNode); err != nil {
fmt.Printf("Error parsing %s: %v\n", dataPath, err)
continue
}
// Add directory name as template slug
template["slug"] = entry.Name()
// Convert environments from map to array format, preserving YAML order
// From: {ENV_NAME: {description: xxx, type: xxx, default: xxx}}
// To: [{name: ENV_NAME, description: xxx, type: xxx, default: xxx}]
if envMap, ok := template["environments"].(map[string]any); ok {
envArray := make([]map[string]any, 0, len(envMap))
if envNode := findNodeValue(&docNode, "environments"); envNode != nil {
for i := 0; i < len(envNode.Content)-1; i += 2 {
name := envNode.Content[i].Value
if value, exists := envMap[name]; exists {
if envValue, ok := value.(map[string]any); ok {
envItem := map[string]any{"name": name}
for k, v := range envValue {
envItem[k] = v
}
envArray = append(envArray, envItem)
}
}
}
}
template["environments"] = envArray
}
// Read docker-compose.yml
composePath := filepath.Join(cwd, entry.Name(), "docker-compose.yml")
composeData, err := os.ReadFile(composePath)
if err != nil {
fmt.Printf("Error reading docker-compose.yml for %s: %v\n", entry.Name(), err)
continue
}
template["compose"] = string(composeData)
// Read and encode logo (check svg first, then png)
var logoPath string
var logoMime string
svgPath := filepath.Join(cwd, entry.Name(), "logo.svg")
pngPath := filepath.Join(cwd, entry.Name(), "logo.png")
if _, err := os.Stat(svgPath); err == nil {
logoPath = svgPath
logoMime = "image/svg+xml"
} else if _, err := os.Stat(pngPath); err == nil {
logoPath = pngPath
logoMime = "image/png"
}
if logoPath != "" {
logoData, err := os.ReadFile(logoPath)
if err != nil {
fmt.Printf("Error reading logo for %s: %v\n", entry.Name(), err)
} else {
template["icon"] = "data:" + logoMime + ";base64," + base64.StdEncoding.EncodeToString(logoData)
}
}
templates = append(templates, template)
fmt.Printf("Loaded template: %s\n", entry.Name())
}
fmt.Printf("\nTotal templates loaded: %d\n", len(templates))
if len(templates) == 0 {
fmt.Println("No templates found")
return
}
// Send templates in batches
client := resty.New()
totalBatches := (len(templates) + batchSize - 1) / batchSize
for i := 0; i < len(templates); i += batchSize {
end := i + batchSize
if end > len(templates) {
end = len(templates)
}
batch := templates[i:end]
batchNum := i/batchSize + 1
jsonData, err := json.Marshal(batch)
if err != nil {
fmt.Printf("Error marshaling batch %d: %v\n", batchNum, err)
continue
}
fmt.Printf("\nSending batch %d/%d (%d templates)...\n", batchNum, totalBatches, len(batch))
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetHeader("X-API-KEY", apiKey).
SetBody(jsonData).
Post(apiURL)
if err != nil {
fmt.Printf("Error sending batch %d: %v\n", batchNum, err)
continue
}
if resp.IsSuccess() {
fmt.Printf("Batch %d sent successfully (status: %d)\n", batchNum, resp.StatusCode())
} else {
fmt.Printf("Batch %d failed (status: %d): %s\n", batchNum, resp.StatusCode(), resp.String())
}
}
fmt.Println("\nImport completed!")
}
// findNodeValue finds the value node for a given key in a YAML document's root mapping.
func findNodeValue(node *yaml.Node, key string) *yaml.Node {
if node.Kind == yaml.DocumentNode && len(node.Content) > 0 {
node = node.Content[0]
}
if node.Kind != yaml.MappingNode {
return nil
}
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == key {
return node.Content[i+1]
}
}
return nil
}