From d8510b9d90aeb48898132e7a0e658555df72f14c Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Mon, 17 May 2021 12:01:59 +0200 Subject: [PATCH 01/15] contains field exclude/include methods --- internal/command/result.go | 14 ++++++++++++++ pkg/flowcli/util/utilities.go | 24 +++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/internal/command/result.go b/internal/command/result.go index 994e7d462..067b3d221 100644 --- a/internal/command/result.go +++ b/internal/command/result.go @@ -18,8 +18,22 @@ package command +import ( + "github.com/onflow/flow-cli/pkg/flowcli/util" +) + type Result interface { String() string Oneliner() string JSON() interface{} } + +// FieldIncluded checks if field should be included in the result by checking if included flags contains field +func FieldIncluded(included []string, field string) bool { + return util.ContainsStringInsensitive(included, field) +} + +// FieldExcluded checks if field should be excluded from the result by checking the flags +func FieldExcluded(excluded []string, field string) bool { + return util.ContainsStringInsensitive(excluded, field) +} diff --git a/pkg/flowcli/util/utilities.go b/pkg/flowcli/util/utilities.go index 6831d5b49..c5047f956 100644 --- a/pkg/flowcli/util/utilities.go +++ b/pkg/flowcli/util/utilities.go @@ -22,6 +22,7 @@ import ( "bytes" "fmt" "io/ioutil" + "strings" "text/tabwriter" "github.com/fatih/color" @@ -80,8 +81,8 @@ func ConvertSigAndHashAlgo( return sigAlgo, hashAlgo, nil } -// StringContains returns true if the slice contains the given string. -func StringContains(s []string, e string) bool { +// ContainsString returns true if the slice contains the given string. +func ContainsString(s []string, e string) bool { for _, a := range s { if a == e { return true @@ -91,6 +92,16 @@ func StringContains(s []string, e string) bool { return false } +func ContainsStringInsensitive(haystack []string, needle string) bool { + for _, n := range haystack { + if strings.ToLower(n) == needle { + return true + } + } + + return false +} + // GetAddressNetwork returns the chain ID for an address. func GetAddressNetwork(address flow.Address) (flow.ChainID, error) { networks := []flow.ChainID{ @@ -111,15 +122,6 @@ func CreateTabWriter(b *bytes.Buffer) *tabwriter.Writer { return tabwriter.NewWriter(b, 0, 8, 1, '\t', tabwriter.AlignRight) } -func ContainsString(s []string, e string) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} - func ParseAddress(value string) (flow.Address, bool) { address := flow.HexToAddress(value) From 00ad67b38a79943eabf6a4d1cf0b9e532f503045 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Mon, 17 May 2021 12:02:43 +0200 Subject: [PATCH 02/15] transactions exclude include --- internal/transactions/transactions.go | 68 ++++++++++++++++++--------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/internal/transactions/transactions.go b/internal/transactions/transactions.go index d19575fc5..84e87f73d 100644 --- a/internal/transactions/transactions.go +++ b/internal/transactions/transactions.go @@ -23,6 +23,8 @@ import ( "encoding/json" "fmt" + "github.com/onflow/flow-cli/internal/command" + jsoncdc "github.com/onflow/cadence/encoding/json" "github.com/onflow/flow-go-sdk" "github.com/spf13/cobra" @@ -47,9 +49,11 @@ func init() { // TransactionResult represent result from all account commands type TransactionResult struct { - result *flow.TransactionResult - tx *flow.Transaction - code bool + result *flow.TransactionResult + tx *flow.Transaction + code bool + include []string + exclude []string } // JSON convert result to JSON @@ -117,21 +121,35 @@ func (r *TransactionResult) String() string { _, _ = fmt.Fprintf(writer, "\nNo Envelope Signatures\n") } + // todo add exclude flags + for i, e := range r.tx.PayloadSignatures { - _, _ = fmt.Fprintf(writer, "\nPayload Signature %v:\n", i) - _, _ = fmt.Fprintf(writer, " Address\t%s\n", e.Address) - _, _ = fmt.Fprintf(writer, " Signature\t%x\n", e.Signature) - _, _ = fmt.Fprintf(writer, " Key Index\t%d\n", e.KeyIndex) + if command.FieldIncluded(r.include, "signatures") { + _, _ = fmt.Fprintf(writer, "\nPayload Signature %v:\n", i) + _, _ = fmt.Fprintf(writer, " Address\t%s\n", e.Address) + _, _ = fmt.Fprintf(writer, " Signature\t%x\n", e.Signature) + _, _ = fmt.Fprintf(writer, " Key Index\t%d\n", e.KeyIndex) + } else { + _, _ = fmt.Fprintf(writer, "\nPayload Signature %v: %s", i, e.Address) + } } for i, e := range r.tx.EnvelopeSignatures { - _, _ = fmt.Fprintf(writer, "\nEnvelope Signature %v:\n", i) - _, _ = fmt.Fprintf(writer, " Address\t%s\n", e.Address) - _, _ = fmt.Fprintf(writer, " Signature\t%x\n", e.Signature) - _, _ = fmt.Fprintf(writer, " Key Index\t%d\n", e.KeyIndex) + if command.FieldIncluded(r.include, "signatures") { + _, _ = fmt.Fprintf(writer, "\nEnvelope Signature %v:\n", i) + _, _ = fmt.Fprintf(writer, " Address\t%s\n", e.Address) + _, _ = fmt.Fprintf(writer, " Signature\t%x\n", e.Signature) + _, _ = fmt.Fprintf(writer, " Key Index\t%d\n", e.KeyIndex) + } else { + _, _ = fmt.Fprintf(writer, "\nEnvelope Signature %v: %s", i, e.Address) + } } - if r.result != nil { + if !command.FieldIncluded(r.include, "signatures") { + _, _ = fmt.Fprintf(writer, "\nSignatures (minimized, use --include signatures)") + } + + if !command.FieldExcluded(r.exclude, "events") { e := events.EventResult{ Events: r.result.Events, } @@ -145,19 +163,27 @@ func (r *TransactionResult) String() string { } if r.tx.Script != nil { - if len(r.tx.Arguments) == 0 { - _, _ = fmt.Fprintf(writer, "\n\nArguments\tNo arguments\n") - } else { - _, _ = fmt.Fprintf(writer, "\n\nArguments (%d):\n", len(r.tx.Arguments)) - for i, argument := range r.tx.Arguments { - _, _ = fmt.Fprintf(writer, " - Argument %d: %s\n", i, argument) + if command.FieldIncluded(r.include, "code") { + if len(r.tx.Arguments) == 0 { + _, _ = fmt.Fprintf(writer, "\n\nArguments\tNo arguments\n") + } else { + _, _ = fmt.Fprintf(writer, "\n\nArguments (%d):\n", len(r.tx.Arguments)) + for i, argument := range r.tx.Arguments { + _, _ = fmt.Fprintf(writer, " - Argument %d: %s\n", i, argument) + } } - } - _, _ = fmt.Fprintf(writer, "\nCode\n\n%s\n", r.tx.Script) + _, _ = fmt.Fprintf(writer, "\nCode\n\n%s\n", r.tx.Script) + } else { + _, _ = fmt.Fprint(writer, "\n\nCode (hidden, use --include code)") + } } - _, _ = fmt.Fprintf(writer, "\n\nPayload:\n%x", r.tx.Encode()) + if command.FieldIncluded(r.include, "payload") { + _, _ = fmt.Fprintf(writer, "\n\nPayload:\n%x", r.tx.Encode()) + } else { + _, _ = fmt.Fprint(writer, "\n\nPayload (hidden, use --include payload)") + } _ = writer.Flush() return b.String() From be91bcd0e59e24c4b5b988ee2a0853dcfdfe43c5 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Mon, 17 May 2021 12:02:57 +0200 Subject: [PATCH 03/15] refactor name --- pkg/flowcli/project/project.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/flowcli/project/project.go b/pkg/flowcli/project/project.go index e153974f7..e89d43146 100644 --- a/pkg/flowcli/project/project.go +++ b/pkg/flowcli/project/project.go @@ -219,7 +219,7 @@ func (p *Project) AccountNamesForNetwork(network string) []string { for _, account := range p.accounts { if len(p.conf.Deployments.GetByAccountAndNetwork(account.name, network)) > 0 { - if !util.StringContains(names, account.name) { + if !util.ContainsString(names, account.name) { names = append(names, account.name) } } From 76ed0e7d1bf3c5e6bd747458738d49efe1c41dc8 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Mon, 17 May 2021 12:08:24 +0200 Subject: [PATCH 04/15] add exclude include flags to tx --- internal/transactions/get.go | 18 +++++++++++++----- internal/transactions/send-signed.go | 8 ++++++-- internal/transactions/send.go | 8 ++++++-- internal/transactions/transactions.go | 4 +--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/internal/transactions/get.go b/internal/transactions/get.go index 60dc5b4f9..ad44855ca 100644 --- a/internal/transactions/get.go +++ b/internal/transactions/get.go @@ -28,8 +28,10 @@ import ( ) type flagsGet struct { - Sealed bool `default:"true" flag:"sealed" info:"Wait for a sealed result"` - Code bool `default:"false" flag:"code" info:"Display transaction code"` + Sealed bool `default:"true" flag:"sealed" info:"Wait for a sealed result"` + Code bool `default:"false" flag:"code" info:"⚠️ Deprecated: use include flag"` + 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)"` } var getFlags = flagsGet{} @@ -53,6 +55,10 @@ var GetCommand = &command.Command{ fmt.Println("⚠️ DEPRECATION WARNING: use \"flow transactions get\" instead") } + if getFlags.Code { + fmt.Println("⚠️ DEPRECATION WARNING: use include flag instead") + } + tx, result, err := services.Transactions.GetStatus( args[0], // transaction id getFlags.Sealed, @@ -62,9 +68,11 @@ var GetCommand = &command.Command{ } return &TransactionResult{ - result: result, - tx: tx, - code: getFlags.Code, + result: result, + tx: tx, + code: getFlags.Code, + include: getFlags.Include, + exclude: getFlags.Exclude, }, nil }, } diff --git a/internal/transactions/send-signed.go b/internal/transactions/send-signed.go index 0dbac74df..7b2c3e49a 100644 --- a/internal/transactions/send-signed.go +++ b/internal/transactions/send-signed.go @@ -26,6 +26,8 @@ import ( ) type flagsSendSigned 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"` } var sendSignedFlags = flagsSendSigned{} @@ -53,8 +55,10 @@ var SendSignedCommand = &command.Command{ } return &TransactionResult{ - result: result, - tx: tx, + result: result, + tx: tx, + include: getFlags.Include, + exclude: getFlags.Exclude, }, nil }, } diff --git a/internal/transactions/send.go b/internal/transactions/send.go index 5b861229f..a68aa1f85 100644 --- a/internal/transactions/send.go +++ b/internal/transactions/send.go @@ -32,6 +32,8 @@ type flagsSend struct { Arg []string `default:"" flag:"arg" info:"argument in Type:Value format"` Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"` + Include []string `default:"" flag:"include" info:"Fields to include in the output"` + Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output"` Code string `default:"" flag:"code" info:"⚠️ Deprecated: use filename argument"` Results bool `default:"" flag:"results" info:"⚠️ Deprecated: all transactions will provide result"` Args string `default:"" flag:"args" info:"⚠️ Deprecated: use arg or args-json flag"` @@ -86,8 +88,10 @@ var SendCommand = &command.Command{ } return &TransactionResult{ - result: result, - tx: tx, + result: result, + tx: tx, + include: getFlags.Include, + exclude: getFlags.Exclude, }, nil }, } diff --git a/internal/transactions/transactions.go b/internal/transactions/transactions.go index 84e87f73d..a1fb38f9b 100644 --- a/internal/transactions/transactions.go +++ b/internal/transactions/transactions.go @@ -121,8 +121,6 @@ func (r *TransactionResult) String() string { _, _ = fmt.Fprintf(writer, "\nNo Envelope Signatures\n") } - // todo add exclude flags - for i, e := range r.tx.PayloadSignatures { if command.FieldIncluded(r.include, "signatures") { _, _ = fmt.Fprintf(writer, "\nPayload Signature %v:\n", i) @@ -163,7 +161,7 @@ func (r *TransactionResult) String() string { } if r.tx.Script != nil { - if command.FieldIncluded(r.include, "code") { + if command.FieldIncluded(r.include, "code") || r.code { if len(r.tx.Arguments) == 0 { _, _ = fmt.Fprintf(writer, "\n\nArguments\tNo arguments\n") } else { From 1921d7e3a696d7a14d9bee7e9df6f6360382f5aa Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Mon, 17 May 2021 13:06:47 +0200 Subject: [PATCH 05/15] accounts add include flag support --- internal/accounts/accounts.go | 29 +++++++++++++++++++--------- internal/accounts/contract-add.go | 6 ++++-- internal/accounts/contract-remove.go | 6 ++++-- internal/accounts/contract-update.go | 6 ++++-- internal/accounts/create.go | 2 ++ internal/accounts/get.go | 10 ++++++++-- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/internal/accounts/accounts.go b/internal/accounts/accounts.go index ed4bb1ca5..0d3a4e937 100644 --- a/internal/accounts/accounts.go +++ b/internal/accounts/accounts.go @@ -22,6 +22,8 @@ import ( "bytes" "fmt" + "github.com/onflow/flow-cli/internal/command" + "github.com/onflow/cadence" "github.com/onflow/flow-go-sdk" "github.com/spf13/cobra" @@ -48,6 +50,7 @@ func init() { type AccountResult struct { *flow.Account showCode bool + include []string } // JSON convert result to JSON @@ -97,24 +100,32 @@ func (r *AccountResult) String() string { _, _ = fmt.Fprintf(writer, "\tSignature Algorithm\t %s\n", key.SigAlgo) _, _ = fmt.Fprintf(writer, "\tHash Algorithm\t %s\n", key.HashAlgo) _, _ = fmt.Fprintf(writer, "\tRevoked \t %t\n", key.Revoked) - fmt.Fprintf(writer, "\tSequence Number \t %d\n", key.SequenceNumber) - fmt.Fprintf(writer, "\tIndex \t %d\n", key.Index) - fmt.Fprintf(writer, "\n") + _, _ = fmt.Fprintf(writer, "\tSequence Number \t %d\n", key.SequenceNumber) + _, _ = fmt.Fprintf(writer, "\tIndex \t %d\n", key.Index) + _, _ = fmt.Fprintf(writer, "\n") + + // only show up to 3 keys and then show label to expand more info + if i == 3 && !command.FieldIncluded(r.include, "keys") { + _, _ = fmt.Fprint(writer, "...keys minimized, use --include keys flag if you want to view all\n\n") + break + } } - fmt.Fprintf(writer, "Contracts Deployed: %d\n", len(r.Contracts)) + _, _ = fmt.Fprintf(writer, "Contracts Deployed: %d\n", len(r.Contracts)) for name := range r.Contracts { - fmt.Fprintf(writer, "Contract: '%s'\n", name) + _, _ = fmt.Fprintf(writer, "Contract: '%s'\n", name) } - if r.showCode { + if r.showCode || command.FieldIncluded(r.include, "contracts") { for name, code := range r.Contracts { - fmt.Fprintf(writer, "Contracts '%s':\n", name) - fmt.Fprintln(writer, string(code)) + _, _ = fmt.Fprintf(writer, "Contracts '%s':\n", name) + _, _ = fmt.Fprintln(writer, string(code)) } + } else { + _, _ = fmt.Fprint(writer, "\n\nContracts (hidden, use --include contracts)") } - writer.Flush() + _ = writer.Flush() return b.String() } diff --git a/internal/accounts/contract-add.go b/internal/accounts/contract-add.go index 19643c5f8..34f9006b7 100644 --- a/internal/accounts/contract-add.go +++ b/internal/accounts/contract-add.go @@ -28,8 +28,9 @@ import ( ) type flagsAddContract struct { - Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` - Results bool `default:"false" flag:"results" info:"⚠️ Deprecated: results are provided by default"` + Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` + Results bool `default:"false" flag:"results" info:"⚠️ Deprecated: results are provided by default"` + Include []string `default:"" flag:"include" info:"Fields to include in the output"` } var addContractFlags = flagsAddContract{} @@ -66,6 +67,7 @@ var AddContractCommand = &command.Command{ return &AccountResult{ Account: account, showCode: false, + include: addContractFlags.Include, }, nil }, } diff --git a/internal/accounts/contract-remove.go b/internal/accounts/contract-remove.go index e41787c44..9cc16c547 100644 --- a/internal/accounts/contract-remove.go +++ b/internal/accounts/contract-remove.go @@ -28,8 +28,9 @@ import ( ) type flagsRemoveContract struct { - Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` - Results bool `default:"false" flag:"results" info:"⚠️ Deprecated: results are provided by default"` + Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` + Results bool `default:"false" flag:"results" info:"⚠️ Deprecated: results are provided by default"` + Include []string `default:"" flag:"include" info:"Fields to include in the output"` } var flagsRemove = flagsRemoveContract{} @@ -63,6 +64,7 @@ var RemoveCommand = &command.Command{ return &AccountResult{ Account: account, showCode: false, + include: flagsRemove.Include, }, nil }, } diff --git a/internal/accounts/contract-update.go b/internal/accounts/contract-update.go index a411ef952..9e23337f0 100644 --- a/internal/accounts/contract-update.go +++ b/internal/accounts/contract-update.go @@ -28,8 +28,9 @@ import ( ) type flagsUpdateContract struct { - Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` - Results bool `default:"false" flag:"results" info:"⚠️ Deprecated: results are provided by default"` + Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` + Results bool `default:"false" flag:"results" info:"⚠️ Deprecated: results are provided by default"` + Include []string `default:"" flag:"include" info:"Fields to include in the output"` } var updateFlags = flagsUpdateContract{} @@ -65,6 +66,7 @@ var UpdateCommand = &command.Command{ return &AccountResult{ Account: account, showCode: false, + include: updateFlags.Include, }, nil }, } diff --git a/internal/accounts/create.go b/internal/accounts/create.go index feb5ecfb7..27e5a3d7c 100644 --- a/internal/accounts/create.go +++ b/internal/accounts/create.go @@ -35,6 +35,7 @@ type flagsCreate struct { HashAlgo string `default:"SHA3_256" flag:"hash-algo" info:"Hash used for the digest"` Contracts []string `flag:"contract" info:"Contract to be deployed during account creation. "` Results bool `default:"false" flag:"results" info:"⚠️ Deprecated: results are provided by default"` + Include []string `default:"" flag:"include" info:"Fields to include in the output"` } var createFlags = flagsCreate{} @@ -71,6 +72,7 @@ var CreateCommand = &command.Command{ return &AccountResult{ Account: account, + include: createFlags.Include, }, nil }, } diff --git a/internal/accounts/get.go b/internal/accounts/get.go index 26433c52a..4c2cac4a8 100644 --- a/internal/accounts/get.go +++ b/internal/accounts/get.go @@ -28,8 +28,9 @@ import ( ) type flagsGet struct { - Contracts bool `default:"false" flag:"contracts" info:"Display contracts deployed to the account"` - Code bool `default:"false" flag:"code" info:"⚠️ Deprecated: use contracts flag instead"` + Contracts bool `default:"false" flag:"contracts" info:"⚠️ Deprecated: use include flag instead"` + Code bool `default:"false" flag:"code" info:"⚠️ Deprecated: use contracts flag instead"` + Include []string `default:"" flag:"include" info:"Fields to include in the output"` } var getFlags = flagsGet{} @@ -52,6 +53,10 @@ var GetCommand = &command.Command{ fmt.Println("⚠️ DEPRECATION WARNING: use contracts flag instead") } + if getFlags.Contracts { + fmt.Println("⚠️ DEPRECATION WARNING: use include contracts flag instead") + } + account, err := services.Accounts.Get(args[0]) // address if err != nil { return nil, err @@ -60,6 +65,7 @@ var GetCommand = &command.Command{ return &AccountResult{ Account: account, showCode: getFlags.Contracts || getFlags.Code, + include: getFlags.Include, }, nil }, } From 5f932ee9d8b79141d15b338034e34ac939987542 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Mon, 17 May 2021 13:07:17 +0200 Subject: [PATCH 06/15] blocks include flag support --- internal/blocks/blocks.go | 7 +++++-- internal/blocks/get.go | 15 ++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/blocks/blocks.go b/internal/blocks/blocks.go index b86a88cc7..892f22d84 100644 --- a/internal/blocks/blocks.go +++ b/internal/blocks/blocks.go @@ -22,6 +22,8 @@ import ( "bytes" "fmt" + "github.com/onflow/flow-cli/internal/command" + "github.com/onflow/flow-go-sdk" "github.com/onflow/flow-go-sdk/client" "github.com/spf13/cobra" @@ -46,6 +48,7 @@ type BlockResult struct { events []client.BlockEvents collections []*flow.Collection verbose bool + included []string } // JSON convert result to JSON @@ -94,7 +97,7 @@ func (r *BlockResult) String() string { for i, guarantee := range r.block.CollectionGuarantees { _, _ = fmt.Fprintf(writer, " Collection %d:\t%s\n", i, guarantee.CollectionID) - if r.verbose { + if r.verbose || command.FieldIncluded(r.included, "transactions") { for x, tx := range r.collections[i].TransactionIDs { _, _ = fmt.Fprintf(writer, " Transaction %d: %s\n", x, tx) } @@ -108,7 +111,7 @@ func (r *BlockResult) String() string { _, _ = fmt.Fprintf(writer, "%s", e.String()) } - writer.Flush() + _ = writer.Flush() return b.String() } diff --git a/internal/blocks/get.go b/internal/blocks/get.go index f11e99575..2a58691c5 100644 --- a/internal/blocks/get.go +++ b/internal/blocks/get.go @@ -28,11 +28,12 @@ import ( ) type flagsBlocks struct { - Events string `default:"" flag:"events" info:"List events of this type for the block"` - Verbose bool `default:"false" flag:"verbose" info:"Display transactions in block"` - Latest bool `default:"false" flag:"latest" info:"⚠️ No longer supported: use command argument"` - BlockID string `default:"" flag:"id" info:"⚠️ No longer supported: use command argument"` - BlockHeight uint64 `default:"0" flag:"height" info:"⚠️ No longer supported: use command argument"` + Events string `default:"" flag:"events" info:"List events of this type for the block"` + Include []string `default:"" flag:"include" info:"Fields to include in the output"` + Verbose bool `default:"false" flag:"verbose" info:"⚠️ Deprecated: use include transactions flag instead"` + Latest bool `default:"false" flag:"latest" info:"⚠️ No longer supported: use command argument"` + BlockID string `default:"" flag:"id" info:"⚠️ No longer supported: use command argument"` + BlockHeight uint64 `default:"0" flag:"height" info:"⚠️ No longer supported: use command argument"` } var blockFlags = flagsBlocks{} @@ -55,6 +56,10 @@ var GetCommand = &command.Command{ return nil, fmt.Errorf("⚠️ No longer supported: use command argument.") } + if blockFlags.Verbose { + fmt.Println("⚠️ DEPRECATION WARNING: use include transactions flag instead") + } + block, events, collections, err := services.Blocks.GetBlock( args[0], // block id blockFlags.Events, From 3fda5a7ade9e2644f3802a1df4eb93c5115da562 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Mon, 17 May 2021 13:07:32 +0200 Subject: [PATCH 07/15] add exclude flag to tx --- internal/transactions/send-signed.go | 6 +++--- internal/transactions/send.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/transactions/send-signed.go b/internal/transactions/send-signed.go index 7b2c3e49a..61de3e88f 100644 --- a/internal/transactions/send-signed.go +++ b/internal/transactions/send-signed.go @@ -27,7 +27,7 @@ import ( type flagsSendSigned 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"` + Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"` } var sendSignedFlags = flagsSendSigned{} @@ -57,8 +57,8 @@ var SendSignedCommand = &command.Command{ return &TransactionResult{ result: result, tx: tx, - include: getFlags.Include, - exclude: getFlags.Exclude, + include: sendSignedFlags.Include, + exclude: sendSignedFlags.Exclude, }, nil }, } diff --git a/internal/transactions/send.go b/internal/transactions/send.go index a68aa1f85..1dfc07be2 100644 --- a/internal/transactions/send.go +++ b/internal/transactions/send.go @@ -33,7 +33,7 @@ type flagsSend struct { Signer string `default:"emulator-account" flag:"signer" info:"Account name from configuration used to sign the transaction"` GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"` Include []string `default:"" flag:"include" info:"Fields to include in the output"` - Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output"` + Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output (events)"` Code string `default:"" flag:"code" info:"⚠️ Deprecated: use filename argument"` Results bool `default:"" flag:"results" info:"⚠️ Deprecated: all transactions will provide result"` Args string `default:"" flag:"args" info:"⚠️ Deprecated: use arg or args-json flag"` @@ -90,8 +90,8 @@ var SendCommand = &command.Command{ return &TransactionResult{ result: result, tx: tx, - include: getFlags.Include, - exclude: getFlags.Exclude, + include: sendFlags.Include, + exclude: sendFlags.Exclude, }, nil }, } From de9858d6985d2c2fda4483f627ee097b0e6d20b3 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 09:39:13 +0200 Subject: [PATCH 08/15] add logs to account --- pkg/flowcli/services/accounts.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/flowcli/services/accounts.go b/pkg/flowcli/services/accounts.go index 52556a4d7..15c5f6aec 100644 --- a/pkg/flowcli/services/accounts.go +++ b/pkg/flowcli/services/accounts.go @@ -343,15 +343,17 @@ func (a *Accounts) addContract( if updateExisting { a.logger.Info(fmt.Sprintf( - "Contract '%s' updated on the account '%s'.", + "Contract '%s' updated on the account '%s'.\nTransaction ID: %s.", contractName, account.Address(), + sentTx.ID().String(), )) } else { a.logger.Info(fmt.Sprintf( - "Contract '%s' deployed to the account '%s'.", + "Contract '%s' deployed to the account '%s'.\nTransaction ID: %s.", contractName, account.Address(), + sentTx.ID().String(), )) } @@ -398,7 +400,12 @@ func (a *Accounts) RemoveContract( } a.logger.StopProgress() - a.logger.Info(fmt.Sprintf("Contract %s removed from account %s\n", contractName, account.Address())) + a.logger.Info(fmt.Sprintf( + "Contract %s removed from account %s.\nTransaction ID: %s.", + contractName, + account.Address(), + sentTx.ID().String(), + )) return a.gateway.GetAccount(account.Address()) } From 1db85d0efe0b0583f30e64e2651c445cf87b2914 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 13:09:19 +0200 Subject: [PATCH 09/15] refactor and extract emoji --- internal/command/command.go | 33 ++++++++++++++------------- internal/keys/keys.go | 4 +++- internal/transactions/transactions.go | 6 +++-- pkg/flowcli/output/emoji.go | 29 +++++++++++++++++++++++ pkg/flowcli/output/logger.go | 2 +- pkg/flowcli/services/project.go | 2 +- 6 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 pkg/flowcli/output/emoji.go diff --git a/internal/command/command.go b/internal/command/command.go index ea8ff954c..f1aa43e2c 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -301,7 +301,7 @@ func outputResult(result string, saveFlag string, formatFlag string, filterFlag Fs: afero.NewOsFs(), } - fmt.Printf("💾 result saved to: %s \n", saveFlag) + fmt.Printf("%s result saved to: %s \n", output.SaveEmoji(), saveFlag) return af.WriteFile(saveFlag, []byte(result), 0644) } @@ -350,32 +350,32 @@ func handleError(description string, err error) { // handle rpc error switch t := err.(type) { case *client.RPCError: - _, _ = fmt.Fprintf(os.Stderr, "❌ Grpc Error: %s \n", t.GRPCStatus().Err().Error()) + _, _ = fmt.Fprintf(os.Stderr, "%s Grpc Error: %s \n", output.ErrorEmoji(), t.GRPCStatus().Err().Error()) default: if errors.Is(err, config.ErrOutdatedFormat) { - _, _ = fmt.Fprintf(os.Stderr, "❌ Config Error: %s \n", err.Error()) - _, _ = fmt.Fprintf(os.Stderr, "🙏 Please reset configuration using: 'flow init --reset'. Read more about new configuration here: https://github.com/onflow/flow-cli/releases/tag/v0.17.0") + _, _ = fmt.Fprintf(os.Stderr, "%s Config Error: %s \n", output.ErrorEmoji(), err.Error()) + _, _ = fmt.Fprintf(os.Stderr, "%s Please reset configuration using: 'flow init --reset'. Read more about new configuration here: https://github.com/onflow/flow-cli/releases/tag/v0.17.0", output.TryEmoji()) } else if strings.Contains(err.Error(), "transport:") { - _, _ = fmt.Fprintf(os.Stderr, "❌ %s \n", strings.Split(err.Error(), "transport:")[1]) - _, _ = fmt.Fprintf(os.Stderr, "🙏 Make sure your emulator is running or connection address is correct.") + _, _ = fmt.Fprintf(os.Stderr, "%s %s \n", output.ErrorEmoji(), strings.Split(err.Error(), "transport:")[1]) + _, _ = fmt.Fprintf(os.Stderr, "%s Make sure your emulator is running or connection address is correct.", output.TryEmoji()) } else if strings.Contains(err.Error(), "NotFound desc =") { - _, _ = fmt.Fprintf(os.Stderr, "❌ Not Found:%s \n", strings.Split(err.Error(), "NotFound desc =")[1]) + _, _ = fmt.Fprintf(os.Stderr, "%s Not Found:%s \n", output.ErrorEmoji(), strings.Split(err.Error(), "NotFound desc =")[1]) } else if strings.Contains(err.Error(), "code = InvalidArgument desc = ") { desc := strings.Split(err.Error(), "code = InvalidArgument desc = ") - _, _ = fmt.Fprintf(os.Stderr, "❌ Invalid argument: %s \n", desc[len(desc)-1]) + _, _ = fmt.Fprintf(os.Stderr, "%s Invalid argument: %s \n", output.ErrorEmoji(), desc[len(desc)-1]) if strings.Contains(err.Error(), "is invalid for chain") { - _, _ = fmt.Fprintf(os.Stderr, "🙏 Check you are connecting to the correct network or account address you use is correct.") + _, _ = fmt.Fprintf(os.Stderr, "%s Check you are connecting to the correct network or account address you use is correct.", output.TryEmoji()) } else { - _, _ = fmt.Fprintf(os.Stderr, "🙏 Check your argument and flags value, you can use --help.") + _, _ = fmt.Fprintf(os.Stderr, "%s Check your argument and flags value, you can use --help.", output.TryEmoji()) } } else if strings.Contains(err.Error(), "invalid signature:") { - _, _ = fmt.Fprintf(os.Stderr, "❌ Invalid signature: %s \n", strings.Split(err.Error(), "invalid signature:")[1]) - _, _ = fmt.Fprintf(os.Stderr, "🙏 Check the signer private key is provided or is in the correct format. If running emulator, make sure it's using the same configuration as this command.") + _, _ = fmt.Fprintf(os.Stderr, "%s Invalid signature: %s \n", output.ErrorEmoji(), strings.Split(err.Error(), "invalid signature:")[1]) + _, _ = fmt.Fprintf(os.Stderr, "%s Check the signer private key is provided or is in the correct format. If running emulator, make sure it's using the same configuration as this command.", output.TryEmoji()) } else if strings.Contains(err.Error(), "signature could not be verified using public key with") { - _, _ = fmt.Fprintf(os.Stderr, "❌ %s: %s \n", description, err) - _, _ = fmt.Fprintf(os.Stderr, "🙏 If you are running emulator locally make sure that the emulator was started with the same config as used in this command. \nTry restarting the emulator.") + _, _ = fmt.Fprintf(os.Stderr, "%s %s: %s \n", output.ErrorEmoji(), description, err) + _, _ = fmt.Fprintf(os.Stderr, "%s If you are running emulator locally make sure that the emulator was started with the same config as used in this command. \nTry restarting the emulator.", output.TryEmoji()) } else { - _, _ = fmt.Fprintf(os.Stderr, "❌ %s: %s", description, err) + _, _ = fmt.Fprintf(os.Stderr, "%s %s: %s", output.ErrorEmoji(), description, err) } } @@ -396,8 +396,9 @@ func checkVersion(logger output.Logger) { if latestVersion != build.Semver() { logger.Info(fmt.Sprintf( - "\n⚠️ Version warning: a new version of Flow CLI is available (%s).\n"+ + "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ "Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install", + output.WarningEmoji(), strings.ReplaceAll(string(latestVersion), "\n", ""), )) } diff --git a/internal/keys/keys.go b/internal/keys/keys.go index 69b602881..16986039f 100644 --- a/internal/keys/keys.go +++ b/internal/keys/keys.go @@ -23,6 +23,8 @@ import ( "encoding/hex" "fmt" + "github.com/onflow/flow-cli/pkg/flowcli/output" + "github.com/onflow/flow-go-sdk" "github.com/onflow/flow-go-sdk/crypto" "github.com/spf13/cobra" @@ -65,7 +67,7 @@ func (k *KeyResult) String() string { writer := util.CreateTabWriter(&b) if k.privateKey != nil { - _, _ = fmt.Fprintf(writer, "🔴️ Store private key safely and don't share with anyone! \n") + _, _ = fmt.Fprintf(writer, "%s Store private key safely and don't share with anyone! \n", output.StopEmoji()) _, _ = fmt.Fprintf(writer, "Private Key \t %x \n", k.privateKey.Encode()) } diff --git a/internal/transactions/transactions.go b/internal/transactions/transactions.go index a1fb38f9b..e27d39ff4 100644 --- a/internal/transactions/transactions.go +++ b/internal/transactions/transactions.go @@ -23,6 +23,8 @@ import ( "encoding/json" "fmt" + "github.com/onflow/flow-cli/pkg/flowcli/output" + "github.com/onflow/flow-cli/internal/command" jsoncdc "github.com/onflow/cadence/encoding/json" @@ -94,12 +96,12 @@ func (r *TransactionResult) String() string { if r.result != nil { if r.result.Error != nil { - _, _ = fmt.Fprintf(writer, "❌ Transaction Error \n%s\n\n\n", r.result.Error.Error()) + _, _ = fmt.Fprintf(writer, "%s Transaction Error \n%s\n\n\n", output.ErrorEmoji(), r.result.Error.Error()) } statusBadge := "" if r.result.Status == flow.TransactionStatusSealed { - statusBadge = "✅" + statusBadge = output.OkEmoji() } _, _ = fmt.Fprintf(writer, "Status\t%s %s\n", statusBadge, r.result.Status) } diff --git a/pkg/flowcli/output/emoji.go b/pkg/flowcli/output/emoji.go new file mode 100644 index 000000000..6593d0842 --- /dev/null +++ b/pkg/flowcli/output/emoji.go @@ -0,0 +1,29 @@ +package output + +func ErrorEmoji() string { + return "❌" +} + +func TryEmoji() string { + return "🙏" +} + +func WarningEmoji() string { + return "⚠️" +} + +func SaveEmoji() string { + return "💾" +} + +func StopEmoji() string { + return "🔴️" +} + +func OkEmoji() string { + return "✅" +} + +func SuccessEmoji() string { + return "✨" +} diff --git a/pkg/flowcli/output/logger.go b/pkg/flowcli/output/logger.go index cba9a6c73..01deaec7e 100644 --- a/pkg/flowcli/output/logger.go +++ b/pkg/flowcli/output/logger.go @@ -69,7 +69,7 @@ func (s *StdoutLogger) Debug(msg string) { } func (s *StdoutLogger) Error(msg string) { - s.log(fmt.Sprintf("❌ %s", util.Red(msg)), ErrorLog) + s.log(fmt.Sprintf("%s %s", ErrorEmoji(), util.Red(msg)), ErrorLog) } func (s *StdoutLogger) StartProgress(msg string) { diff --git a/pkg/flowcli/services/project.go b/pkg/flowcli/services/project.go index 021df62dc..f7dd66c56 100644 --- a/pkg/flowcli/services/project.go +++ b/pkg/flowcli/services/project.go @@ -242,7 +242,7 @@ func (p *Project) Deploy(network string, update bool) ([]*contracts.Contract, er } if !deployErr { - p.logger.Info("\n✨ All contracts deployed successfully") + p.logger.Info(fmt.Sprintf("\n%s All contracts deployed successfully", output.SuccessEmoji())) } else { err = fmt.Errorf("failed to deploy contracts") p.logger.Error(err.Error()) From f56476500d02f25198c1742ef055385753bbbd17 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 13:17:44 +0200 Subject: [PATCH 10/15] refactor status for colors and emoji --- internal/status/status.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/internal/status/status.go b/internal/status/status.go index 5b1a33ea0..5a00f03fe 100644 --- a/internal/status/status.go +++ b/internal/status/status.go @@ -22,6 +22,8 @@ import ( "bytes" "fmt" + "github.com/onflow/flow-cli/pkg/flowcli/output" + "github.com/spf13/cobra" "github.com/onflow/flow-cli/internal/command" @@ -62,39 +64,31 @@ type Result struct { err error } -const ( - OnlineIcon = "🟢" - OnlineStatus = "ONLINE" - - OfflineIcon = "🔴" - OfflineStatus = "OFFLINE" -) - // getStatus returns string representation for Flow network status. func (r *Result) getStatus() string { if r.err == nil { - return OnlineStatus + return "ONLINE" } - return OfflineStatus + return "OFFLINE" } // getColoredStatus returns colored string representation for Flow network status. func (r *Result) getColoredStatus() string { if r.err == nil { - return util.Green(OnlineStatus) + return output.Green(r.getStatus()) } - return util.Red(OfflineStatus) + return util.Red(output.Red(r.getStatus())) } // getIcon returns emoji icon representing Flow network status. func (r *Result) getIcon() string { if r.err == nil { - return OnlineIcon + return output.GoEmoji() } - return OfflineIcon + return output.StopEmoji() } // String converts result to a string. From 99f6098c829b65f9a1270a29a8774d9ad3730ec3 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 13:18:56 +0200 Subject: [PATCH 11/15] implement own color --- pkg/flowcli/output/colors.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 pkg/flowcli/output/colors.go diff --git a/pkg/flowcli/output/colors.go b/pkg/flowcli/output/colors.go new file mode 100644 index 000000000..a61457745 --- /dev/null +++ b/pkg/flowcli/output/colors.go @@ -0,0 +1,31 @@ +package output + +import ( + "fmt" + "runtime" +) + +var red = "\033[31m" +var green = "\033[32m" +var bold = "\033[1m" +var reset = "\033[0m" + +func printColor(msg string, color string) string { + if runtime.GOOS == "windows" { + return msg + } + + return fmt.Sprintf("%s%s%s", color, msg, reset) +} + +func Red(msg string) string { + return printColor(msg, red) +} + +func Green(msg string) string { + return printColor(msg, green) +} + +func Bold(msg string) string { + return printColor(msg, bold) +} From 1e9a1141cb892d380b9476d5696d7be9ef0dca2d Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 13:19:10 +0200 Subject: [PATCH 12/15] refactor color use and remove dependency --- internal/config/init.go | 8 +++++--- internal/status/status.go | 2 +- pkg/flowcli/output/emoji.go | 4 ++++ pkg/flowcli/output/logger.go | 4 +--- pkg/flowcli/services/project.go | 4 ++-- pkg/flowcli/util/utilities.go | 6 ------ 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/internal/config/init.go b/internal/config/init.go index 0937c0559..990e1f241 100644 --- a/internal/config/init.go +++ b/internal/config/init.go @@ -22,6 +22,8 @@ import ( "bytes" "fmt" + "github.com/onflow/flow-cli/pkg/flowcli/output" + "github.com/spf13/cobra" "github.com/onflow/flow-cli/internal/command" @@ -84,11 +86,11 @@ func (r *InitResult) String() string { account, _ := r.Project.EmulatorServiceAccount() _, _ = fmt.Fprintf(writer, "Configuration initialized\n") - _, _ = fmt.Fprintf(writer, "Service account: %s\n\n", util.Bold("0x"+account.Address().String())) + _, _ = fmt.Fprintf(writer, "Service account: %s\n\n", output.Bold("0x"+account.Address().String())) _, _ = fmt.Fprintf(writer, "Start emulator by running: %s \nReset configuration using: %s\n", - util.Bold("'flow emulator'"), - util.Bold("'flow init --reset'"), + output.Bold("'flow emulator'"), + output.Bold("'flow init --reset'"), ) writer.Flush() diff --git a/internal/status/status.go b/internal/status/status.go index 5a00f03fe..ee1a45095 100644 --- a/internal/status/status.go +++ b/internal/status/status.go @@ -79,7 +79,7 @@ func (r *Result) getColoredStatus() string { return output.Green(r.getStatus()) } - return util.Red(output.Red(r.getStatus())) + return output.Red(output.Red(r.getStatus())) } // getIcon returns emoji icon representing Flow network status. diff --git a/pkg/flowcli/output/emoji.go b/pkg/flowcli/output/emoji.go index 6593d0842..cc86cc500 100644 --- a/pkg/flowcli/output/emoji.go +++ b/pkg/flowcli/output/emoji.go @@ -20,6 +20,10 @@ func StopEmoji() string { return "🔴️" } +func GoEmoji() string { + return "🟢" +} + func OkEmoji() string { return "✅" } diff --git a/pkg/flowcli/output/logger.go b/pkg/flowcli/output/logger.go index 01deaec7e..8e9009871 100644 --- a/pkg/flowcli/output/logger.go +++ b/pkg/flowcli/output/logger.go @@ -20,8 +20,6 @@ package output import ( "fmt" - - "github.com/onflow/flow-cli/pkg/flowcli/util" ) const ( @@ -69,7 +67,7 @@ func (s *StdoutLogger) Debug(msg string) { } func (s *StdoutLogger) Error(msg string) { - s.log(fmt.Sprintf("%s %s", ErrorEmoji(), util.Red(msg)), ErrorLog) + s.log(fmt.Sprintf("%s %s", ErrorEmoji(), output.Red(msg)), ErrorLog) } func (s *StdoutLogger) StartProgress(msg string) { diff --git a/pkg/flowcli/services/project.go b/pkg/flowcli/services/project.go index f7dd66c56..374434403 100644 --- a/pkg/flowcli/services/project.go +++ b/pkg/flowcli/services/project.go @@ -221,7 +221,7 @@ func (p *Project) Deploy(network string, update bool) ([]*contracts.Contract, er } p.logger.StartProgress( - fmt.Sprintf("%s deploying...", util.Bold(contract.Name())), + fmt.Sprintf("%s deploying...", output.Bold(contract.Name())), ) result, err := p.gateway.GetTransactionResult(sentTx, true) @@ -232,7 +232,7 @@ func (p *Project) Deploy(network string, update bool) ([]*contracts.Contract, er if result.Error == nil && !deployErr { p.logger.StopProgress() - fmt.Printf("%s -> 0x%s\n", util.Green(contract.Name()), contract.Target()) + fmt.Printf("%s -> 0x%s\n", output.Green(contract.Name()), contract.Target()) } else { p.logger.StopProgress() p.logger.Error( diff --git a/pkg/flowcli/util/utilities.go b/pkg/flowcli/util/utilities.go index c5047f956..511a2f87b 100644 --- a/pkg/flowcli/util/utilities.go +++ b/pkg/flowcli/util/utilities.go @@ -25,16 +25,10 @@ import ( "strings" "text/tabwriter" - "github.com/fatih/color" - "github.com/onflow/flow-go-sdk" "github.com/onflow/flow-go-sdk/crypto" ) -var Green = color.New(color.FgGreen, color.Bold).SprintfFunc() -var Red = color.New(color.FgRed, color.Bold).SprintfFunc() -var Bold = color.New(color.FgCyan).SprintfFunc() - // LoadFile loads a file by filename. func LoadFile(filename string) ([]byte, error) { var code []byte From 4dec96b1ea85dc9bb87a32dfadc6bec03bc63087 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 13:21:09 +0200 Subject: [PATCH 13/15] version check fix --- internal/command/command.go | 4 ++-- pkg/flowcli/output/logger.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index f1aa43e2c..35f552d62 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -396,8 +396,8 @@ func checkVersion(logger output.Logger) { if latestVersion != build.Semver() { logger.Info(fmt.Sprintf( - "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ - "Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install", + "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ + " Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install\n", output.WarningEmoji(), strings.ReplaceAll(string(latestVersion), "\n", ""), )) diff --git a/pkg/flowcli/output/logger.go b/pkg/flowcli/output/logger.go index 8e9009871..3cb717637 100644 --- a/pkg/flowcli/output/logger.go +++ b/pkg/flowcli/output/logger.go @@ -67,7 +67,7 @@ func (s *StdoutLogger) Debug(msg string) { } func (s *StdoutLogger) Error(msg string) { - s.log(fmt.Sprintf("%s %s", ErrorEmoji(), output.Red(msg)), ErrorLog) + s.log(fmt.Sprintf("%s %s", ErrorEmoji(), Red(msg)), ErrorLog) } func (s *StdoutLogger) StartProgress(msg string) { From ef19ca7d311391accede9294828955a831874a6d Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 18:01:29 +0200 Subject: [PATCH 14/15] emoji test os --- pkg/flowcli/output/emoji.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/pkg/flowcli/output/emoji.go b/pkg/flowcli/output/emoji.go index cc86cc500..b0465cd61 100644 --- a/pkg/flowcli/output/emoji.go +++ b/pkg/flowcli/output/emoji.go @@ -1,33 +1,43 @@ package output +import "runtime" + +func printEmoji(emoji string) string { + if runtime.GOOS == "windows" { + return "" + } + + return emoji +} + func ErrorEmoji() string { - return "❌" + return printEmoji("❌") } func TryEmoji() string { - return "🙏" + return printEmoji("🙏") } func WarningEmoji() string { - return "⚠️" + return printEmoji("⚠️") } func SaveEmoji() string { - return "💾" + return printEmoji("💾") } func StopEmoji() string { - return "🔴️" + return printEmoji("🔴️") } func GoEmoji() string { - return "🟢" + return printEmoji("🟢") } func OkEmoji() string { - return "✅" + return printEmoji("✅") } func SuccessEmoji() string { - return "✨" + return printEmoji("✨") } From bb9b3f5f0b53451957edd820008331f422d5c026 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Fri, 21 May 2021 11:55:35 +0200 Subject: [PATCH 15/15] refactor include exclude function --- internal/accounts/accounts.go | 4 ++-- internal/blocks/blocks.go | 2 +- internal/command/result.go | 17 +++++++++-------- internal/transactions/transactions.go | 12 ++++++------ pkg/flowcli/util/utilities.go | 11 ----------- 5 files changed, 18 insertions(+), 28 deletions(-) diff --git a/internal/accounts/accounts.go b/internal/accounts/accounts.go index 0d3a4e937..3f3d8c68d 100644 --- a/internal/accounts/accounts.go +++ b/internal/accounts/accounts.go @@ -105,7 +105,7 @@ func (r *AccountResult) String() string { _, _ = fmt.Fprintf(writer, "\n") // only show up to 3 keys and then show label to expand more info - if i == 3 && !command.FieldIncluded(r.include, "keys") { + if i == 3 && !command.ContainsFlag(r.include, "keys") { _, _ = fmt.Fprint(writer, "...keys minimized, use --include keys flag if you want to view all\n\n") break } @@ -116,7 +116,7 @@ func (r *AccountResult) String() string { _, _ = fmt.Fprintf(writer, "Contract: '%s'\n", name) } - if r.showCode || command.FieldIncluded(r.include, "contracts") { + if r.showCode || command.ContainsFlag(r.include, "contracts") { for name, code := range r.Contracts { _, _ = fmt.Fprintf(writer, "Contracts '%s':\n", name) _, _ = fmt.Fprintln(writer, string(code)) diff --git a/internal/blocks/blocks.go b/internal/blocks/blocks.go index 892f22d84..d0e51733c 100644 --- a/internal/blocks/blocks.go +++ b/internal/blocks/blocks.go @@ -97,7 +97,7 @@ func (r *BlockResult) String() string { for i, guarantee := range r.block.CollectionGuarantees { _, _ = fmt.Fprintf(writer, " Collection %d:\t%s\n", i, guarantee.CollectionID) - if r.verbose || command.FieldIncluded(r.included, "transactions") { + if r.verbose || command.ContainsFlag(r.included, "transactions") { for x, tx := range r.collections[i].TransactionIDs { _, _ = fmt.Fprintf(writer, " Transaction %d: %s\n", x, tx) } diff --git a/internal/command/result.go b/internal/command/result.go index 067b3d221..13723dd40 100644 --- a/internal/command/result.go +++ b/internal/command/result.go @@ -19,7 +19,7 @@ package command import ( - "github.com/onflow/flow-cli/pkg/flowcli/util" + "strings" ) type Result interface { @@ -28,12 +28,13 @@ type Result interface { JSON() interface{} } -// FieldIncluded checks if field should be included in the result by checking if included flags contains field -func FieldIncluded(included []string, field string) bool { - return util.ContainsStringInsensitive(included, field) -} +// ContainsFlag checks if output flag is present for the provided field +func ContainsFlag(flags []string, field string) bool { + for _, n := range flags { + if strings.ToLower(n) == field { + return true + } + } -// FieldExcluded checks if field should be excluded from the result by checking the flags -func FieldExcluded(excluded []string, field string) bool { - return util.ContainsStringInsensitive(excluded, field) + return false } diff --git a/internal/transactions/transactions.go b/internal/transactions/transactions.go index e27d39ff4..e80a87247 100644 --- a/internal/transactions/transactions.go +++ b/internal/transactions/transactions.go @@ -124,7 +124,7 @@ func (r *TransactionResult) String() string { } for i, e := range r.tx.PayloadSignatures { - if command.FieldIncluded(r.include, "signatures") { + if command.ContainsFlag(r.include, "signatures") { _, _ = fmt.Fprintf(writer, "\nPayload Signature %v:\n", i) _, _ = fmt.Fprintf(writer, " Address\t%s\n", e.Address) _, _ = fmt.Fprintf(writer, " Signature\t%x\n", e.Signature) @@ -135,7 +135,7 @@ func (r *TransactionResult) String() string { } for i, e := range r.tx.EnvelopeSignatures { - if command.FieldIncluded(r.include, "signatures") { + if command.ContainsFlag(r.include, "signatures") { _, _ = fmt.Fprintf(writer, "\nEnvelope Signature %v:\n", i) _, _ = fmt.Fprintf(writer, " Address\t%s\n", e.Address) _, _ = fmt.Fprintf(writer, " Signature\t%x\n", e.Signature) @@ -145,11 +145,11 @@ func (r *TransactionResult) String() string { } } - if !command.FieldIncluded(r.include, "signatures") { + if !command.ContainsFlag(r.include, "signatures") { _, _ = fmt.Fprintf(writer, "\nSignatures (minimized, use --include signatures)") } - if !command.FieldExcluded(r.exclude, "events") { + if !command.ContainsFlag(r.exclude, "events") { e := events.EventResult{ Events: r.result.Events, } @@ -163,7 +163,7 @@ func (r *TransactionResult) String() string { } if r.tx.Script != nil { - if command.FieldIncluded(r.include, "code") || r.code { + if command.ContainsFlag(r.include, "code") || r.code { if len(r.tx.Arguments) == 0 { _, _ = fmt.Fprintf(writer, "\n\nArguments\tNo arguments\n") } else { @@ -179,7 +179,7 @@ func (r *TransactionResult) String() string { } } - if command.FieldIncluded(r.include, "payload") { + if command.ContainsFlag(r.include, "payload") { _, _ = fmt.Fprintf(writer, "\n\nPayload:\n%x", r.tx.Encode()) } else { _, _ = fmt.Fprint(writer, "\n\nPayload (hidden, use --include payload)") diff --git a/pkg/flowcli/util/utilities.go b/pkg/flowcli/util/utilities.go index 511a2f87b..f25d57099 100644 --- a/pkg/flowcli/util/utilities.go +++ b/pkg/flowcli/util/utilities.go @@ -22,7 +22,6 @@ import ( "bytes" "fmt" "io/ioutil" - "strings" "text/tabwriter" "github.com/onflow/flow-go-sdk" @@ -86,16 +85,6 @@ func ContainsString(s []string, e string) bool { return false } -func ContainsStringInsensitive(haystack []string, needle string) bool { - for _, n := range haystack { - if strings.ToLower(n) == needle { - return true - } - } - - return false -} - // GetAddressNetwork returns the chain ID for an address. func GetAddressNetwork(address flow.Address) (flow.ChainID, error) { networks := []flow.ChainID{