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

Commit

Permalink
Merge pull request #62 from caarlos0/next
Browse files Browse the repository at this point in the history
antibody-next
  • Loading branch information
caarlos0 committed Sep 5, 2015
2 parents c6a09b1 + 53d6de0 commit 54b9996
Show file tree
Hide file tree
Showing 20 changed files with 410 additions and 363 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
@@ -0,0 +1,14 @@
root = true

[*]
end_of_line = lf
indent_size = 2
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
58 changes: 35 additions & 23 deletions antibody.go
Expand Up @@ -2,47 +2,59 @@ package antibody

import (
"fmt"
"os"
"sync"

"github.com/caarlos0/antibody/bundle"
"github.com/caarlos0/gohome"
)

// Antibody wraps a list of bundles to be processed.
type Antibody struct {
bundles []Bundle
bundles []bundle.Bundle
}
type bundleFn func(bundle Bundle)

// NewAntibody creates an instance of antibody with the given bundles.
func NewAntibody(bundles []Bundle) Antibody {
return Antibody{
bundles: bundles,
}
}
type bundleAction func(b bundle.Bundle)

func (a Antibody) forEach(fn bundleFn) {
var wg sync.WaitGroup
for _, bundle := range a.bundles {
wg.Add(1)
go func(bundle Bundle, fn bundleFn) {
fn(bundle)
for _, sourceable := range bundle.Sourceables() {
fmt.Println(sourceable)
}
wg.Done()
}(bundle, fn)
}
wg.Wait()
// New creates an instance of antibody with the given bundles.
func New(bundles []bundle.Bundle) Antibody {
return Antibody{bundles: bundles}
}

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

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

func (a Antibody) forEach(action bundleAction) {
var wg sync.WaitGroup
for _, b := range a.bundles {
wg.Add(1)
go func(b bundle.Bundle, action bundleAction) {
action(b)
for _, sourceable := range b.Sourceables() {
fmt.Println(sourceable)
}
wg.Done()
}(b, 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
}
10 changes: 5 additions & 5 deletions antibody.zsh
Expand Up @@ -3,18 +3,18 @@ ANTIBODY_BINARIES="$(dirname $0)"

antibody() {
case "$1" in
version)
${ANTIBODY_BINARIES}/bin/antibody $@
;;
*)
bundle|update)
while read bundle; do
source "$bundle" 2&> /tmp/antibody-log
done < <( ${ANTIBODY_BINARIES}/bin/antibody $@ )
;;
*)
${ANTIBODY_BINARIES}/bin/antibody $@
;;
esac
}

_antibody() {
IFS=' ' read -A reply <<< "$(echo "bundle update version")"
IFS=' ' read -A reply <<< "$(echo "bundle update list help")"
}
compctl -K _antibody antibody
34 changes: 34 additions & 0 deletions antibody_test.go
@@ -0,0 +1,34 @@
package antibody_test

import (
"os"
"testing"

"github.com/caarlos0/antibody"
"github.com/caarlos0/antibody/bundle"
"github.com/caarlos0/antibody/internal"
"github.com/stretchr/testify/assert"
)

func TestBundleAndUpdate(t *testing.T) {
home := internal.TempHome()
defer os.RemoveAll(home)
a := antibody.New([]bundle.Bundle{
bundle.New("caarlos0/zsh-pg", home),
bundle.New("caarlos0/zsh-open-pr", home),
})
a.Download()
a.Update()
internal.AssertFileCount(t, 2, home)
}

func TestCustomHome(t *testing.T) {
home := internal.TempHome()
defer os.RemoveAll(home)
assert.Equal(t, home, antibody.Home())
}

func TestDefaultHome(t *testing.T) {
os.Unsetenv("ANTIBODY_HOME")
assert.NotEmpty(t, antibody.Home())
}
73 changes: 0 additions & 73 deletions api.go

This file was deleted.

136 changes: 0 additions & 136 deletions api_test.go

This file was deleted.

9 changes: 0 additions & 9 deletions bundle.go

This file was deleted.

0 comments on commit 54b9996

Please sign in to comment.