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+ }
0 commit comments