/
depends.go
100 lines (83 loc) · 2.59 KB
/
depends.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"strings"
"github.com/Bios-Marcel/spoon/internal/cli"
"github.com/Bios-Marcel/spoon/pkg/scoop"
jsoniter "github.com/json-iterator/go"
"github.com/spf13/cobra"
)
func dependsCmd() *cobra.Command {
cmd := &cobra.Command{
// TODO USage
Use: "depends {app}",
Short: "Show dependency tree or reverse dependency tree of an app",
Example: cli.FormatUsageExample(
"spoon depends poetry",
"spoon depends -r python",
),
Aliases: []string{"depend", "needs", "need"},
Args: cobra.ExactArgs(1),
ValidArgsFunction: autocompleteAvailable,
RunE: RunE(func(cmd *cobra.Command, args []string) error {
defaultScoop, err := scoop.NewScoop()
if err != nil {
return fmt.Errorf("error getting default scoop: %w", err)
}
app, err := defaultScoop.GetAvailableApp(args[0])
if err != nil {
return fmt.Errorf("error looking up app: %w", err)
}
if app == nil {
return fmt.Errorf("app '%s' doesn't exist", args[0])
}
iter := jsoniter.Parse(jsoniter.ConfigFastest, nil, 1024*128)
if err := app.LoadDetailsWithIter(iter, scoop.DetailFieldDepends); err != nil {
return fmt.Errorf("error loading app details: %w", err)
}
reverse := must(cmd.Flags().GetBool("reverse"))
// TODOs
// 1. Fancy print tree (Ascii tree guides)
// 2. JSON
// 3. Multiarg
// 4. Speed up
if reverse {
buckets, err := defaultScoop.GetLocalBuckets()
if err != nil {
return fmt.Errorf("error getting buckets: %w", err)
}
var apps []*scoop.App
for _, bucket := range buckets {
bucketApps, err := bucket.AvailableApps()
if err != nil {
return fmt.Errorf("error getting available apps for bucket: %w", err)
}
for _, app := range bucketApps {
if err := app.LoadDetailsWithIter(iter, scoop.DetailFieldDepends); err != nil {
return fmt.Errorf("error loading app details: %w", err)
}
}
apps = append(apps, bucketApps...)
}
tree := defaultScoop.ReverseDependencyTree(apps, app)
printDeps(0, tree)
} else {
tree, err := defaultScoop.DependencyTree(app)
if err != nil {
return fmt.Errorf("error building dependency tree: %w", err)
}
printDeps(0, tree)
}
return nil
}),
}
cmd.Flags().BoolP("reverse", "r", false, "Reverse the direction we retrieve dependencies")
return cmd
}
func printDeps(indent int, dependencies *scoop.Dependencies) {
indentStr := strings.Repeat(" ", indent)
fmt.Println(indentStr + dependencies.App.Name)
for _, dependencies := range dependencies.Values {
printDeps(indent+1, dependencies)
}
}