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
/
prov.go
107 lines (91 loc) · 2.01 KB
/
prov.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
package main
import (
"fmt"
"regexp"
"github.com/go2c/optparse"
"github.com/onodera-punpun/prt/ports"
)
// provCommand searches ports for files.
func provCommand(input []string) error {
// Define valid arguments.
o := optparse.New()
argi := o.Bool("installed", 'i', false)
argh := o.Bool("help", 'h', false)
// Parse arguments.
vals, 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 prov [arguments] [queries]")
fmt.Println("")
fmt.Println("arguments:")
fmt.Println(" -i, --installed search in installed ports")
fmt.Println(" -h, --help print help and exit")
return nil
}
// This command needs a value.
if len(vals) == 0 {
return fmt.Errorf("please specify a query")
}
for _, v := range vals {
if *argi {
var db ports.Database
if err := db.Parse(); err != nil {
return err
}
for _, p := range db.Packages {
// Search for files.
var fl []string
for _, f := range p.Files {
m, err := regexp.MatchString(v, f)
if err != nil {
return err
}
if m {
fl = append(fl, f)
}
}
// Print port location.
if len(fl) > 0 {
fmt.Println(p.Name)
}
// Print matched files.
for _, f := range fl {
fmt.Printf("%s%s\n", dark(config.IndentChar), f)
}
}
} else {
all, err := ports.All()
if err != nil {
return err
}
for _, p := range all {
if err := p.Footprint.Parse(); err != nil {
continue
}
// Search for files.
var fl []string
for _, f := range p.Footprint.Files {
m, err := regexp.MatchString(v, f.Path)
if err != nil {
return err
}
if m {
fl = append(fl, f.Path)
}
}
// Print port location.
if len(fl) > 0 {
fmt.Println(p.Location.Base())
}
// Print matched files.
for _, f := range fl {
fmt.Printf("%s%s\n", dark(config.IndentChar), f)
}
}
}
}
return nil
}