Skip to content
29 changes: 20 additions & 9 deletions internal/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -48,6 +50,7 @@ func init() {
type AccountResult struct {
*flow.Account
showCode bool
include []string
}

// JSON convert result to JSON
Expand Down Expand Up @@ -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.ContainsFlag(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.ContainsFlag(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()
}
Expand Down
6 changes: 4 additions & 2 deletions internal/accounts/contract-add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -66,6 +67,7 @@ var AddContractCommand = &command.Command{
return &AccountResult{
Account: account,
showCode: false,
include: addContractFlags.Include,
}, nil
},
}
6 changes: 4 additions & 2 deletions internal/accounts/contract-remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -63,6 +64,7 @@ var RemoveCommand = &command.Command{
return &AccountResult{
Account: account,
showCode: false,
include: flagsRemove.Include,
}, nil
},
}
6 changes: 4 additions & 2 deletions internal/accounts/contract-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -65,6 +66,7 @@ var UpdateCommand = &command.Command{
return &AccountResult{
Account: account,
showCode: false,
include: updateFlags.Include,
}, nil
},
}
2 changes: 2 additions & 0 deletions internal/accounts/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. <name:filename>"`
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{}
Expand Down Expand Up @@ -71,6 +72,7 @@ var CreateCommand = &command.Command{

return &AccountResult{
Account: account,
include: createFlags.Include,
}, nil
},
}
10 changes: 8 additions & 2 deletions internal/accounts/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand All @@ -60,6 +65,7 @@ var GetCommand = &command.Command{
return &AccountResult{
Account: account,
showCode: getFlags.Contracts || getFlags.Code,
include: getFlags.Include,
}, nil
},
}
7 changes: 5 additions & 2 deletions internal/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -46,6 +48,7 @@ type BlockResult struct {
events []client.BlockEvents
collections []*flow.Collection
verbose bool
included []string
}

// JSON convert result to JSON
Expand Down Expand Up @@ -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.ContainsFlag(r.included, "transactions") {
for x, tx := range r.collections[i].TransactionIDs {
_, _ = fmt.Fprintf(writer, " Transaction %d: %s\n", x, tx)
}
Expand All @@ -108,7 +111,7 @@ func (r *BlockResult) String() string {
_, _ = fmt.Fprintf(writer, "%s", e.String())
}

writer.Flush()
_ = writer.Flush()
return b.String()
}

Expand Down
15 changes: 10 additions & 5 deletions internal/blocks/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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,
Expand Down
35 changes: 18 additions & 17 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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"+
"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", ""),
))
}
Expand Down
15 changes: 15 additions & 0 deletions internal/command/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,23 @@

package command

import (
"strings"
)

type Result interface {
String() string
Oneliner() string
JSON() interface{}
}

// 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
}
}

return false
}
8 changes: 5 additions & 3 deletions internal/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion internal/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}

Expand Down
Loading