This repository has been archived by the owner on May 27, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |