From f5e900f0308f6f7b4e0d9f74a5bd409f69f868dd Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Fri, 29 Sep 2023 11:08:26 -0500 Subject: [PATCH 01/12] add support for js code generation for flix --- internal/super/flix.go | 50 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index 4c53d261d..b825a8b6f 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -26,13 +26,12 @@ import ( "github.com/onflow/flixkit-go" + "github.com/onflow/flow-cli/flowkit" "github.com/onflow/flow-cli/flowkit/output" "github.com/onflow/flow-cli/internal/command" "github.com/onflow/flow-cli/internal/scripts" "github.com/onflow/flow-cli/internal/transactions" - "github.com/onflow/flow-cli/flowkit" - "github.com/spf13/cobra" ) @@ -47,8 +46,15 @@ type flixFlags struct { Include []string `default:"" flag:"include" info:"Fields to include in the output"` Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"` GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"` + Generate string `default:"" flag:"generate" info:"generate code for the given language"` + Save string `default:"" flag:"output" info:"output file for generated code"` } +type flixResult struct { + output string + save string +} + var flags = flixFlags{} var FlixCommand = &command.Command{ @@ -106,6 +112,7 @@ func execute( ctx := context.Background() var template *flixkit.FlowInteractionTemplate flixQuery := args[0] + isLocal := false switch getType(flixQuery) { case flixId: @@ -121,6 +128,7 @@ func execute( } case flixPath: + isLocal = true file, err := os.ReadFile(flixQuery) if err != nil { return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err) @@ -134,6 +142,22 @@ func execute( return nil, fmt.Errorf("invalid flix query type: %s", flixQuery) } + if flags.Generate != "" { + jsGen := flixkit.JavascriptGenerator{} + out, err := jsGen.Generate(template, flixQuery, isLocal) + + if flags.Save != "" { + err = os.WriteFile(flags.Save, []byte(out), 0644) + if err != nil { + return nil, fmt.Errorf("could not write to file %s: %w", flags.Save, err) + } + } + return &flixResult{ + save: flags.Save, + output: out, + }, err + } + cadenceWithImportsReplaced, err := template.GetAndReplaceCadenceImports(flow.Network().Name) if err != nil { logger.Error("could not replace imports") @@ -161,3 +185,25 @@ func execute( } return transactions.SendTransaction([]byte(cadenceWithImportsReplaced), args[1:], "", flow, state, transactionFlags) } + +func (fr *flixResult) JSON() any { + result := make(map[string]any) + result["output"] = fr.output + result["save"] = fr.save + return result +} + +func (fr *flixResult) String() string { + return isSaved(fr) +} + +func (fr *flixResult) Oneliner() string { + return isSaved(fr) +} + +func isSaved(fr *flixResult) string { + if fr.save != "" { + return fmt.Sprintf("Generated code saved to %s", fr.save) + } + return fr.output +} \ No newline at end of file From 23e2c33c3735e8ff86b0332231ec5e0d083a17cb Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Wed, 4 Oct 2023 14:19:44 -0500 Subject: [PATCH 02/12] verify binding value is correct, change flags name to binding --- internal/super/flix.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index b825a8b6f..93c62f61d 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -25,6 +25,7 @@ import ( "os" "github.com/onflow/flixkit-go" + "github.com/onflow/flixkit-go/bindings" "github.com/onflow/flow-cli/flowkit" "github.com/onflow/flow-cli/flowkit/output" @@ -46,14 +47,14 @@ type flixFlags struct { Include []string `default:"" flag:"include" info:"Fields to include in the output"` Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"` GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"` - Generate string `default:"" flag:"generate" info:"generate code for the given language"` - Save string `default:"" flag:"output" info:"output file for generated code"` + Bindings string `default:"" flag:"bindings" info:"generate binding code for the given language, fcl-js' is supported language"` + Save string `default:"" flag:"output" info:"output file for generated binding code"` } type flixResult struct { output string - save string -} + save string +} var flags = flixFlags{} @@ -62,7 +63,7 @@ var FlixCommand = &command.Command{ Use: "flix ", Short: "Execute FLIX template with a given id, name, or local filename", Example: "flow flix multiply 2 3", - Args: cobra.ArbitraryArgs, + Args: cobra.MinimumNArgs(1), GroupID: "super", }, Flags: &flags, @@ -142,9 +143,13 @@ func execute( return nil, fmt.Errorf("invalid flix query type: %s", flixQuery) } - if flags.Generate != "" { - jsGen := flixkit.JavascriptGenerator{} - out, err := jsGen.Generate(template, flixQuery, isLocal) + if flags.Bindings != "" { + if flags.Bindings != "fcl-js" { + return nil, fmt.Errorf("binding not supported %s", flags.Bindings) + } + + fclJsGen := bindings.FclJSGenerator{} + out, err := fclJsGen.Generate(template, flixQuery, isLocal) if flags.Save != "" { err = os.WriteFile(flags.Save, []byte(out), 0644) @@ -153,10 +158,10 @@ func execute( } } return &flixResult{ - save: flags.Save, + save: flags.Save, output: out, }, err - } + } cadenceWithImportsReplaced, err := template.GetAndReplaceCadenceImports(flow.Network().Name) if err != nil { @@ -206,4 +211,4 @@ func isSaved(fr *flixResult) string { return fmt.Sprintf("Generated code saved to %s", fr.save) } return fr.output -} \ No newline at end of file +} From f4510f81766ce2c58e676f3c802b5aac584b89fa Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Wed, 18 Oct 2023 12:07:41 -0500 Subject: [PATCH 03/12] use new helper function to import fcl js code generator --- internal/super/flix.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index 93c62f61d..c11b5522f 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -148,7 +148,8 @@ func execute( return nil, fmt.Errorf("binding not supported %s", flags.Bindings) } - fclJsGen := bindings.FclJSGenerator{} + fclJsGen := bindings.NewFclJSGenerator() + out, err := fclJsGen.Generate(template, flixQuery, isLocal) if flags.Save != "" { From 2ffb69c8bb13d69c810700140902dad0a354500a Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Tue, 24 Oct 2023 10:43:18 -0500 Subject: [PATCH 04/12] updated flixkit-go reference to new v0.2.0 --- go.mod | 5 +++-- go.sum | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index b68e033c5..e8a8a7171 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/onflow/cadence-tools/languageserver v0.32.0 github.com/onflow/cadence-tools/test v0.10.0 github.com/onflow/fcl-dev-wallet v0.7.2 - github.com/onflow/flixkit-go v0.1.0 + github.com/onflow/flixkit-go v0.2.0 github.com/onflow/flow-cli/flowkit v1.3.5-0.20230808220356-6a2bfeb10552 github.com/onflow/flow-core-contracts/lib/go/templates v1.2.3 github.com/onflow/flow-emulator v0.54.1 @@ -182,6 +182,7 @@ require ( github.com/spf13/cast v1.5.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stoewer/go-strcase v1.3.0 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c // indirect @@ -210,7 +211,7 @@ require ( golang.org/x/net v0.12.0 // indirect golang.org/x/oauth2 v0.10.0 // indirect golang.org/x/sync v0.3.0 // indirect - golang.org/x/sys v0.10.0 // indirect + golang.org/x/sys v0.12.0 // indirect golang.org/x/term v0.10.0 // indirect golang.org/x/text v0.11.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect diff --git a/go.sum b/go.sum index 860734b64..732ec6ad0 100644 --- a/go.sum +++ b/go.sum @@ -261,6 +261,7 @@ github.com/ethereum/go-ethereum v1.10.22 h1:HbEgsDo1YTGIf4KB/NNpn+XH+PiNJXUZ9ksR github.com/ethereum/go-ethereum v1.10.22/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg= github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= @@ -518,6 +519,9 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/hexops/autogold/v2 v2.2.1 h1:JPUXuZQGkcQMv7eeDXuNMovjfoRYaa0yVcm+F3voaGY= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/valast v1.4.4 h1:rETyycw+/L2ZVJHHNxEBgh8KUn+87WugH9MxcEv9PGs= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag= @@ -758,6 +762,7 @@ github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= @@ -780,8 +785,8 @@ github.com/onflow/cadence-tools/test v0.10.0 h1:tcNjOFXl7ZuuvgQ3uS36ENd8l3RkSBv4 github.com/onflow/cadence-tools/test v0.10.0/go.mod h1:eIvZOzFdi4JR2x6ekQfN8veQy04ZlZYbeeiQF/oZ0zM= github.com/onflow/fcl-dev-wallet v0.7.2 h1:ZwhpzDakcZn9rHiIr52LwkdPh1MpUPbxA/PwCVaZ2SA= github.com/onflow/fcl-dev-wallet v0.7.2/go.mod h1:kc42jkiuoPJmxMRFjfbRO9XvnR/3XLheaOerxVMDTiw= -github.com/onflow/flixkit-go v0.1.0 h1:3nH+1z+D+0YlmEJk9zYtvD8p4eUl2wJtiV6ZCgM7vYE= -github.com/onflow/flixkit-go v0.1.0/go.mod h1:gPffHQ6jDyuNtLG6W9C6FGvDZmcOb9CkvsJYKY0Opc4= +github.com/onflow/flixkit-go v0.2.0 h1:yI7jksFPkLoQkFZ31oYrq3xPaZBn+qqLslpLM7TttSo= +github.com/onflow/flixkit-go v0.2.0/go.mod h1:i3EYZbxDP+k0n4nt1fzlwBFMENdNA82w+ZlQ9knfunQ= github.com/onflow/flow-archive v1.3.4-0.20230503192214-9e81e82d4dcc h1:C4ZniFeOv+pHlDLJdGc/4e3NklSjVuvaXKN47980gnY= github.com/onflow/flow-archive v1.3.4-0.20230503192214-9e81e82d4dcc/go.mod h1:UPsvKk/37Atosif4wlBl3gsLbGJyGpdXYpXDsWtMVBE= github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d h1:B7PdhdUNkve5MVrekWDuQf84XsGBxNZ/D3x+QQ8XeVs= @@ -982,6 +987,8 @@ github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1Fof github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8OR4w3TdeIHIh1g6EMY5p0gVNOovcWC+1vpc7naMuAw= github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU= +github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs= +github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= @@ -1183,7 +1190,7 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= -golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1364,8 +1371,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -1459,7 +1466,7 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= -golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -1696,6 +1703,7 @@ modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= modernc.org/sqlite v1.21.1 h1:GyDFqNnESLOhwwDRaHGdp2jKLDzpyT/rNLglX3ZkMSU= modernc.org/sqlite v1.21.1/go.mod h1:XwQ0wZPIh1iKb5mkvCJ3szzbhk+tykC8ZWqTRTgYRwI= +mvdan.cc/gofumpt v0.5.0 h1:0EQ+Z56k8tXjj/6TQD25BFNKQXpCvT0rnansIc7Ug5E= nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= From 9969b9010838cf5b880937a45f47bc3a4c1c368d Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Tue, 24 Oct 2023 20:38:11 -0500 Subject: [PATCH 05/12] organize flix commands, there are going to be a few so organizing now --- cmd/flow/main.go | 2 +- internal/super/flix.go | 116 ++++++++++++++++++++++++++++++----------- 2 files changed, 86 insertions(+), 32 deletions(-) diff --git a/cmd/flow/main.go b/cmd/flow/main.go index 6e3405b0e..ce35fd9ae 100644 --- a/cmd/flow/main.go +++ b/cmd/flow/main.go @@ -68,7 +68,6 @@ func main() { // super commands super.SetupCommand.AddToParent(cmd) super.DevCommand.AddToParent(cmd) - super.FlixCommand.AddToParent(cmd) // structured commands cmd.AddCommand(settings.Cmd) @@ -86,6 +85,7 @@ func main() { cmd.AddCommand(config.Cmd) cmd.AddCommand(signatures.Cmd) cmd.AddCommand(snapshot.Cmd) + cmd.AddCommand(super.FlixCmd) command.InitFlags(cmd) cmd.AddGroup(&cobra.Group{ diff --git a/internal/super/flix.go b/internal/super/flix.go index c11b5522f..f11065b2b 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -47,7 +47,6 @@ type flixFlags struct { Include []string `default:"" flag:"include" info:"Fields to include in the output"` Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"` GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"` - Bindings string `default:"" flag:"bindings" info:"generate binding code for the given language, fcl-js' is supported language"` Save string `default:"" flag:"output" info:"output file for generated binding code"` } @@ -57,17 +56,38 @@ type flixResult struct { } var flags = flixFlags{} +var FlixCmd = &cobra.Command{ + Use: "flix", + Short: "execute , bindings , generate ", + TraverseChildren: true, + GroupID: "tools", +} -var FlixCommand = &command.Command{ +var executeCommand = &command.Command{ Cmd: &cobra.Command{ - Use: "flix ", - Short: "Execute FLIX template with a given id, name, or local filename", - Example: "flow flix multiply 2 3", + Use: "execute ", + Short: "execute FLIX template with a given id, name, or local filename", + Example: "flow flix execute multiply 2 3", Args: cobra.MinimumNArgs(1), - GroupID: "super", }, Flags: &flags, - RunS: execute, + RunS: executeCmd, +} + +var bindingCommand = &command.Command{ + Cmd: &cobra.Command{ + Use: "bindings ", + Short: "generate binding file for FLIX template fcl-js is default", + Example: "flow flix bindings multiply.template.json", + Args: cobra.MinimumNArgs(1), + }, + Flags: &flags, + RunS: bindingsCmd, +} + +func init() { + executeCommand.AddToParent(FlixCmd) + bindingCommand.AddToParent(FlixCmd) } type flixQueryTypes string @@ -102,7 +122,7 @@ func getType(s string) flixQueryTypes { } } -func execute( +func executeCmd( args []string, _ command.GlobalFlags, logger output.Logger, @@ -113,7 +133,6 @@ func execute( ctx := context.Background() var template *flixkit.FlowInteractionTemplate flixQuery := args[0] - isLocal := false switch getType(flixQuery) { case flixId: @@ -129,7 +148,6 @@ func execute( } case flixPath: - isLocal = true file, err := os.ReadFile(flixQuery) if err != nil { return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err) @@ -143,27 +161,6 @@ func execute( return nil, fmt.Errorf("invalid flix query type: %s", flixQuery) } - if flags.Bindings != "" { - if flags.Bindings != "fcl-js" { - return nil, fmt.Errorf("binding not supported %s", flags.Bindings) - } - - fclJsGen := bindings.NewFclJSGenerator() - - out, err := fclJsGen.Generate(template, flixQuery, isLocal) - - if flags.Save != "" { - err = os.WriteFile(flags.Save, []byte(out), 0644) - if err != nil { - return nil, fmt.Errorf("could not write to file %s: %w", flags.Save, err) - } - } - return &flixResult{ - save: flags.Save, - output: out, - }, err - } - cadenceWithImportsReplaced, err := template.GetAndReplaceCadenceImports(flow.Network().Name) if err != nil { logger.Error("could not replace imports") @@ -192,6 +189,63 @@ func execute( return transactions.SendTransaction([]byte(cadenceWithImportsReplaced), args[1:], "", flow, state, transactionFlags) } +func bindingsCmd( + args []string, + _ command.GlobalFlags, + logger output.Logger, + flow flowkit.Services, + state *flowkit.State, +) (result command.Result, err error) { + flixService := flixkit.NewFlixService(&flixkit.Config{}) + ctx := context.Background() + var template *flixkit.FlowInteractionTemplate + flixQuery := args[0] + isLocal := false + + switch getType(flixQuery) { + case flixId: + template, err = flixService.GetFlixByID(ctx, flixQuery) + if err != nil { + return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err) + } + + case flixName: + template, err = flixService.GetFlix(ctx, flixQuery) + if err != nil { + return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err) + } + + case flixPath: + isLocal = true + file, err := os.ReadFile(flixQuery) + if err != nil { + return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err) + } + template, err = flixkit.ParseFlix(string(file)) + if err != nil { + return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err) + } + + default: + return nil, fmt.Errorf("invalid flix query type: %s", flixQuery) + } + + fclJsGen := bindings.NewFclJSGenerator() + + out, err := fclJsGen.Generate(template, flixQuery, isLocal) + + if flags.Save != "" { + err = os.WriteFile(flags.Save, []byte(out), 0644) + if err != nil { + return nil, fmt.Errorf("could not write to file %s: %w", flags.Save, err) + } + } + return &flixResult{ + save: flags.Save, + output: out, + }, err +} + func (fr *flixResult) JSON() any { result := make(map[string]any) result["output"] = fr.output From e153a9a7b7ffea9c67f21f41e99f7a4de879bd6c Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Tue, 24 Oct 2023 20:56:54 -0500 Subject: [PATCH 06/12] add transfer-tokens template example that is named on server --- internal/super/flix.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index f11065b2b..8edffb500 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -47,11 +47,11 @@ type flixFlags struct { Include []string `default:"" flag:"include" info:"Fields to include in the output"` Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"` GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"` - Save string `default:"" flag:"output" info:"output file for generated binding code"` + Save string `default:"" flag:"save" info:"save generated code to file"` } type flixResult struct { - output string + result string save string } @@ -67,7 +67,7 @@ var executeCommand = &command.Command{ Cmd: &cobra.Command{ Use: "execute ", Short: "execute FLIX template with a given id, name, or local filename", - Example: "flow flix execute multiply 2 3", + Example: "flow flix execute transfer-flow 1 0x123456789", Args: cobra.MinimumNArgs(1), }, Flags: &flags, @@ -242,13 +242,13 @@ func bindingsCmd( } return &flixResult{ save: flags.Save, - output: out, + result: out, }, err } func (fr *flixResult) JSON() any { result := make(map[string]any) - result["output"] = fr.output + result["result"] = fr.result result["save"] = fr.save return result } @@ -265,5 +265,5 @@ func isSaved(fr *flixResult) string { if fr.save != "" { return fmt.Sprintf("Generated code saved to %s", fr.save) } - return fr.output + return fr.result } From a8e3a5ae04c9454358dce203df7ea9373c5760d4 Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Tue, 24 Oct 2023 21:03:51 -0500 Subject: [PATCH 07/12] removed generate, that is a future verb --- internal/super/flix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index 8edffb500..d5115f015 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -58,7 +58,7 @@ type flixResult struct { var flags = flixFlags{} var FlixCmd = &cobra.Command{ Use: "flix", - Short: "execute , bindings , generate ", + Short: "execute , bindings ", TraverseChildren: true, GroupID: "tools", } From aad19cb72fea714b6b7eecfeafc6a9424aaed093 Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Tue, 24 Oct 2023 21:04:52 -0500 Subject: [PATCH 08/12] update to simplify base help message for flix --- internal/super/flix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index d5115f015..091a103cf 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -58,7 +58,7 @@ type flixResult struct { var flags = flixFlags{} var FlixCmd = &cobra.Command{ Use: "flix", - Short: "execute , bindings ", + Short: "execute, bindings", TraverseChildren: true, GroupID: "tools", } From 9c2ad0a0624749e6c115788cb8542209c0eeb34f Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Tue, 24 Oct 2023 21:27:05 -0500 Subject: [PATCH 09/12] dry up code --- internal/super/flix.go | 99 ++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 56 deletions(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index 091a103cf..1f8e7a585 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -130,35 +130,10 @@ func executeCmd( state *flowkit.State, ) (result command.Result, err error) { flixService := flixkit.NewFlixService(&flixkit.Config{}) - ctx := context.Background() - var template *flixkit.FlowInteractionTemplate flixQuery := args[0] - - switch getType(flixQuery) { - case flixId: - template, err = flixService.GetFlixByID(ctx, flixQuery) - if err != nil { - return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err) - } - - case flixName: - template, err = flixService.GetFlix(ctx, flixQuery) - if err != nil { - return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err) - } - - case flixPath: - file, err := os.ReadFile(flixQuery) - if err != nil { - return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err) - } - template, err = flixkit.ParseFlix(string(file)) - if err != nil { - return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err) - } - - default: - return nil, fmt.Errorf("invalid flix query type: %s", flixQuery) + template, err := getTemplate(state, flixService, flixQuery) + if err != nil { + return nil, err } cadenceWithImportsReplaced, err := template.GetAndReplaceCadenceImports(flow.Network().Name) @@ -197,37 +172,16 @@ func bindingsCmd( state *flowkit.State, ) (result command.Result, err error) { flixService := flixkit.NewFlixService(&flixkit.Config{}) - ctx := context.Background() - var template *flixkit.FlowInteractionTemplate flixQuery := args[0] - isLocal := false - - switch getType(flixQuery) { - case flixId: - template, err = flixService.GetFlixByID(ctx, flixQuery) - if err != nil { - return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err) - } - case flixName: - template, err = flixService.GetFlix(ctx, flixQuery) - if err != nil { - return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err) - } + template, err := getTemplate(state, flixService, flixQuery) + if err != nil { + return nil, err + } - case flixPath: + isLocal := false + if getType(flixQuery) == flixPath { isLocal = true - file, err := os.ReadFile(flixQuery) - if err != nil { - return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err) - } - template, err = flixkit.ParseFlix(string(file)) - if err != nil { - return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err) - } - - default: - return nil, fmt.Errorf("invalid flix query type: %s", flixQuery) } fclJsGen := bindings.NewFclJSGenerator() @@ -235,7 +189,7 @@ func bindingsCmd( out, err := fclJsGen.Generate(template, flixQuery, isLocal) if flags.Save != "" { - err = os.WriteFile(flags.Save, []byte(out), 0644) + err = state.ReaderWriter().WriteFile(flags.Save, []byte(out), 0644) if err != nil { return nil, fmt.Errorf("could not write to file %s: %w", flags.Save, err) } @@ -267,3 +221,36 @@ func isSaved(fr *flixResult) string { } return fr.result } + +func getTemplate(state *flowkit.State, flixService flixkit.FlixService, flixQuery string) (*flixkit.FlowInteractionTemplate, error) { + var template *flixkit.FlowInteractionTemplate + var err error + ctx := context.Background() + switch getType(flixQuery) { + case flixId: + template, err = flixService.GetFlixByID(ctx, flixQuery) + if err != nil { + return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err) + } + + case flixName: + template, err = flixService.GetFlix(ctx, flixQuery) + if err != nil { + return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err) + } + + case flixPath: + file, err := state.ReadFile(flixQuery) + if err != nil { + return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err) + } + template, err = flixkit.ParseFlix(string(file)) + if err != nil { + return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err) + } + + default: + return nil, fmt.Errorf("invalid flix query type: %s", flixQuery) + } + return template, nil +} From 411cc71aed15bcf9831a54f2f5ebb6a53573d041 Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Wed, 25 Oct 2023 11:44:55 -0500 Subject: [PATCH 10/12] reove local save flag and have more descriptive json output --- internal/super/flix.go | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index 1f8e7a585..ebbc33124 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -47,12 +47,11 @@ type flixFlags struct { Include []string `default:"" flag:"include" info:"Fields to include in the output"` Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"` GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"` - Save string `default:"" flag:"save" info:"save generated code to file"` } type flixResult struct { - result string - save string + flixQuery string + result string } var flags = flixFlags{} @@ -188,37 +187,24 @@ func bindingsCmd( out, err := fclJsGen.Generate(template, flixQuery, isLocal) - if flags.Save != "" { - err = state.ReaderWriter().WriteFile(flags.Save, []byte(out), 0644) - if err != nil { - return nil, fmt.Errorf("could not write to file %s: %w", flags.Save, err) - } - } return &flixResult{ - save: flags.Save, - result: out, + flixQuery: flixQuery, + result: out, }, err } func (fr *flixResult) JSON() any { result := make(map[string]any) + result["flixQuery"] = fr.flixQuery result["result"] = fr.result - result["save"] = fr.save return result } func (fr *flixResult) String() string { - return isSaved(fr) + return fr.result } func (fr *flixResult) Oneliner() string { - return isSaved(fr) -} - -func isSaved(fr *flixResult) string { - if fr.save != "" { - return fmt.Sprintf("Generated code saved to %s", fr.save) - } return fr.result } From 91863a4d796ed2fdfd8c1c24b996c9d610d8042a Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Wed, 25 Oct 2023 11:57:19 -0500 Subject: [PATCH 11/12] update go sum --- go.sum | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.sum b/go.sum index bc94135b4..d10c8697a 100644 --- a/go.sum +++ b/go.sum @@ -295,6 +295,7 @@ github.com/ethereum/go-ethereum v1.10.22 h1:HbEgsDo1YTGIf4KB/NNpn+XH+PiNJXUZ9ksR github.com/ethereum/go-ethereum v1.10.22/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= @@ -870,6 +871,7 @@ github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= From 0009702e7a1554b2313300ed78012d522f35e615 Mon Sep 17 00:00:00 2001 From: Tom Haile Date: Fri, 27 Oct 2023 11:36:47 -0500 Subject: [PATCH 12/12] change command name from bindings to package --- internal/super/flix.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/super/flix.go b/internal/super/flix.go index ebbc33124..79ad9098d 100644 --- a/internal/super/flix.go +++ b/internal/super/flix.go @@ -57,7 +57,7 @@ type flixResult struct { var flags = flixFlags{} var FlixCmd = &cobra.Command{ Use: "flix", - Short: "execute, bindings", + Short: "execute, package", TraverseChildren: true, GroupID: "tools", } @@ -73,20 +73,20 @@ var executeCommand = &command.Command{ RunS: executeCmd, } -var bindingCommand = &command.Command{ +var packageCommand = &command.Command{ Cmd: &cobra.Command{ - Use: "bindings ", - Short: "generate binding file for FLIX template fcl-js is default", - Example: "flow flix bindings multiply.template.json", + Use: "package ", + Short: "package file for FLIX template fcl-js is default", + Example: "flow flix package multiply.template.json", Args: cobra.MinimumNArgs(1), }, Flags: &flags, - RunS: bindingsCmd, + RunS: packageCmd, } func init() { executeCommand.AddToParent(FlixCmd) - bindingCommand.AddToParent(FlixCmd) + packageCommand.AddToParent(FlixCmd) } type flixQueryTypes string @@ -163,7 +163,7 @@ func executeCmd( return transactions.SendTransaction([]byte(cadenceWithImportsReplaced), args[1:], "", flow, state, transactionFlags) } -func bindingsCmd( +func packageCmd( args []string, _ command.GlobalFlags, logger output.Logger,