Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions cmd/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

ldsview "github.com/kgoins/ldsview/pkg"
"github.com/spf13/cobra"
)
Expand All @@ -12,24 +13,20 @@ var countCmd = &cobra.Command{
Short: "Counts the number of entities in an ldif file",
Run: func(cmd *cobra.Command, args []string) {
dumpFile, _ := cmd.Flags().GetString("file")
builder := ldsview.NewLdifParser(dumpFile)

entities := make(chan ldsview.Entity)
done := make(chan bool)
parser := ldsview.NewLdifParser(dumpFile)

// Start the printing goroutine
go ChannelPrinter(entities, done, cmd)

err := builder.BuildEntities(entities, done)
count, err := parser.CountEntities()
if err != nil {
fmt.Printf("Unable to parse file: %s\n", err.Error())
return
}

fmt.Println("Entities: ", count)
},
}

func init() {
rootCmd.AddCommand(countCmd)
countCmd.Flags().Bool( "count", true, "" )
countCmd.Flags().Bool("count", true, "")
countCmd.Flags().MarkHidden("count")
}
8 changes: 1 addition & 7 deletions cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func PrintEntity(entity ldsview.Entity, decodeTS bool) {
// when finished
func ChannelPrinter(entities chan ldsview.Entity, done chan bool, cmd *cobra.Command) {

count, _ := cmd.Flags().GetBool("count")
tdc, _ := cmd.Flags().GetBool("tdc")

printLimit, intParseErr := cmd.Flags().GetInt("first")
Expand All @@ -59,17 +58,12 @@ func ChannelPrinter(entities chan ldsview.Entity, done chan bool, cmd *cobra.Com
for entity := range entities {
entCount = entCount + 1

if !count {
PrintEntity(entity, tdc)
}
PrintEntity(entity, tdc)

if entCount == printLimit {
break
}
}

if count {
fmt.Println("Entities: ", entCount)
}
done <- true
}
31 changes: 31 additions & 0 deletions pkg/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ldsview

import (
"errors"
"os"
)

// CountEntities returns the number of entities in the input file
func (parser LdifParser) CountEntities() (count int, err error) {
Logger.Info("Opening ldif file: " + parser.filename)
dumpFile, err := os.Open(parser.filename)
if err != nil {
return
}
defer dumpFile.Close()

Logger.Info("Finding first entity block")
entityScanner := parser.findFirstEntityBlock(dumpFile)
if entityScanner == nil {
return count, errors.New("Unable to find first entity block")
}

for entityScanner.Scan() {
titleLine := entityScanner.Text()
if parser.isEntityTitle(titleLine) {
count++
}
}

return
}
18 changes: 18 additions & 0 deletions pkg/counter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ldsview

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestLdifParser_CountEntities(t *testing.T) {
a := assert.New(t)

parser := NewLdifParser(TESTFILE)
a.NotNil(parser)

count, err := parser.CountEntities()
a.NoError(err)
a.Equal(count, NUMENTITIES)
}
2 changes: 1 addition & 1 deletion pkg/ldif_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBuildEntities(t *testing.T) {

err := parser.BuildEntities(entities, done)
assert.Nil(t, err)
assert.Equal(t, 3, counter.c)
assert.Equal(t, NUMENTITIES, counter.c)
})
}

Expand Down
1 change: 1 addition & 0 deletions pkg/pkg_main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package ldsview

const TESTFILE = "../testdata/test_users.ldif"
const NUMENTITIES = 3