forked from getantibody/antibody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (100 loc) · 3.1 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
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"text/tabwriter"
"github.com/alecthomas/kingpin"
"github.com/getantibody/antibody/antibodylib"
"github.com/getantibody/antibody/project"
"github.com/getantibody/antibody/shell"
"github.com/getantibody/folder"
"golang.org/x/crypto/ssh/terminal"
)
// nolint: gochecknoglobals
var (
version = "dev"
app = kingpin.New("antibody", "The fastest shell plugin manager")
parallelism = app.Flag("parallelism", "max amount of tasks to launch in parallel").
Short('p').
Default(strconv.Itoa(runtime.NumCPU())).
Int()
bundleCmd = app.Command("bundle", "downloads a bundle and prints its source line")
bundles = bundleCmd.Arg("bundles", "bundle list").Strings()
updateCmd = app.Command("update", "updates all previously bundled bundles")
homeCmd = app.Command("home", "prints where antibody is cloning the bundles")
purgeCmd = app.Command("purge", "purges a bundle from your computer")
purgee = purgeCmd.Arg("bundle", "bundle to be purged").Required().String()
listCmd = app.Command("list", "lists all currently installed bundles").Alias("ls")
initCmd = app.Command("init", "initializes the shell so Antibody can work as expected")
)
// nolint: gochecknoinits
func init() {
log.SetOutput(os.Stderr)
log.SetPrefix("antibody: ")
log.SetFlags(0)
}
func main() {
app.Author("Carlos Alexandro Becker <[email protected]>")
app.Version("antibody version " + version)
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case bundleCmd.FullCommand():
bundle()
case updateCmd.FullCommand():
update()
case homeCmd.FullCommand():
fmt.Println(antibodylib.Home())
case purgeCmd.FullCommand():
purge()
case listCmd.FullCommand():
list()
case initCmd.FullCommand():
sh, err := shell.Init()
app.FatalIfError(err, "failed to init")
fmt.Println(sh)
}
}
func bundle() {
var input io.Reader
if !terminal.IsTerminal(int(os.Stdin.Fd())) && len(*bundles) == 0 {
input = os.Stdin
} else {
input = bytes.NewBufferString(strings.Join(*bundles, " "))
}
sh, err := antibodylib.New(antibodylib.Home(), input, *parallelism).Bundle()
app.FatalIfError(err, "failed to bundle")
fmt.Println(sh)
}
func update() {
var home = antibodylib.Home()
fmt.Printf("Updating all bundles in %v...\n", home)
var err = project.Update(home, *parallelism)
app.FatalIfError(err, "failed to update")
}
func purge() {
fmt.Printf("Removing %s...\n", *purgee)
var path = project.New(antibodylib.Home(), *purgee).Path()
if _, err := os.Stat(path); os.IsNotExist(err) {
app.Fatalf("%s does not exist on expected location: %s", *purgee, path)
}
app.FatalIfError(os.RemoveAll(path), "failed to purge")
fmt.Println("removed!")
}
func list() {
home := antibodylib.Home()
projects, err := project.List(home)
app.FatalIfError(err, "failed to list bundles")
w := tabwriter.NewWriter(os.Stdout, 0, 1, 4, ' ', tabwriter.TabIndent)
for _, b := range projects {
fmt.Fprintf(w, "%s\t%s\n", folder.ToURL(b), filepath.Join(home, b))
}
app.FatalIfError(w.Flush(), "failed to flush")
}