-
Notifications
You must be signed in to change notification settings - Fork 9
/
support.go
54 lines (41 loc) · 1.34 KB
/
support.go
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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package provider
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"namespacelabs.dev/foundation/framework/resources"
)
func MustPrepare(intent any) (context.Context, *resources.Parsed) {
intentFlag := flag.String("intent", "", "The serialized JSON intent.")
resourcesFlag := flag.String("resources", "", "The serialized JSON resources.")
flag.Parse()
resources, err := prepare(*intentFlag, *resourcesFlag, intent)
if err != nil {
log.Fatal(err.Error())
}
return context.Background(), resources
}
func EmitResult(instance any) {
serialized, err := json.Marshal(instance)
if err != nil {
log.Fatalf("failed to marshal instance: %v", err)
}
fmt.Printf("namespace.provision.result: %s\n", serialized)
}
func prepare(intentFlag, resourcesFlag string, intent any) (*resources.Parsed, error) {
if intentFlag == "" {
return nil, fmt.Errorf("--intent is required")
}
if err := json.Unmarshal([]byte(intentFlag), intent); err != nil {
return nil, fmt.Errorf("failed to decode intent: %w", err)
}
if resourcesFlag == "" {
return nil, fmt.Errorf("--resources is required")
}
return resources.ParseResourceData([]byte(resourcesFlag))
}