Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions check-headers.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh

files=$(find . -name \*.go -type f -print0 | xargs -0 egrep -L '(Licensed under the Apache License)|(Code generated from|by)')
files=$(find . -name \*.go -type f -print0 | xargs -0 grep -L -E '(Licensed under the Apache License)|(Code generated (from|by))')
if [ -n "$files" ]; then
echo "Missing license header in:"
echo "$files"
exit 1
fi
fi
55 changes: 55 additions & 0 deletions flow/arguments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Flow CLI
*
* Copyright 2019-2020 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cli

import (
"encoding/json"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
)

type CadenceArgument struct {
Value cadence.Value
}

func (v CadenceArgument) MarshalJSON() ([]byte, error) {
return jsoncdc.Encode(v.Value)
}
func (v *CadenceArgument) UnmarshalJSON(b []byte) (err error) {
v.Value, err = jsoncdc.Decode(b)
if err != nil {
return err
}
return nil
}
func ParseArguments(input string) ([]cadence.Value, error) {
var args []CadenceArgument
b := []byte(input)
err := json.Unmarshal(b, &args)

if err != nil {
return nil, err
}

cadenceArgs := make([]cadence.Value, len(args))
for i, arg := range args {
cadenceArgs[i] = arg.Value
}
return cadenceArgs, nil
}
25 changes: 19 additions & 6 deletions flow/scripts/execute/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package execute

import (
"github.com/onflow/cadence"
"io/ioutil"
"log"

Expand All @@ -29,25 +30,37 @@ import (
)

type Config struct {
Args string `default:"" flag:"args" info:"arguments in JSON-Cadence format"`
Code string `flag:"code,c" info:"path to Cadence file"`
Host string `flag:"host" info:"Flow Access API host address"`
}

var conf Config

var Cmd = &cobra.Command{
Use: "execute <script.cdc>",
Short: "Execute a script",
Args: cobra.ExactArgs(1),
Use: "execute",
Short: "Execute a script",
Example: `flow scripts execute --code=script.cdc --args="[{\"type\": \"String\", \"value\": \"Hello, Cadence\"}]"`,
Run: func(cmd *cobra.Command, args []string) {
code, err := ioutil.ReadFile(args[0])
code, err := ioutil.ReadFile(conf.Code)
if err != nil {
cli.Exitf(1, "Failed to read script from %s", args[0])
cli.Exitf(1, "Failed to read script from %s", conf.Code)
}
projectConf := new(cli.Config)
if conf.Host == "" {
projectConf = cli.LoadConfig()
}
cli.ExecuteScript(projectConf.HostWithOverride(conf.Host), code)

// Arguments
var scriptArguments []cadence.Value
if conf.Args != "" {
scriptArguments, err = cli.ParseArguments(conf.Args)
if err != nil {
cli.Exitf(1, "Invalid arguments passed: %s", conf.Args)
}
}

cli.ExecuteScript(projectConf.HostWithOverride(conf.Host), code, scriptArguments...)
},
}

Expand Down
27 changes: 23 additions & 4 deletions flow/transactions/send/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ import (
)

type Config struct {
Signer string `default:"service" flag:"signer,s"`
Args string `default:"" flag:"args" info:"arguments in JSON-Cadence format"`
Code string `flag:"code,c" info:"path to Cadence file"`
Host string `flag:"host" info:"Flow Access API host address"`
Signer string `default:"service" flag:"signer,s"`
Results bool `default:"false" flag:"results" info:"Display the results of the transaction"`
}

var conf Config

var Cmd = &cobra.Command{
Use: "send",
Short: "Send a transaction",
Use: "send",
Short: "Send a transaction",
Example: `flow transactions send --code=tx.cdc --args="[{\"type\": \"String\", \"value\": \"Hello, Cadence\"}]"`,
Run: func(cmd *cobra.Command, args []string) {
projectConf := cli.LoadConfig()

Expand All @@ -59,10 +61,27 @@ var Cmd = &cobra.Command{
}
}

tx := flow.NewTransaction().
tx := flow.
NewTransaction().
SetScript(code).
AddAuthorizer(signerAccount.Address)

// Arguments
if conf.Args != "" {
transactionArguments, err := cli.ParseArguments(conf.Args)
if err != nil {
cli.Exitf(1, "Invalid arguments passed: %s", conf.Args)
}

for _, arg := range transactionArguments {
err := tx.AddArgument(arg)

if err != nil {
cli.Exitf(1, "Failed to add %s argument to a transaction ", conf.Code)
}
}
}

cli.SendTransaction(projectConf.HostWithOverride(conf.Host), signerAccount, tx, conf.Results)
},
}
Expand Down