-
Notifications
You must be signed in to change notification settings - Fork 9
/
printsealed.go
102 lines (86 loc) · 2.7 KB
/
printsealed.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
// 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 debug
import (
"context"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/planning/deploy"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/std/cfg"
)
func newPrintSealedCmd() *cobra.Command {
var (
outputType string = "json"
printDeploy bool
env cfg.Context
locs fncobra.Locations
)
return fncobra.
Cmd(&cobra.Command{
Use: "print-sealed",
Short: "Load a server definition and print it's computed sealed workspace as JSON.",
}).
WithFlags(func(flags *pflag.FlagSet) {
flags.StringVar(&outputType, "output", outputType, "One of json, textproto.")
flags.BoolVar(&printDeploy, "deploy_stack", false, "If specified, prints the sealed workspace for each of the servers in the stack.")
}).
With(
fncobra.ParseEnv(&env),
fncobra.ParseLocations(&locs, &env, fncobra.ParseLocationsOpts{RequireSingle: true})).
Do(func(ctx context.Context) error {
pl := parsing.NewPackageLoader(env)
loc := locs.Locs[0]
cluster, err := runtime.PlannerFor(ctx, env)
if err != nil {
return err
}
if !printDeploy {
sealed, err := parsing.Seal(ctx, pl, loc.AsPackageName(), nil)
if err != nil {
return err
}
return output(ctx, pl, sealed.Proto, outputType)
} else {
t, err := planning.RequireServer(ctx, env, loc.AsPackageName())
if err != nil {
return err
}
plan, err := deploy.PrepareDeployServers(ctx, env, cluster, t)
if err != nil {
return err
}
computedPlan, err := compute.GetValue(ctx, plan)
if err != nil {
return err
}
stack := computedPlan.ComputedStack.Proto()
for _, s := range stack.Entry {
if err := output(ctx, t.SealedContext(), s, outputType); err != nil {
return err
}
}
for _, endpoint := range stack.Endpoint {
if err := output(ctx, t.SealedContext(), endpoint, outputType); err != nil {
return err
}
}
for _, endpoint := range stack.InternalEndpoint {
if err := output(ctx, t.SealedContext(), endpoint, outputType); err != nil {
return err
}
}
for _, ingress := range computedPlan.IngressFragments {
if err := output(ctx, t.SealedContext(), ingress, outputType); err != nil {
return err
}
}
return nil
}
})
}