From 25c9492cffbf9ab803c55e842fcab6becad41d88 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Thu, 21 Nov 2024 16:19:32 -0800 Subject: [PATCH 1/7] Remove custom name option --- internal/dependencymanager/add.go | 4 +--- .../dependencymanager/dependencyinstaller.go | 19 ++++--------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/internal/dependencymanager/add.go b/internal/dependencymanager/add.go index 736a3b4c7..2de6071e4 100644 --- a/internal/dependencymanager/add.go +++ b/internal/dependencymanager/add.go @@ -59,8 +59,6 @@ flow dependencies add FlowToken`, func init() { // Add common flags. addFlags.Flags.AddToCommand(addCommand.Cmd) - // Add command-specific flags. - addCommand.Cmd.Flags().StringVar(&addFlags.name, "name", "", "Name of the dependency") } func add( @@ -91,7 +89,7 @@ func add( } // Otherwise, add the dependency by source string. - if err := installer.AddBySourceString(dep, addFlags.name); err != nil { + if err := installer.AddBySourceString(dep); err != nil { logger.Error(fmt.Sprintf("Error: %v", err)) return nil, err } diff --git a/internal/dependencymanager/dependencyinstaller.go b/internal/dependencymanager/dependencyinstaller.go index 84963a020..882df2c69 100644 --- a/internal/dependencymanager/dependencyinstaller.go +++ b/internal/dependencymanager/dependencyinstaller.go @@ -182,20 +182,14 @@ func (di *DependencyInstaller) Install() error { } // AddBySourceString processes a single dependency and installs it and any dependencies it has, as well as adding it to the state -func (di *DependencyInstaller) AddBySourceString(depSource, customName string) error { +func (di *DependencyInstaller) AddBySourceString(depSource string) error { depNetwork, depAddress, depContractName, err := config.ParseSourceString(depSource) if err != nil { return fmt.Errorf("error parsing source: %w", err) } - name := depContractName - - if customName != "" { - name = customName - } - dep := config.Dependency{ - Name: name, + Name: depContractName, Source: config.Source{ NetworkName: depNetwork, Address: flowsdk.HexToAddress(depAddress), @@ -206,7 +200,7 @@ func (di *DependencyInstaller) AddBySourceString(depSource, customName string) e return di.Add(dep) } -func (di *DependencyInstaller) AddByCoreContractName(coreContractName, customName string) error { +func (di *DependencyInstaller) AddByCoreContractName(coreContractName string) error { var depNetwork, depAddress, depContractName string sc := systemcontracts.SystemContractsForChain(flowGo.Mainnet) for _, coreContract := range sc.All() { @@ -222,13 +216,8 @@ func (di *DependencyInstaller) AddByCoreContractName(coreContractName, customNam return fmt.Errorf("contract %s not found in core contracts", coreContractName) } - name := depContractName - if customName != "" { - name = customName - } - dep := config.Dependency{ - Name: name, + Name: depContractName, Source: config.Source{ NetworkName: depNetwork, Address: flowsdk.HexToAddress(depAddress), From d77fc9fc309e17a486e2cd090e2bd0beb13c0616 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Thu, 21 Nov 2024 16:19:47 -0800 Subject: [PATCH 2/7] Remove too many args --- internal/dependencymanager/add.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/dependencymanager/add.go b/internal/dependencymanager/add.go index 2de6071e4..6cc72ab5a 100644 --- a/internal/dependencymanager/add.go +++ b/internal/dependencymanager/add.go @@ -81,7 +81,7 @@ func add( // First check if the dependency is a core contract. coreContractName := findCoreContractCaseInsensitive(dep) if coreContractName != "" { - if err := installer.AddByCoreContractName(coreContractName, addFlags.name); err != nil { + if err := installer.AddByCoreContractName(coreContractName); err != nil { logger.Error(fmt.Sprintf("Error: %v", err)) return nil, err } From 9111c65c28e9cd0581122fe925a4daf14d4290de Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Fri, 22 Nov 2024 12:13:40 -0800 Subject: [PATCH 3/7] Merge add with install --- internal/dependencymanager/add.go | 81 +++++---------------------- internal/dependencymanager/install.go | 53 ++++++++++++++++-- 2 files changed, 62 insertions(+), 72 deletions(-) diff --git a/internal/dependencymanager/add.go b/internal/dependencymanager/add.go index 6cc72ab5a..e31a0301c 100644 --- a/internal/dependencymanager/add.go +++ b/internal/dependencymanager/add.go @@ -20,88 +20,33 @@ package dependencymanager import ( "fmt" - "strings" - - "github.com/onflow/flow-go/fvm/systemcontracts" - - "github.com/spf13/cobra" - + "github.com/onflow/flow-cli/internal/util" "github.com/onflow/flowkit/v2" - "github.com/onflow/flowkit/v2/output" - - flowGo "github.com/onflow/flow-go/model/flow" + "github.com/spf13/cobra" "github.com/onflow/flow-cli/internal/command" - "github.com/onflow/flow-cli/internal/util" + "github.com/onflow/flowkit/v2/output" ) -type addFlagsCollection struct { - *Flags - name string -} - -var addFlags = addFlagsCollection{ - Flags: &Flags{}, -} - var addCommand = &command.Command{ Cmd: &cobra.Command{ - Use: "add ", - Short: "Add a single contract and its dependencies.", - Example: `flow dependencies add testnet://0afe396ebc8eee65.FlowToken -flow dependencies add FlowToken`, - Args: cobra.ExactArgs(1), + Use: "add", + Short: "This command has been deprecated.", + Long: "The 'add' command has been deprecated. Please use the 'install' command instead.", + Deprecated: "This command is deprecated. Use 'install' to manage dependencies.", }, RunS: add, Flags: &struct{}{}, } -func init() { - // Add common flags. - addFlags.Flags.AddToCommand(addCommand.Cmd) -} - func add( - args []string, + _ []string, _ command.GlobalFlags, logger output.Logger, - flow flowkit.Services, - state *flowkit.State, -) (result command.Result, err error) { - logger.Info(fmt.Sprintf("%s Installing dependencies for %s...", util.PrintEmoji("🔄"), args[0])) - - dep := args[0] - - installer, err := NewDependencyInstaller(logger, state, true, "", *addFlags.Flags) - if err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) - return nil, err - } - - // First check if the dependency is a core contract. - coreContractName := findCoreContractCaseInsensitive(dep) - if coreContractName != "" { - if err := installer.AddByCoreContractName(coreContractName); err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) - return nil, err - } - return nil, nil - } - - // Otherwise, add the dependency by source string. - if err := installer.AddBySourceString(dep); err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) - return nil, err - } - + _ flowkit.Services, + _ *flowkit.State, +) (command.Result, error) { + // Output a no-op message using the logger + logger.Info(fmt.Sprintf("%s The 'add' command has been deprecated. Please use 'install' instead.", util.PrintEmoji("⚠️"))) return nil, nil } - -func findCoreContractCaseInsensitive(name string) string { - for _, contract := range systemcontracts.SystemContractsForChain(flowGo.Mainnet).All() { - if strings.EqualFold(contract.Name, name) { - return contract.Name - } - } - return "" -} diff --git a/internal/dependencymanager/install.go b/internal/dependencymanager/install.go index ca6c3e42c..9c49a5336 100644 --- a/internal/dependencymanager/install.go +++ b/internal/dependencymanager/install.go @@ -20,6 +20,9 @@ package dependencymanager import ( "fmt" + "github.com/onflow/flow-go/fvm/systemcontracts" + flowGo "github.com/onflow/flow-go/model/flow" + "strings" "github.com/onflow/flow-cli/internal/util" @@ -35,21 +38,54 @@ var installFlags = Flags{} var installCommand = &command.Command{ Cmd: &cobra.Command{ - Use: "install", - Short: "Install contract and dependencies.", - Example: "flow dependencies install", + Use: "install", + Short: "Install contract and dependencies.", + Example: `flow dependencies install +flow dependencies install testnet://0afe396ebc8eee65.FlowToken +flow dependencies install FlowToken`, + Args: cobra.ArbitraryArgs, }, Flags: &installFlags, RunS: install, } func install( - _ []string, + args []string, _ command.GlobalFlags, logger output.Logger, flow flowkit.Services, state *flowkit.State, ) (result command.Result, err error) { + if len(args) > 0 { + logger.Info(fmt.Sprintf("%s Installing dependency %s...", util.PrintEmoji("🔄"), args[0])) + + dep := args[0] + + installer, err := NewDependencyInstaller(logger, state, true, "", installFlags) + if err != nil { + logger.Error(fmt.Sprintf("Error: %v", err)) + return nil, err + } + + // Check if the dependency is a core contract + coreContractName := findCoreContractCaseInsensitive(dep) + if coreContractName != "" { + if err := installer.AddByCoreContractName(coreContractName); err != nil { + logger.Error(fmt.Sprintf("Error: %v", err)) + return nil, err + } + return nil, nil + } + + // Otherwise, add the dependency by source string + if err := installer.AddBySourceString(dep); err != nil { + logger.Error(fmt.Sprintf("Error: %v", err)) + return nil, err + } + + return nil, nil + } + logger.Info(util.MessageWithEmojiPrefix("🔄", "Installing dependencies from flow.json...")) installer, err := NewDependencyInstaller(logger, state, true, "", installFlags) @@ -65,3 +101,12 @@ func install( return nil, nil } + +func findCoreContractCaseInsensitive(name string) string { + for _, contract := range systemcontracts.SystemContractsForChain(flowGo.Mainnet).All() { + if strings.EqualFold(contract.Name, name) { + return contract.Name + } + } + return "" +} From fe452b89cd2ed197792aa3daf9a48f369bdada42 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Fri, 22 Nov 2024 12:15:36 -0800 Subject: [PATCH 4/7] Fix tests --- internal/dependencymanager/dependencyinstaller_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/dependencymanager/dependencyinstaller_test.go b/internal/dependencymanager/dependencyinstaller_test.go index 2957d3ef7..571e38743 100644 --- a/internal/dependencymanager/dependencyinstaller_test.go +++ b/internal/dependencymanager/dependencyinstaller_test.go @@ -131,7 +131,7 @@ func TestDependencyInstallerAdd(t *testing.T) { } sourceStr := fmt.Sprintf("emulator://%s.%s", serviceAddress.String(), tests.ContractHelloString.Name) - err := di.AddBySourceString(sourceStr, "") + err := di.AddBySourceString(sourceStr) assert.NoError(t, err, "Failed to install dependencies") filePath := fmt.Sprintf("imports/%s/%s.cdc", serviceAddress.String(), tests.ContractHelloString.Name) @@ -215,7 +215,7 @@ func TestDependencyInstallerAdd(t *testing.T) { dependencies: make(map[string]config.Dependency), } - err := di.AddByCoreContractName("FlowToken", "") + err := di.AddByCoreContractName("FlowToken") assert.NoError(t, err, "Failed to install dependencies") filePath := fmt.Sprintf("imports/%s/%s.cdc", "1654653399040a61", "FlowToken") From a713b8f6436a834057208bfe89a5f9a5d94fe364 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Fri, 22 Nov 2024 12:30:58 -0800 Subject: [PATCH 5/7] Fix report --- .../dependencymanager/dependencyinstaller.go | 7 +-- internal/dependencymanager/install.go | 61 +++++++++++-------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/internal/dependencymanager/dependencyinstaller.go b/internal/dependencymanager/dependencyinstaller.go index 882df2c69..cae67bb78 100644 --- a/internal/dependencymanager/dependencyinstaller.go +++ b/internal/dependencymanager/dependencyinstaller.go @@ -147,6 +147,7 @@ func NewDependencyInstaller(logger output.Logger, state *flowkit.State, saveStat SkipDeployments: flags.skipDeployments, SkipAlias: flags.skipAlias, dependencies: make(map[string]config.Dependency), + logs: categorizedLogs{}, }, nil } @@ -176,8 +177,6 @@ func (di *DependencyInstaller) Install() error { return fmt.Errorf("error saving state: %w", err) } - di.logs.LogAll(di.Logger) - return nil } @@ -240,8 +239,6 @@ func (di *DependencyInstaller) Add(dep config.Dependency) error { return err } - di.logs.LogAll(di.Logger) - return nil } @@ -257,8 +254,6 @@ func (di *DependencyInstaller) AddMany(dependencies []config.Dependency) error { return err } - di.logs.LogAll(di.Logger) - return nil } diff --git a/internal/dependencymanager/install.go b/internal/dependencymanager/install.go index 9c49a5336..8916b261c 100644 --- a/internal/dependencymanager/install.go +++ b/internal/dependencymanager/install.go @@ -42,7 +42,8 @@ var installCommand = &command.Command{ Short: "Install contract and dependencies.", Example: `flow dependencies install flow dependencies install testnet://0afe396ebc8eee65.FlowToken -flow dependencies install FlowToken`, +flow dependencies install FlowToken +flow dependencies install FlowToken NonFungibleToken`, Args: cobra.ArbitraryArgs, }, Flags: &installFlags, @@ -56,49 +57,57 @@ func install( flow flowkit.Services, state *flowkit.State, ) (result command.Result, err error) { - if len(args) > 0 { - logger.Info(fmt.Sprintf("%s Installing dependency %s...", util.PrintEmoji("🔄"), args[0])) - - dep := args[0] + installer, err := NewDependencyInstaller(logger, state, true, "", installFlags) + if err != nil { + logger.Error(fmt.Sprintf("Error initializing dependency installer: %v", err)) + return nil, err + } - installer, err := NewDependencyInstaller(logger, state, true, "", installFlags) - if err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) - return nil, err - } + if len(args) > 0 { + for _, dep := range args { + logger.Info(fmt.Sprintf("%s Processing dependency %s...", util.PrintEmoji("🔄"), dep)) + + // Check if the dependency is a core contract + coreContractName := findCoreContractCaseInsensitive(dep) + if coreContractName != "" { + if err := installer.AddByCoreContractName(coreContractName); err != nil { + logger.Error(fmt.Sprintf("Error adding core contract %s: %v", coreContractName, err)) + return nil, err + } + continue + } - // Check if the dependency is a core contract - coreContractName := findCoreContractCaseInsensitive(dep) - if coreContractName != "" { - if err := installer.AddByCoreContractName(coreContractName); err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) + if err := installer.AddBySourceString(dep); err != nil { + if strings.Contains(err.Error(), "invalid dependency source format") { + logger.Error(fmt.Sprintf("Error: '%s' is neither a core contract nor a valid dependency source format.\nPlease provide a valid dependency source in the format 'network://address.ContractName', e.g., 'testnet://0x1234567890abcdef.MyContract', or use a valid core contract name such as 'FlowToken'.", dep)) + } else { + logger.Error(fmt.Sprintf("Error adding dependency %s: %v", dep, err)) + } return nil, err } - return nil, nil } - // Otherwise, add the dependency by source string - if err := installer.AddBySourceString(dep); err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) + logger.Info(util.MessageWithEmojiPrefix("🔄", "Installing added dependencies...")) + + if err := installer.Install(); err != nil { + logger.Error(fmt.Sprintf("Error installing dependencies: %v", err)) return nil, err } + installer.logs.LogAll(logger) + return nil, nil } logger.Info(util.MessageWithEmojiPrefix("🔄", "Installing dependencies from flow.json...")) - installer, err := NewDependencyInstaller(logger, state, true, "", installFlags) - if err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) - return nil, err - } - if err := installer.Install(); err != nil { - logger.Error(fmt.Sprintf("Error: %v", err)) + logger.Error(fmt.Sprintf("Error installing dependencies: %v", err)) return nil, err } + installer.logs.LogAll(logger) + return nil, nil } From 57fcf30c479a9db9741622021aff68b375f533b8 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Fri, 22 Nov 2024 12:35:33 -0800 Subject: [PATCH 6/7] Remove comment --- internal/dependencymanager/add.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/dependencymanager/add.go b/internal/dependencymanager/add.go index e31a0301c..98daa0701 100644 --- a/internal/dependencymanager/add.go +++ b/internal/dependencymanager/add.go @@ -46,7 +46,6 @@ func add( _ flowkit.Services, _ *flowkit.State, ) (command.Result, error) { - // Output a no-op message using the logger logger.Info(fmt.Sprintf("%s The 'add' command has been deprecated. Please use 'install' instead.", util.PrintEmoji("⚠️"))) return nil, nil } From ffc5d9a0d9090c68b8b913f3affa60984c8dcdb3 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Fri, 22 Nov 2024 12:38:23 -0800 Subject: [PATCH 7/7] Run lint fix --- internal/dependencymanager/add.go | 7 +++++-- internal/dependencymanager/install.go | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/dependencymanager/add.go b/internal/dependencymanager/add.go index 98daa0701..081ad07bc 100644 --- a/internal/dependencymanager/add.go +++ b/internal/dependencymanager/add.go @@ -20,12 +20,15 @@ package dependencymanager import ( "fmt" - "github.com/onflow/flow-cli/internal/util" + "github.com/onflow/flowkit/v2" "github.com/spf13/cobra" - "github.com/onflow/flow-cli/internal/command" + "github.com/onflow/flow-cli/internal/util" + "github.com/onflow/flowkit/v2/output" + + "github.com/onflow/flow-cli/internal/command" ) var addCommand = &command.Command{ diff --git a/internal/dependencymanager/install.go b/internal/dependencymanager/install.go index 8916b261c..87e648aa5 100644 --- a/internal/dependencymanager/install.go +++ b/internal/dependencymanager/install.go @@ -20,9 +20,10 @@ package dependencymanager import ( "fmt" + "strings" + "github.com/onflow/flow-go/fvm/systemcontracts" flowGo "github.com/onflow/flow-go/model/flow" - "strings" "github.com/onflow/flow-cli/internal/util"