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

Commit

Permalink
organization
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Sep 5, 2015
1 parent e9d327a commit e59135d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 70 deletions.
25 changes: 12 additions & 13 deletions antibody.go
Expand Up @@ -5,48 +5,47 @@ import (
"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 bundleAction func(bundle Bundle)
type bundleAction func(b bundle.Bundle)

// New creates an instance of antibody with the given bundles.
func New(bundles []Bundle) Antibody {
return Antibody{
bundles: 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 _, bundle := range a.bundles {
for _, b := range a.bundles {
wg.Add(1)
go func(bundle Bundle, action bundleAction) {
action(bundle)
for _, sourceable := range bundle.Sourceables() {
go func(b bundle.Bundle, action bundleAction) {
action(b)
for _, sourceable := range b.Sourceables() {
fmt.Println(sourceable)
}
wg.Done()
}(bundle, action)
}(b, action)
}
wg.Wait()
}
Expand Down
21 changes: 17 additions & 4 deletions bundle.go → bundle/bundle.go
@@ -1,6 +1,7 @@
package antibody
package bundle

import (
"io/ioutil"
"path/filepath"

"github.com/caarlos0/antibody/git"
Expand All @@ -11,10 +12,10 @@ type Bundle struct {
git.Repo
}

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

Expand All @@ -29,3 +30,15 @@ func (b Bundle) Sourceables() []string {
}
return nil
}

// List all bundles in the given folder
func List(folder string) []Bundle {
entries, _ := ioutil.ReadDir(folder)
var bundles []Bundle
for _, bundle := range entries {
if bundle.Mode().IsDir() && bundle.Name()[0] != '.' {
bundles = append(bundles, New(bundle.Name(), folder))
}
}
return bundles
}
38 changes: 38 additions & 0 deletions bundle/bundle_test.go
@@ -0,0 +1,38 @@
package bundle_test

import (
"os"
"testing"

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

func TestBundleSourceables(t *testing.T) {
home := internal.TempHome()
defer os.RemoveAll(home)
b := bundle.New("caarlos0/zsh-pg", home)
b.Download()
assert.NotEmpty(t, b.Sourceables())
}

func TestSourceablesWithoutDownload(t *testing.T) {
home := internal.TempHome()
defer os.RemoveAll(home)
b := bundle.New("caarlos0/zsh-pg", home)
assert.Empty(t, b.Sourceables())
}

func TestListEmptyFolder(t *testing.T) {
home := internal.TempHome()
defer os.RemoveAll(home)
assert.Empty(t, bundle.List(home))
}

func TestList(t *testing.T) {
home := internal.TempHome()
defer os.RemoveAll(home)
bundle.New("caarlos0/zsh-pg", home).Download()
assert.NotEmpty(t, bundle.List(home))
}
25 changes: 0 additions & 25 deletions bundle_test.go

This file was deleted.

17 changes: 8 additions & 9 deletions cmd/antibody/actions/bundle.go
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

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

Expand All @@ -15,11 +16,9 @@ func Bundle(c *cli.Context) {
if readFromStdin() {
processStdin(os.Stdin)
} else {
antibody.New(
[]antibody.Bundle{
antibody.NewBundle(c.Args().First(), antibody.Home()),
},
).Download()
antibody.New([]bundle.Bundle{
bundle.New(c.Args().First(), antibody.Home()),
}).Download()
}
}

Expand All @@ -31,12 +30,12 @@ func readFromStdin() bool {
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 == "" {
var bundles []bundle.Bundle
for _, b := range strings.Split(string(entries), "\n") {
if b == "" {
continue
}
bundles = append(bundles, antibody.NewBundle(bundle, home))
bundles = append(bundles, bundle.New(b, home))
}
antibody.New(bundles).Download()
}
21 changes: 3 additions & 18 deletions cmd/antibody/actions/list.go
Expand Up @@ -2,30 +2,15 @@ package actions

import (
"fmt"
"io/ioutil"

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

// List all installed bundles
func List(c *cli.Context) {
for _, bundle := range installedBundles() {
fmt.Println(bundle.Name())
for _, b := range bundle.List(antibody.Home()) {
fmt.Println(b.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
}
3 changes: 2 additions & 1 deletion cmd/antibody/actions/update.go
Expand Up @@ -2,10 +2,11 @@ package actions

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

// Update all installed bundles
func Update(c *cli.Context) {
antibody.New(installedBundles()).Update()
antibody.New(bundle.List(antibody.Home())).Update()
}

0 comments on commit e59135d

Please sign in to comment.