-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (63 loc) · 1.87 KB
/
main.go
File metadata and controls
74 lines (63 loc) · 1.87 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
// Example demonstrates updating script configuration content from a file in CacheFly.
//
// This example shows:
// - Client initialization with API token
// - Updating script config value using file content
// - Error handling and response formatting
//
// Usage:
//
// export CACHEFLY_API_TOKEN="your-token"
// go run main.go <config_id> <file_path>
//
// Example:
//
// go run main.go cfg_123456789 config.yaml
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/cachefly/cachefly-sdk-go/pkg/cachefly"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables from .env file
if err := godotenv.Load(); err != nil {
log.Printf("⚠️ Warning: unable to load .env file: %v", err)
}
// Read API token
token := os.Getenv("CACHEFLY_API_TOKEN")
if token == "" {
log.Fatal("❌ CACHEFLY_API_TOKEN environment variable is required")
}
// Read Script Config ID and file path arguments
if len(os.Args) < 3 {
log.Fatalf("⚠️ Usage: go run main.go <config_id> <script_file_path>")
}
configID := os.Args[1]
filePath := os.Args[2]
// Read the script content from file
content, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("❌ Failed to read script file %s: %v", filePath, err)
}
// Initialize CacheFly client
client := cachefly.NewClient(
cachefly.WithToken(token),
)
// Update the script config value from file
updatedConfig, err := client.ScriptConfigs.UpdateValueAsFile(context.Background(), configID, content)
if err != nil {
log.Fatalf("❌ Failed to update script config %s from file: %v", configID, err)
}
// Pretty-print the updated configuration
out, err := json.MarshalIndent(updatedConfig, "", " ")
if err != nil {
log.Fatalf("❌ Error formatting updated script config JSON: %v", err)
}
fmt.Println("\n✅ Script configuration value updated from file successfully:")
fmt.Println(string(out))
}