Skip to content

Commit 0e7b990

Browse files
T4Buclaudejribbink
authored
Add alias configuration command (#2114)
* Created a new add-alias option for flow config. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * creates a new add-alias_test * Validate network addresses * Switch to RPC chain ID validation --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Jordan Ribbink <jribbink@telus.net>
1 parent e7ae0ce commit 0e7b990

5 files changed

Lines changed: 590 additions & 6 deletions

File tree

internal/config/add-alias.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Flow CLI
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package config
20+
21+
import (
22+
"fmt"
23+
24+
"github.com/onflow/flow-cli/internal/prompt"
25+
"github.com/onflow/flow-cli/internal/util"
26+
27+
flow "github.com/onflow/flow-go-sdk"
28+
"github.com/spf13/cobra"
29+
30+
"github.com/onflow/flowkit/v2"
31+
"github.com/onflow/flowkit/v2/output"
32+
33+
"github.com/onflow/flow-cli/internal/command"
34+
)
35+
36+
type flagsAddAlias struct {
37+
Contract string `flag:"contract" info:"Name of the contract to add alias for"`
38+
Network string `flag:"network" info:"Network name for the alias"`
39+
Address string `flag:"address" info:"Address for the alias"`
40+
}
41+
42+
var addAliasFlags = flagsAddAlias{}
43+
44+
var addAliasCommand = &command.Command{
45+
Cmd: &cobra.Command{
46+
Use: "alias",
47+
Short: "Add alias to contract configuration",
48+
Example: "flow config add alias --contract MyContract --network testnet --address 0x1234567890abcdef",
49+
Args: cobra.NoArgs,
50+
},
51+
Flags: &addAliasFlags,
52+
RunS: addAlias,
53+
}
54+
55+
func addAlias(
56+
_ []string,
57+
globalFlags command.GlobalFlags,
58+
_ output.Logger,
59+
flowServices flowkit.Services,
60+
state *flowkit.State,
61+
) (command.Result, error) {
62+
raw, flagsProvided, err := flagsToAliasData(addAliasFlags, state)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
if !flagsProvided {
68+
raw = prompt.NewAliasPrompt()
69+
err = validateAliasData(raw, state)
70+
if err != nil {
71+
return nil, err
72+
}
73+
}
74+
75+
contract, err := state.Contracts().ByName(raw.Contract)
76+
if err != nil {
77+
return nil, fmt.Errorf("contract %s not found in configuration: %w", raw.Contract, err)
78+
}
79+
80+
contract.Aliases.Add(
81+
raw.Network,
82+
flow.HexToAddress(raw.Address),
83+
)
84+
85+
state.Contracts().AddOrUpdate(*contract)
86+
87+
err = state.SaveEdited(globalFlags.ConfigPaths)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
return &result{
93+
result: fmt.Sprintf("Alias for contract %s on network %s added to the configuration", raw.Contract, raw.Network),
94+
}, nil
95+
}
96+
97+
func validateAliasData(data *prompt.AliasData, state *flowkit.State) error {
98+
address := flow.HexToAddress(data.Address)
99+
if address == flow.EmptyAddress {
100+
return fmt.Errorf("invalid address")
101+
}
102+
103+
network, err := state.Networks().ByName(data.Network)
104+
if err != nil {
105+
return fmt.Errorf("network %s not found in configuration", data.Network)
106+
}
107+
108+
return util.ValidateAddressForNetwork(address, network)
109+
}
110+
111+
func flagsToAliasData(flags flagsAddAlias, state *flowkit.State) (*prompt.AliasData, bool, error) {
112+
if flags.Contract == "" && flags.Network == "" && flags.Address == "" {
113+
return nil, false, nil
114+
}
115+
116+
if flags.Contract == "" {
117+
return nil, true, fmt.Errorf("contract name must be provided")
118+
}
119+
120+
if flags.Network == "" {
121+
return nil, true, fmt.Errorf("network name must be provided")
122+
}
123+
124+
if flags.Address == "" {
125+
return nil, true, fmt.Errorf("address must be provided")
126+
}
127+
128+
data := &prompt.AliasData{
129+
Contract: flags.Contract,
130+
Network: flags.Network,
131+
Address: flags.Address,
132+
}
133+
134+
err := validateAliasData(data, state)
135+
if err != nil {
136+
return nil, true, err
137+
}
138+
139+
return data, true, nil
140+
}

0 commit comments

Comments
 (0)