This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
depends.go
108 lines (88 loc) · 2.16 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
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"strings"
"github.com/go2c/optparse"
"github.com/onodera-punpun/prt/ports"
)
// dependsCommand lists dependencies recursively.
func dependsCommand(input []string) error {
// Define valid arguments.
o := optparse.New()
arga := o.Bool("all", 'a', false)
argn := o.Bool("no-alias", 'n', false)
argt := o.Bool("tree", 't', false)
argh := o.Bool("help", 'h', false)
// Parse arguments.
_, err := o.Parse(input)
if err != nil {
return fmt.Errorf("invaild argument, use `-h` for a list of arguments")
}
// Print help.
if *argh {
fmt.Println("Usage: prt depends [arguments]")
fmt.Println("")
fmt.Println("arguments:")
fmt.Println(" -a, --all also list installed dependencies")
fmt.Println(" -n, --no-alias disable aliasing")
fmt.Println(" -t, --tree list using tree view")
fmt.Println(" -h, --help print help and exit")
return nil
}
p := ports.New(".")
if err := p.Pkgfile.Parse(); err != nil {
return err
}
var db ports.Database
if !*arga {
// Get installed ports.
if err := db.Parse(); err != nil {
return err
}
}
// Get all ports.
all, err := ports.All()
if err != nil {
return err
}
if err := p.ParseDepends(all, !*argn); err != nil {
return err
}
dependsRecurse(&p, db, 0, *argt)
return nil
}
var dependsCheck []*ports.Port
var dependsArrow bool
func dependsRecurse(p *ports.Port, db ports.Database, l int, t bool) {
outer:
for _, d := range p.Depends {
// Continue if installed.
for _, n := range db.Packages {
if d.Pkgfile.Name == n.Name {
continue outer
}
}
// Continue if already checked.
for _, c := range dependsCheck {
if d.Pkgfile.Name == c.Pkgfile.Name {
if !t {
continue outer
}
if len(d.Pkgfile.Depends) > 0 {
if !dependsArrow {
fmt.Printf("%s%s%s\n", dark(strings.Repeat(config.
IndentChar, l)), d.Pkgfile.Name, dark(" ->"))
continue outer
}
dependsArrow = !dependsArrow
}
}
}
dependsCheck = append(dependsCheck, d)
if t {
fmt.Print(dark(strings.Repeat(config.IndentChar, l)))
}
fmt.Println(d.Pkgfile.Name)
dependsRecurse(d, db, l+1, t)
}
}