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
/
loc.go
93 lines (78 loc) · 1.75 KB
/
loc.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
package main
import (
"fmt"
"strings"
"github.com/go2c/optparse"
"github.com/onodera-punpun/go-utils/array"
"github.com/onodera-punpun/prt/ports"
)
// locCommand prints port locations
func locCommand(input []string) error {
// Define valid arguments.
o := optparse.New()
argd := o.Bool("duplicate", 'd', false)
argn := o.Bool("no-alias", 'n', 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 loc [arguments] [ports]")
fmt.Println("")
fmt.Println("arguments:")
fmt.Println(" -d, --duplicate list duplicate ports as well")
fmt.Println(" -n, --no-alias disable aliasing")
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 port")
}
// Get all ports.
all, err := ports.All()
if err != nil {
return err
}
var check []string
var i int
for _, v := range vals {
// Continue if already checked.
if array.ContainsString(check, v) {
continue
}
check = append(check, v)
pl, err := ports.Locate(all, v)
if err != nil {
return err
}
if !*argd {
pl = []ports.Port{pl[0]}
}
var op string
for _, p := range pl {
// Alias if needed.
if !*argn {
p.Alias()
}
// Print duplicate indentation.
if *argd {
// Reset indentation on new port
if p.Location.Port != op {
i = 0
}
op = p.Location.Port
if i > 0 {
fmt.Print(dark(strings.Repeat(config.IndentChar, i)))
}
i++
}
// Finally print the port.
fmt.Println(p.Location.Base())
}
}
return nil
}