|
| 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 dependencymanager |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + |
| 27 | + "github.com/onflow/flowkit/v2" |
| 28 | + "github.com/onflow/flowkit/v2/output" |
| 29 | + |
| 30 | + "github.com/onflow/flow-cli/common/branding" |
| 31 | + "github.com/onflow/flow-cli/internal/command" |
| 32 | +) |
| 33 | + |
| 34 | +type ListResult struct { |
| 35 | + Dependencies []DependencyInfo `json:"dependencies"` |
| 36 | +} |
| 37 | + |
| 38 | +type DependencyInfo struct { |
| 39 | + Name string `json:"name"` |
| 40 | + NetworkName string `json:"network"` |
| 41 | + Address string `json:"address"` |
| 42 | + Contract string `json:"contract"` |
| 43 | +} |
| 44 | + |
| 45 | +var listCommand = &command.Command{ |
| 46 | + Cmd: &cobra.Command{ |
| 47 | + Use: "list", |
| 48 | + Short: "List installed dependencies", |
| 49 | + Example: "flow dependencies list", |
| 50 | + Args: cobra.NoArgs, |
| 51 | + }, |
| 52 | + RunS: list, |
| 53 | + Flags: &struct{}{}, |
| 54 | +} |
| 55 | + |
| 56 | +func list( |
| 57 | + _ []string, |
| 58 | + globalFlags command.GlobalFlags, |
| 59 | + logger output.Logger, |
| 60 | + flow flowkit.Services, |
| 61 | + state *flowkit.State, |
| 62 | +) (command.Result, error) { |
| 63 | + installedDeps := state.Dependencies() |
| 64 | + if installedDeps == nil || len(*installedDeps) == 0 { |
| 65 | + return &ListResult{Dependencies: []DependencyInfo{}}, nil |
| 66 | + } |
| 67 | + |
| 68 | + var dependencies []DependencyInfo |
| 69 | + for _, dep := range *installedDeps { |
| 70 | + dependencies = append(dependencies, DependencyInfo{ |
| 71 | + Name: dep.Name, |
| 72 | + NetworkName: dep.Source.NetworkName, |
| 73 | + Address: dep.Source.Address.String(), |
| 74 | + Contract: dep.Source.ContractName, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + return &ListResult{Dependencies: dependencies}, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (r *ListResult) String() string { |
| 82 | + if len(r.Dependencies) == 0 { |
| 83 | + return branding.GrayStyle.Render("📦 No dependencies installed") |
| 84 | + } |
| 85 | + |
| 86 | + var result strings.Builder |
| 87 | + |
| 88 | + header := fmt.Sprintf("📦 Installed dependencies (%d):", len(r.Dependencies)) |
| 89 | + result.WriteString(branding.PurpleStyle.Render(header) + "\n\n") |
| 90 | + |
| 91 | + // Find max widths for alignment |
| 92 | + maxNameWidth := 4 // "NAME" |
| 93 | + maxNetworkWidth := 7 // "NETWORK" |
| 94 | + maxAddressWidth := 7 // "ADDRESS" |
| 95 | + |
| 96 | + for _, dep := range r.Dependencies { |
| 97 | + if len(dep.Name) > maxNameWidth { |
| 98 | + maxNameWidth = len(dep.Name) |
| 99 | + } |
| 100 | + if len(dep.NetworkName) > maxNetworkWidth { |
| 101 | + maxNetworkWidth = len(dep.NetworkName) |
| 102 | + } |
| 103 | + if len(dep.Address) > maxAddressWidth { |
| 104 | + maxAddressWidth = len(dep.Address) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + result.WriteString(fmt.Sprintf("%s %s %s %s\n", |
| 109 | + branding.GreenStyle.Render(fmt.Sprintf("%-*s", maxNameWidth, "NAME")), |
| 110 | + branding.GreenStyle.Render(fmt.Sprintf("%-*s", maxNetworkWidth, "NETWORK")), |
| 111 | + branding.GreenStyle.Render(fmt.Sprintf("%-*s", maxAddressWidth, "ADDRESS")), |
| 112 | + branding.GreenStyle.Render("CONTRACT"))) |
| 113 | + |
| 114 | + result.WriteString(branding.GrayStyle.Render(strings.Repeat("─", maxNameWidth+maxNetworkWidth+maxAddressWidth+20)) + "\n") |
| 115 | + |
| 116 | + for _, dep := range r.Dependencies { |
| 117 | + |
| 118 | + contractName := branding.GreenStyle.Render(fmt.Sprintf("%-*s", maxNameWidth, dep.Name)) |
| 119 | + network := branding.PurpleStyle.Render(fmt.Sprintf("%-*s", maxNetworkWidth, dep.NetworkName)) |
| 120 | + address := branding.GrayStyle.Render(fmt.Sprintf("%-*s", maxAddressWidth, dep.Address)) |
| 121 | + contract := dep.Contract |
| 122 | + |
| 123 | + result.WriteString(fmt.Sprintf("%s %s %s %s\n", |
| 124 | + contractName, network, address, contract)) |
| 125 | + } |
| 126 | + |
| 127 | + return result.String() |
| 128 | +} |
| 129 | + |
| 130 | +func (r *ListResult) Oneliner() string { |
| 131 | + return fmt.Sprintf("Found %d installed dependencies", len(r.Dependencies)) |
| 132 | +} |
| 133 | + |
| 134 | +func (r *ListResult) JSON() any { |
| 135 | + return r |
| 136 | +} |
0 commit comments