-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
171 lines (158 loc) · 3.39 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/cmd/goic/internal/cmd"
"github.com/aviate-labs/agent-go/gen"
"github.com/aviate-labs/agent-go/principal"
"os"
)
var root = cmd.NewCommandFork(
"goic",
"`goic` is a CLI tool for creating a Go agent.",
cmd.NewCommand(
"version",
"Print the version of `goic`.",
[]string{},
[]cmd.CommandOption{},
func(args []string, options map[string]string) error {
fmt.Println("0.0.1")
return nil
},
),
cmd.NewCommand(
"fetch",
"Fetch a DID from a canister ID.",
[]string{"id"},
[]cmd.CommandOption{
{
Name: "output",
HasValue: true,
},
},
func(args []string, options map[string]string) error {
id := args[0]
canisterId, err := principal.Decode(id)
if err != nil {
return err
}
rawDID, err := fetchDID(canisterId)
if err != nil {
return err
}
var path string
if p, ok := options["output"]; ok {
path = p
}
if path != "" {
return os.WriteFile(path, rawDID, os.ModePerm)
}
fmt.Println(string(rawDID))
return nil
},
),
cmd.NewCommandFork(
"generate",
"Generate a new Agent from...",
cmd.NewCommand(
"did",
"Generate a new Agent from a DID.",
[]string{"path", "name"},
[]cmd.CommandOption{
{
Name: "output",
HasValue: true,
},
{
Name: "packageName",
HasValue: true,
},
},
func(args []string, options map[string]string) error {
inputPath := args[0]
rawDID, err := os.ReadFile(inputPath)
if err != nil {
return err
}
var path string
if p, ok := options["output"]; ok {
path = p
}
canisterName := args[1]
packageName := canisterName
if p, ok := options["packageName"]; ok {
packageName = p
}
return writeDID(canisterName, packageName, path, rawDID)
},
),
cmd.NewCommand(
"remote",
"Generate a new Agent from a canister ID.",
[]string{"id", "canisterName"},
[]cmd.CommandOption{
{
Name: "output",
HasValue: true,
},
{
Name: "packageName",
HasValue: true,
},
},
func(args []string, options map[string]string) error {
id := args[0]
canisterId, err := principal.Decode(id)
if err != nil {
return err
}
rawDID, err := fetchDID(canisterId)
if err != nil {
return err
}
var path string
if p, ok := options["output"]; ok {
path = p
}
canisterName := args[1]
packageName := canisterName
if p, ok := options["packageName"]; ok {
packageName = p
}
return writeDID(canisterName, packageName, path, rawDID)
},
),
),
)
func fetchDID(canisterId principal.Principal) ([]byte, error) {
a, err := agent.New(agent.Config{})
if err != nil {
return nil, err
}
var did string
if err := a.Query(canisterId, "__get_candid_interface_tmp_hack", nil, []any{&did}); err != nil {
return nil, err
}
return []byte(did), nil
}
func main() {
if err := root.Call(os.Args[1:]...); err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
}
func writeDID(canisterName, packageName, outputPath string, rawDID []byte) error {
g, err := gen.NewGenerator("", canisterName, packageName, rawDID)
if err != nil {
return err
}
raw, err := g.Generate()
if err != nil {
return err
}
if outputPath != "" {
return os.WriteFile(outputPath, raw, os.ModePerm)
}
fmt.Println(string(raw))
return nil
}