This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
218 lines (196 loc) · 5.62 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2023 Blockwatch Data Inc.
// Author: alex@blockwatch.cc, abdul@blockwatch.cc
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"time"
"blockwatch.cc/tzgo/internal/compose"
"blockwatch.cc/tzgo/tezos"
_ "blockwatch.cc/tzgo/internal/compose/alpha"
_ "blockwatch.cc/tzgo/internal/compose/alpha/task"
)
var (
flags = flag.NewFlagSet(appName, flag.ContinueOnError)
runflags = flag.NewFlagSet("run", flag.ContinueOnError)
cloneflags = flag.NewFlagSet("clone", flag.ContinueOnError)
errExit = errors.New("exit")
errNoCmd = errors.New("unsupported command")
verbose bool
vtrace bool
vdebug bool
cmd string = "[cmd]"
// run config
fpath string
resume bool
rpcUrl string
// clone config
name string
mode compose.CloneMode
addr tezos.Address
numOpsAfterOrigination uint
version string
indexUrl string
outputPath string
)
func init() {
flags.Usage = func() {}
flags.BoolVar(&verbose, "v", true, "be verbose")
flags.BoolVar(&vdebug, "vv", false, "debug mode")
flags.BoolVar(&vtrace, "vvv", false, "trace mode")
runflags.Usage = func() {}
runflags.StringVar(&fpath, "f", "tzcompose.yaml", "configuration `file` or path")
runflags.StringVar(&fpath, "file", "tzcompose.yaml", "configuration `file` or path")
runflags.BoolVar(&resume, "resume", false, "continue pipeline execution")
runflags.StringVar(&rpcUrl, "rpc", "https://rpc.tzpro.io", "Tezos node RPC url")
cloneflags.Usage = func() {}
cloneflags.StringVar(&indexUrl, "index", "https://api.tzpro.io", "Tezos indexer url")
cloneflags.StringVar(&rpcUrl, "rpc", "https://rpc.tzpro.io", "Tezos node RPC url")
cloneflags.StringVar(&version, "version", "alpha", "compose engine version")
cloneflags.Var(&addr, "contract", "address of the contract to clone")
cloneflags.Var(&mode, "mode", "output mode for cloned micheline data (file, json, bin, url)")
cloneflags.StringVar(&name, "name", "contract", "project name")
cloneflags.StringVar(&outputPath, "out", "tzcompose.yaml", "output path for generated files")
cloneflags.UintVar(&numOpsAfterOrigination, "n", 0, "number of operations after origination")
}
func main() {
if err := parseFlags(); err != nil {
if err != errExit {
fmt.Println("Error:", err)
}
return
}
initLogging()
if err := run(); err != nil {
fmt.Println("Error:", err)
}
}
func run() error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
ectx := compose.NewContext(ctx)
ectx.WithLogger(taskLog).
WithUrl(rpcUrl).
WithApiKey(os.Getenv("TZCOMPOSE_API_KEY")).
WithBase(os.Getenv("TZCOMPOSE_BASE_KEY")).
WithResume(resume)
var err error
switch cmd {
case "version":
printVersion()
case "validate":
err = compose.Run(ectx, fpath, compose.RunModeValidate)
case "simulate":
err = compose.Run(ectx, fpath, compose.RunModeSimulate)
case "run":
err = compose.Run(ectx, fpath, compose.RunModeExecute)
case "clone":
err = compose.Clone(ectx, version, compose.CloneConfig{
Name: name,
Contract: addr,
IndexUrl: indexUrl,
NumOps: numOpsAfterOrigination,
Path: outputPath,
Mode: mode,
})
default:
err = errNoCmd
}
return err
}
func parseFlags() error {
if len(os.Args) < 2 {
printHelp()
return errExit
}
n := 1
if !strings.HasPrefix(os.Args[n], "-") {
cmd = os.Args[n]
n++
}
switch cmd {
case "validate", "simulate", "run", "clone", "version", "[cmd]":
// ok
default:
return errNoCmd
}
// parse global flags
err := flags.Parse(filterFlags(flags, os.Args[n:]))
if err != nil {
if err == flag.ErrHelp {
printHelp()
return errExit
}
}
// parse command flags
switch cmd {
case "validate", "simulate", "run":
err = runflags.Parse(filterFlags(runflags, os.Args[2:]))
case "clone":
err = cloneflags.Parse(filterFlags(cloneflags, os.Args[2:]))
}
if err != nil {
if err == flag.ErrHelp {
printHelp()
return errExit
}
return err
}
return nil
}
func filterFlags(set *flag.FlagSet, args []string) []string {
res := make([]string, 0)
var maybeCopyNext bool
for _, v := range args {
if strings.HasPrefix(v, "-") {
f := set.Lookup(v[1:])
if f == nil && v != "-h" {
maybeCopyNext = false
continue
}
maybeCopyNext = true
res = append(res, v)
} else if maybeCopyNext {
maybeCopyNext = false
res = append(res, v)
}
}
return res
}
func printHelp() {
fmt.Printf("(c) Copyright (c) %d Blockwatch Data Inc.\n", time.Now().Year())
fmt.Printf("Usage: %s %s [flags]\n", appName, cmd)
switch cmd {
case "validate", "simulate", "run":
fmt.Printf("\nEnv\n")
fmt.Println(" TZCOMPOSE_BASE_KEY private key for base account")
fmt.Println(" TZCOMPOSE_API_KEY API key for RPC and index calls (optional)")
fmt.Println("\nFlags")
runflags.PrintDefaults()
fmt.Println(" -h print help and exit")
flags.PrintDefaults()
case "clone":
fmt.Printf("\nEnv\n")
fmt.Println(" TZCOMPOSE_BASE_KEY private key for base account")
fmt.Println(" TZCOMPOSE_API_KEY API key for RPC and index calls (optional)")
fmt.Println("\nFlags")
cloneflags.PrintDefaults()
fmt.Println(" -h print help and exit")
flags.PrintDefaults()
default:
fmt.Printf("\nCommands\n")
fmt.Println(" clone Clone a contract and its transactions")
fmt.Println(" validate Check compose file syntax and parameters")
fmt.Println(" simulate Simulate compose file execution")
fmt.Println(" run Execute compose file(s)")
fmt.Println(" version Print version and exit")
fmt.Println("\nFlags")
fmt.Println(" -h print help and exit")
flags.PrintDefaults()
}
}