Skip to content

Commit

Permalink
refactor AddressPromptOrEmpty, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bartolomej committed Jun 29, 2024
1 parent b6a7c3a commit 50e389c
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions deps/dependencyinstaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,16 @@ type DeploymentData struct {
}

type Prompter interface {
// ShouldUpdateDependency prompts the user if the dependency should be updated in flow configuration
ShouldUpdateDependency(contractName string) bool
// AddContractToDeployment prompts the user to select an account to deploy a given contract on a given network
AddContractToDeployment(networkName string, accounts accounts.Accounts, contractName string) *DeploymentData
AddressPromptOrEmpty(label, errorMessage string) string
// AddressPromptOrEmpty prompts the user to enter a flow address
AddressPromptOrEmpty(label string, validate InputValidator) string
}

type InputValidator func(string) error

type Flags struct {
SkipDeployments bool
SkipAlias bool
Expand Down Expand Up @@ -529,7 +534,7 @@ func (di *DependencyInstaller) handleAdditionalDependencyTasks(networkName, cont
func (di *DependencyInstaller) updateDependencyDeployment(contractName string) error {
// Add to deployments
// If a deployment already exists for that account, contract, and network, then ignore
raw := di.prompter.AddContractToDeployment("emulator", *di.State.Accounts(), contractName)
raw := di.prompter.AddContractToDeployment(config.EmulatorNetwork.Name, *di.State.Accounts(), contractName)

if raw != nil {
deployment := di.State.Deployments().ByAccountAndNetwork(raw.Account, raw.Network)
Expand Down Expand Up @@ -559,7 +564,7 @@ func (di *DependencyInstaller) updateDependencyAlias(contractName, aliasNetwork
}

label := fmt.Sprintf("Enter an alias address for %s on %s if you have one, otherwise leave blank", contractName, missingNetwork)
raw := di.prompter.AddressPromptOrEmpty(label, "Invalid alias address")
raw := di.prompter.AddressPromptOrEmpty(label, addressValidator)

if raw != "" {
contract, err := di.State.Contracts().ByName(contractName)
Expand All @@ -573,6 +578,16 @@ func (di *DependencyInstaller) updateDependencyAlias(contractName, aliasNetwork
return nil
}

func addressValidator(address string) error {
if address == "" {
return nil
}
if flowsdk.HexToAddress(address) == flowsdk.EmptyAddress {
return fmt.Errorf("invalid alias address")
}
return nil
}

func (di *DependencyInstaller) updateDependencyState(networkName, contractAddress, assignedName, contractName, contractHash string) error {
dep := config.Dependency{
Name: assignedName,
Expand Down

0 comments on commit 50e389c

Please sign in to comment.