-
Notifications
You must be signed in to change notification settings - Fork 0
/
program-common.go
104 lines (79 loc) · 2.11 KB
/
program-common.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
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
package pulumirunner
import (
"bytes"
"context"
"fmt"
"io"
"os"
"github.com/katasec/pulumi-runner/utils"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
)
func setConfig(w io.Writer, ctx context.Context, s auto.Stack, config []map[string]string) (auto.Stack, error) {
// Set stack config if specified:
if config != nil {
// set stack configuration using name/value from map
for _, key := range config {
err := s.SetConfig(ctx, key["name"], auto.ConfigValue{Value: key["value"]})
if err != nil {
return s, err
}
}
utils.Fprintln(w, "Successfully set config")
}
return s, nil
}
func refreshStack(w io.Writer, ctx context.Context, s auto.Stack) error {
utils.Fprintln(w, "Starting refresh")
result, err := s.Refresh(ctx)
if err != nil {
utils.Fprintln(w, fmt.Sprintf("Failed to refresh stack: %v\n", err))
return err
}
utils.Fprintln(w, fmt.Sprintf("Refresh succeeded!, Result:%s \n", result.Summary.Result))
return nil
}
func validateRemoteArgs(args *RemoteProgramArgs) {
if args.ProjectName == "" {
exitMessage("ProjectName cannot be empty")
}
if args.GitURL == "" {
exitMessage("GitURL cannot be empty")
}
if args.StackName == "" {
exitMessage("StackName cannot be empty")
}
}
func validateInlineArgs(args *InlineProgramArgs) {
if args.ProjectName == "" {
exitMessage("ProjectName cannot be empty")
}
if args.StackName == "" {
exitMessage("StackName cannot be empty")
}
}
func validateLocalArgs(args *LocalProgramArgs) {
if args.ProjectName == "" {
exitMessage("ProjectName cannot be empty")
}
if args.StackName == "" {
exitMessage("StackName cannot be empty")
}
}
func exitMessage(message string) {
utils.Fprintln(os.Stderr, message)
//os.Exit(1)
}
func replaceInFile(filepath string, src string, dst string) error {
fmt.Printf("Replacing source: %s\n Destination: %s\n File: %s\n", src, dst, filepath)
input, err := os.ReadFile(filepath)
if err != nil {
fmt.Println(err)
return err
}
output := bytes.Replace(input, []byte(src), []byte(dst), 1)
if err = os.WriteFile(filepath, output, 0666); err != nil {
fmt.Println(err)
return err
}
return nil
}