-
Notifications
You must be signed in to change notification settings - Fork 94
/
main.go
60 lines (48 loc) · 1.54 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
tfe "github.com/hashicorp/go-tfe"
)
// instanceAddr contains the state target that will be forcibly replaced every run
const instanceAddr = "module.tflocal.module.tfbox.aws_instance.tfbox"
// tokenAddr contains the target token that will be forcibly replaced every run
const tokenAddr = "module.tflocal.var.tflocal_cloud_admin_token"
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if len(os.Args) < 3 {
log.Fatal("usage: <organization-name> <workspace-name>")
}
organizationName := os.Args[1]
workspaceName := os.Args[2]
if err := triggerRun(ctx, organizationName, workspaceName); err != nil {
log.Fatal(err)
}
}
func triggerRun(ctx context.Context, organizationName, workspaceName string) error {
config := &tfe.Config{Token: os.Getenv("TFE_TOKEN")}
client, err := tfe.NewClient(config)
if err != nil {
return fmt.Errorf("client initialization error: %w", err)
}
wk, err := client.Workspaces.Read(ctx, organizationName, workspaceName)
if err != nil {
return fmt.Errorf("failed to read workspace: %w", err)
}
run, err := client.Runs.Create(ctx, tfe.RunCreateOptions{
IsDestroy: tfe.Bool(false),
Message: tfe.String("Queued nightly from tflocal-cloud GH Actions via go-tfe"),
Workspace: wk,
AutoApply: tfe.Bool(true),
ReplaceAddrs: []string{instanceAddr, tokenAddr},
})
if err != nil {
return fmt.Errorf("failed to trigger run: %w", err)
}
fmt.Println("Created run: " + run.ID)
return nil
}