Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Commit

Permalink
rewrite 2/N: cmd/bundle/antibody
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Sep 5, 2015
1 parent 381e222 commit 4331f04
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
@@ -0,0 +1,14 @@
root = true

[*]
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8

[*.go]
indent_style = tab
indent_size = 4
61 changes: 61 additions & 0 deletions antibody.go
@@ -0,0 +1,61 @@
package antibody

import (
"fmt"
"os"
"sync"

"github.com/caarlos0/gohome"
)

// Antibody wraps a list of bundles to be processed.
type Antibody struct {
bundles []Bundle
}

type bundleAction func(bundle Bundle)

// New creates an instance of antibody with the given bundles.
func New(bundles []Bundle) Antibody {
return Antibody{
bundles: bundles,
}
}

// Download the needed bundles.
func (a Antibody) Download() {
a.forEach(func(b Bundle) {
b.Download()
})
}

// Update all bundles.
func (a Antibody) Update() {
a.forEach(func(b Bundle) {
b.Update()
})
}

func (a Antibody) forEach(action bundleAction) {
var wg sync.WaitGroup
for _, bundle := range a.bundles {
wg.Add(1)
go func(bundle Bundle, action bundleAction) {
action(bundle)
for _, sourceable := range bundle.Sourceables() {
fmt.Println(sourceable)
}
wg.Done()
}(bundle, action)
}
wg.Wait()
}

// Home finds the right home folder to use
func Home() string {
home := os.Getenv("ANTIBODY_HOME")
if home == "" {
return gohome.Cache("antibody")
}
return home
}
31 changes: 31 additions & 0 deletions bundle.go
@@ -0,0 +1,31 @@
package antibody

import (
"path/filepath"

"github.com/caarlos0/antibody/git"
)

// Bundle is a git-based bundle/plugin
type Bundle struct {
git.Repo
}

// NewBundle creates a new bundle instance
func NewBundle(bundle, home string) Bundle {
return Bundle{
git.NewGithubRepo(bundle, home),
}
}

// Sourceables returns the list of files that could be sourced
func (b Bundle) Sourceables() []string {
globs := []string{"*.plugin.zsh", "*.zsh", "*.sh"}
for _, glob := range globs {
files, _ := filepath.Glob(filepath.Join(b.Folder(), glob))
if files != nil {
return files
}
}
return nil
}
42 changes: 42 additions & 0 deletions cmd/antibody/actions/bundle.go
@@ -0,0 +1,42 @@
package actions

import (
"io"
"io/ioutil"
"os"
"strings"

"github.com/caarlos0/antibody"
"github.com/codegangsta/cli"
)

// Bundle download all given bundles (stdin or args)
func Bundle(c *cli.Context) {
if readFromStdin() {
processStdin(os.Stdin)
} else {
antibody.New(
[]antibody.Bundle{
antibody.NewBundle(c.Args().First(), antibody.Home()),
},
).Download()
}
}

func readFromStdin() bool {
stat, _ := os.Stdin.Stat()
return (stat.Mode() & os.ModeCharDevice) == 0
}

func processStdin(stdin io.Reader) {
home := antibody.Home()
entries, _ := ioutil.ReadAll(stdin)
var bundles []antibody.Bundle
for _, bundle := range strings.Split(string(entries), "\n") {
if bundle == "" {
continue
}
bundles = append(bundles, antibody.NewBundle(bundle, home))
}
antibody.New(bundles).Download()
}
31 changes: 31 additions & 0 deletions cmd/antibody/actions/list.go
@@ -0,0 +1,31 @@
package actions

import (
"fmt"
"io/ioutil"

"github.com/caarlos0/antibody"
"github.com/codegangsta/cli"
)

// List all installed bundles
func List(c *cli.Context) {
for _, bundle := range installedBundles() {
fmt.Println(bundle.Name())
}
}

func installedBundles() []antibody.Bundle {
home := antibody.Home()
entries, _ := ioutil.ReadDir(home)
var bundles []antibody.Bundle
for _, bundle := range entries {
if bundle.Mode().IsDir() && bundle.Name()[0] != '.' {
bundles = append(
bundles,
antibody.NewBundle(bundle.Name(), home),
)
}
}
return bundles
}
11 changes: 11 additions & 0 deletions cmd/antibody/actions/update.go
@@ -0,0 +1,11 @@
package actions

import (
"github.com/caarlos0/antibody"
"github.com/codegangsta/cli"
)

// Update all installed bundles
func Update(c *cli.Context) {
antibody.New(installedBundles()).Update()
}
33 changes: 33 additions & 0 deletions cmd/antibody/main.go
@@ -0,0 +1,33 @@
package main

import (
"os"

"github.com/caarlos0/antibody/cmd/antibody/actions"
"github.com/codegangsta/cli"
)

var version = "master"

func main() {
app := cli.NewApp()
app.Name = "antibody"
app.Usage = "antibody is a faster and leaner version of antigen"
app.Commands = []cli.Command{
{
Name: "bundle",
Usage: "bundle one or more bundles",
Action: actions.Bundle,
}, {
Name: "update",
Usage: "updates all previously bundled commands",
Action: actions.Update,
}, {
Name: "list",
Usage: "list all currently installed bundles",
Action: actions.List,
},
}
app.Version = version
app.Run(os.Args)
}

0 comments on commit 4331f04

Please sign in to comment.