-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive.go
More file actions
119 lines (109 loc) · 3.39 KB
/
Copy pathdrive.go
File metadata and controls
119 lines (109 loc) · 3.39 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-License-Identifier: EUPL-1.2
// Drive is the resource handle registry for transport connections.
// Packages register their transport handles (API, MCP, SSH, VPN)
// and other packages access them by name.
//
// Register a transport:
//
// c.Drive().New(core.NewOptions(
// core.Option{Key: "name", Value: "api"},
// core.Option{Key: "transport", Value: "https://api.lthn.ai"},
// ))
// c.Drive().New(core.NewOptions(
// core.Option{Key: "name", Value: "ssh"},
// core.Option{Key: "transport", Value: "ssh://claude@10.69.69.165"},
// ))
// c.Drive().New(core.NewOptions(
// core.Option{Key: "name", Value: "mcp"},
// core.Option{Key: "transport", Value: "mcp://mcp.lthn.sh"},
// ))
//
// Retrieve a handle:
//
// api := c.Drive().Get("api")
package core
// DriveHandle holds a named transport resource.
//
// handle := &core.DriveHandle{Name: "homelab", Transport: "ssh://agent@10.69.69.165"}
// core.Println(handle.Transport)
type DriveHandle struct {
Name string
Transport string
Options Options
}
// Drive manages named transport handles. Embeds Registry[*DriveHandle].
//
// c := core.New()
// c.Drive().New(core.NewOptions(
// core.Option{Key: "name", Value: "homelab"},
// core.Option{Key: "transport", Value: "ssh://agent@10.69.69.165"},
// ))
//
// A view bound to one handle answers for it directly (see On / c.Drive(name)):
//
// if c.Drive("homelab").Exists() { transport := c.Drive("homelab").Transport() }
type Drive struct {
*Registry[*DriveHandle]
bound string // non-empty on a handle-bound view — see On / c.Drive(name)
}
// On returns a view of Drive bound to a named handle. The view shares
// the handle registry with the parent; only the binding is new. Sugar
// reached via c.Drive(name).
//
// forge := c.Drive().On("forge")
// if forge.Exists() { ... }
func (d *Drive) On(name string) *Drive {
return &Drive{Registry: d.Registry, bound: name}
}
// Exists reports whether the bound handle is registered — the
// capability check for named transports.
//
// if c.Drive("forge").Exists() { ... }
func (d *Drive) Exists() bool {
return d.bound != "" && d.Has(d.bound)
}
// Handle returns the bound transport handle. Fails with operation
// "drive.Handle" when the view has no binding or the handle is missing.
//
// r := c.Drive("forge").Handle()
// if r.OK { handle := r.Value.(*core.DriveHandle) }
func (d *Drive) Handle() Result {
if d.bound == "" {
return Result{E("drive.Handle", "no handle bound — use c.Drive(name)", nil), false}
}
r := d.Get(d.bound)
if !r.OK {
return Result{E("drive.Handle", Concat("handle not found: ", d.bound), nil), false}
}
return r
}
// Transport returns the bound handle's transport URL, "" when the
// binding is absent — the Options-getter contract for named transports.
//
// url := c.Drive("homelab").Transport() // "ssh://agent@10.69.69.165"
func (d *Drive) Transport() string {
r := d.Handle()
if !r.OK {
return ""
}
return r.Value.(*DriveHandle).Transport
}
// New registers a transport handle.
//
// c.Drive().New(core.NewOptions(
// core.Option{Key: "name", Value: "api"},
// core.Option{Key: "transport", Value: "https://api.lthn.ai"},
// ))
func (d *Drive) New(opts Options) Result {
name := opts.String("name")
if name == "" {
return Result{}
}
handle := &DriveHandle{
Name: name,
Transport: opts.String("transport"),
Options: opts,
}
d.Set(name, handle)
return Result{handle, true}
}