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 #169 from getantibody/keep-order
Browse files Browse the repository at this point in the history
keep the order of the plugins
  • Loading branch information
caarlos0 authored Feb 6, 2017
2 parents a33f5c3 + 5fa2dd0 commit 3dd1475
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
9 changes: 6 additions & 3 deletions antibody.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ func New(home string, r io.Reader) *Antibody {
func (a *Antibody) Bundle() (result string, err error) {
var g errgroup.Group
var lock sync.Mutex
var shs []string
var shs indexedLines
var idx int
scanner := bufio.NewScanner(a.r)
for scanner.Scan() {
l := scanner.Text()
index := idx
idx++
g.Go(func() error {
l = strings.TrimSpace(l)
if l != "" && l[0] != '#' {
s, err := bundle.New(a.Home, l).Get()
lock.Lock()
shs = append(shs, s)
shs = append(shs, indexedLine{index, s})
lock.Unlock()
return err
}
Expand All @@ -50,7 +53,7 @@ func (a *Antibody) Bundle() (result string, err error) {
return result, err
}
err = g.Wait()
return strings.Join(shs, "\n"), err
return shs.String(), err
}

// Home finds the right home folder to use
Expand Down
38 changes: 38 additions & 0 deletions sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package antibody

import (
"sort"
"strings"
)

type indexedLine struct {
idx int
line string
}

type indexedLines []indexedLine

// Len is needed by Sort interface
func (slice indexedLines) Len() int {
return len(slice)
}

// Less is needed by Sort interface
func (slice indexedLines) Less(i, j int) bool {
return slice[i].idx < slice[j].idx
}

// Swap is needed by Sort interface
func (slice indexedLines) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}

// Sort all lines and join them in a string
func (slice indexedLines) String() string {
sort.Sort(slice)
var lines []string
for _, line := range slice {
lines = append(lines, line.line)
}
return strings.Join(lines, "\n")
}

0 comments on commit 3dd1475

Please sign in to comment.