Skip to content

Commit

Permalink
modded count cmd to use count api
Browse files Browse the repository at this point in the history
  • Loading branch information
kgoins committed Apr 12, 2021
1 parent ac1b88a commit 666c921
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
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
}

0 comments on commit 666c921

Please sign in to comment.