-
Notifications
You must be signed in to change notification settings - Fork 9
/
attach.go
75 lines (66 loc) · 2.34 KB
/
attach.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
// 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 cmd
import (
"context"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/cli/keyboard"
"namespacelabs.dev/foundation/internal/devsession"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/logs/logtail"
"namespacelabs.dev/foundation/internal/observers"
"namespacelabs.dev/foundation/internal/planning/deploy/view"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/storage"
"namespacelabs.dev/foundation/std/cfg"
)
func NewAttachCmd() *cobra.Command {
var res hydrateResult
return fncobra.
Cmd(&cobra.Command{
Use: "attach [path/to/server]...",
Short: "Attaches to the specified environment, of the specified servers.",
Args: cobra.ArbitraryArgs}).
With(parseHydrationWithDeps(&res, &fncobra.ParseLocationsOpts{ReturnAllIfNoneSpecified: true}, &hydrateOpts{rehydrate: true})...).
Do(func(ctx context.Context) error {
event := &observers.StackUpdateEvent{
Env: res.Env.Environment(),
Stack: res.Stack,
Focus: schema.Strs(res.Focus...),
}
observer := observers.Static()
observer.PushUpdate(event)
cluster, err := runtime.NamespaceFor(ctx, res.Env)
if err != nil {
return err
}
return keyboard.Handle(ctx, keyboard.HandleOpts{
Provider: observer,
Keybindings: []keyboard.Handler{
logtail.Keybinding{
LoadEnvironment: func(name string) (cfg.Context, error) {
if name == res.Env.Environment().Name {
return res.Env, nil
}
return nil, fnerrors.InternalError("requested invalid environment: %s", name)
},
},
view.NewNetworkPlanKeybinding("ingress"),
},
Handler: func(ctx context.Context) error {
pfwd := devsession.NewPortFwd(ctx, nil, res.Env, cluster, "localhost")
pfwd.OnUpdate = func(plan *storage.NetworkPlan) {
event := protos.Clone(event)
event.NetworkPlan = plan
observer.PushUpdate(event)
}
pfwd.Update(res.Stack, res.Focus, res.Ingress)
return nil
},
})
})
}