-
Notifications
You must be signed in to change notification settings - Fork 221
/
main.go
79 lines (75 loc) · 1.8 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"fmt"
"os"
"os/exec"
"time"
"github.com/google/shlex"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/logger"
"github.com/superfly/flyctl/iostreams"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
var (
ctx = context.TODO()
apiClient = api.NewClientFromOptions(api.ClientOptions{
AccessToken: os.Getenv("FLY_PREFLIGHT_TEST_ACCESS_TOKEN"),
Name: buildinfo.Name(),
Version: buildinfo.Version().String(),
BaseURL: "https://api.fly.io",
Logger: logger.FromEnv(iostreams.System().ErrOut),
})
)
if apiClient == nil {
return fmt.Errorf("failed to init api client :-(")
}
_ = `# @genqlient
query AllApps($orgSlug:String!) {
organization(slug:$orgSlug) {
apps {
nodes {
id
createdAt
}
}
}
}`
resp, err := gql.AllApps(ctx, apiClient.GenqClient, os.Getenv("FLY_PREFLIGHT_TEST_FLY_ORG"))
if err != nil {
return err
}
for _, app := range resp.Organization.Apps.Nodes {
if time.Since(app.CreatedAt) > 30*time.Minute {
flyctlBin := "flyctl"
cmdStr := fmt.Sprintf("%s apps destroy --yes %s", flyctlBin, app.Id)
cmdParts, err := shlex.Split(cmdStr)
if err != nil {
return err
}
cmd := exec.CommandContext(ctx, flyctlBin, cmdParts[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, fmt.Sprintf("FLY_API_TOKEN=%s", os.Getenv("FLY_PREFLIGHT_TEST_ACCESS_TOKEN")))
fmt.Fprintln(os.Stderr, cmdStr)
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
}
}
return nil
}