Skip to content

Commit 8dedaa6

Browse files
T4Buclaude
andcommitted
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>
1 parent 3cb4c78 commit 8dedaa6

3 files changed

Lines changed: 148 additions & 1 deletion

File tree

internal/config/add-alias.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
26+
"github.com/onflow/flow-go-sdk"
27+
"github.com/spf13/cobra"
28+
29+
"github.com/onflow/flowkit/v2"
30+
"github.com/onflow/flowkit/v2/output"
31+
32+
"github.com/onflow/flow-cli/internal/command"
33+
)
34+
35+
type flagsAddAlias struct {
36+
Contract string `flag:"contract" info:"Name of the contract to add alias for"`
37+
Network string `flag:"network" info:"Network name for the alias"`
38+
Address string `flag:"address" info:"Address for the alias"`
39+
}
40+
41+
var addAliasFlags = flagsAddAlias{}
42+
43+
var addAliasCommand = &command.Command{
44+
Cmd: &cobra.Command{
45+
Use: "alias",
46+
Short: "Add alias to contract configuration",
47+
Example: "flow config add alias --contract MyContract --network testnet --address 0x1234567890abcdef",
48+
Args: cobra.NoArgs,
49+
},
50+
Flags: &addAliasFlags,
51+
RunS: addAlias,
52+
}
53+
54+
func addAlias(
55+
_ []string,
56+
globalFlags command.GlobalFlags,
57+
_ output.Logger,
58+
_ flowkit.Services,
59+
state *flowkit.State,
60+
) (command.Result, error) {
61+
raw, flagsProvided, err := flagsToAliasData(addAliasFlags)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
if !flagsProvided {
67+
raw = prompt.NewAliasPrompt()
68+
}
69+
70+
contract, err := state.Contracts().ByName(raw.Contract)
71+
if err != nil {
72+
return nil, fmt.Errorf("contract %s not found in configuration: %w", raw.Contract, err)
73+
}
74+
75+
contract.Aliases.Add(
76+
raw.Network,
77+
flow.HexToAddress(raw.Address),
78+
)
79+
80+
state.Contracts().AddOrUpdate(*contract)
81+
82+
err = state.SaveEdited(globalFlags.ConfigPaths)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
return &result{
88+
result: fmt.Sprintf("Alias for contract %s on network %s added to the configuration", raw.Contract, raw.Network),
89+
}, nil
90+
}
91+
92+
func flagsToAliasData(flags flagsAddAlias) (*prompt.AliasData, bool, error) {
93+
if flags.Contract == "" && flags.Network == "" && flags.Address == "" {
94+
return nil, false, nil
95+
}
96+
97+
if flags.Contract == "" {
98+
return nil, true, fmt.Errorf("contract name must be provided")
99+
}
100+
101+
if flags.Network == "" {
102+
return nil, true, fmt.Errorf("network name must be provided")
103+
}
104+
105+
if flags.Address == "" {
106+
return nil, true, fmt.Errorf("address must be provided")
107+
}
108+
109+
if flow.HexToAddress(flags.Address) == flow.EmptyAddress {
110+
return nil, true, fmt.Errorf("invalid address")
111+
}
112+
113+
return &prompt.AliasData{
114+
Contract: flags.Contract,
115+
Network: flags.Network,
116+
Address: flags.Address,
117+
}, true, nil
118+
}

internal/config/add.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
var addCmd = &cobra.Command{
26-
Use: "add <account|contract|deployment|network>",
26+
Use: "add <account|contract|deployment|network|alias>",
2727
Short: "Add resource to configuration",
2828
Example: "flow config add account",
2929
Args: cobra.ExactArgs(1),
@@ -32,6 +32,7 @@ var addCmd = &cobra.Command{
3232

3333
func init() {
3434
addAccountCommand.AddToParent(addCmd)
35+
addAliasCommand.AddToParent(addCmd)
3536
addContractCommand.AddToParent(addCmd)
3637
addDeploymentCommand.AddToParent(addCmd)
3738
addNetworkCommand.AddToParent(addCmd)

internal/prompt/prompt.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,34 @@ func NewNetworkPrompt() map[string]string {
374374
return networkData
375375
}
376376

377+
func NewAliasPrompt() *AliasData {
378+
alias := &AliasData{
379+
Contract: NamePrompt(),
380+
}
381+
382+
alias.Network, _ = RunTextInputWithValidation(
383+
"Enter network name",
384+
"testnet",
385+
"",
386+
func(s string) error {
387+
if len(s) < 1 {
388+
return fmt.Errorf("network name cannot be empty")
389+
}
390+
return nil
391+
},
392+
)
393+
394+
alias.Address = addressPrompt("Enter address for alias", "invalid address", false)
395+
396+
return alias
397+
}
398+
399+
type AliasData struct {
400+
Contract string
401+
Network string
402+
Address string
403+
}
404+
377405
type DeploymentData struct {
378406
Network string
379407
Account string

0 commit comments

Comments
 (0)