-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
102 lines (83 loc) · 1.94 KB
/
main.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
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/manifoldco/promptui/list"
"github.com/magefile/mage/mage"
"github.com/manifoldco/promptui"
)
const (
maxSize = 10
)
var (
mageVersion = "<unknown>"
timestamp = "<unknown>"
hash = "<unknown>"
tag = "<unknown>"
)
func searcher(targets []string) list.Searcher {
return func(input string, index int) bool {
if strings.Contains(strings.ToLower(targets[index]), input) {
return true
}
return false
}
}
func main() {
if len(os.Args) > 1 {
if os.Args[1] == "-version" {
fmt.Printf("mage-select CLI frontend: %s\nBuild Date: %s\nCommit: %s\nMage: %s\n", tag, timestamp, hash, mageVersion)
os.Exit(0)
}
}
cmd := exec.Command("mage", "-l")
cmd.Stderr = os.Stderr
cmdEnviron := []string{}
cmdEnviron = append(cmdEnviron, cmd.Environ()...)
cmdEnviron = append(cmdEnviron, "MAGEFILE_ENABLE_COLOR=0")
cmd.Env = cmdEnviron
out, err := cmd.Output()
if err != nil {
return
}
scan := bufio.NewScanner(bytes.NewBuffer(out))
targets := []string{}
for scan.Scan() {
line := scan.Text()
if strings.HasPrefix(line, "Targets:") {
continue
}
line = strings.TrimSpace(line)
targets = append(targets, line)
}
templates := &promptui.SelectTemplates{
Label: "{{.}}",
Active: promptui.IconSelect + " {{.}}",
Inactive: " {{.|faint}}",
Selected: promptui.IconGood + " {{.}}",
}
size := maxSize
if len(targets) < size {
size = len(targets)
}
prompt := promptui.Select{
Label: "Select a mage target:",
Items: targets,
Templates: templates,
HideHelp: true,
Size: size,
Searcher: searcher(targets),
StartInSearchMode: true,
}
_, result, err := prompt.Run()
if err != nil {
return
}
result = strings.Split(result, " ")[0]
fmt.Printf("mage %s\n", result)
os.Exit(mage.ParseAndRun(os.Stdout, os.Stderr, os.Stdin, []string{result}))
}