-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from tkuchiki/fix-count-subcommand
Move from `alp count` to `alp (json|ltsv|regexp) count`
- Loading branch information
Showing
14 changed files
with
398 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/tkuchiki/alp/options" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/tkuchiki/alp/counter" | ||
"github.com/tkuchiki/alp/options" | ||
"github.com/tkuchiki/alp/parsers" | ||
) | ||
|
||
func newCountCmd(flags *flags) *cobra.Command { | ||
var countCmd = &cobra.Command{ | ||
func newCountSubCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "count", | ||
Short: "Count by log entries", | ||
Long: `Count by log entries`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts, err := flags.createCountOptions(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: start | ||
// Remove these after implementing `alp (json|ltsv|regex) count`. | ||
pattern, err := cmd.PersistentFlags().GetString("pattern") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
format, err := cmd.PersistentFlags().GetString("format") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
opts = options.SetOptions(opts, | ||
options.Pattern(pattern), | ||
options.Format(format), | ||
) | ||
// TODO: end | ||
|
||
cnter := counter.NewCounter(os.Stdout, os.Stderr, opts.Reverse) | ||
|
||
f, err := cnter.Open(opts.File) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
var parser parsers.Parser | ||
switch format { | ||
case "json": | ||
jsonKeys := parsers.NewJSONKeys(opts.JSON.UriKey, opts.JSON.MethodKey, opts.JSON.TimeKey, | ||
opts.JSON.ResponseTimeKey, opts.JSON.RequestTimeKey, opts.JSON.BodyBytesKey, opts.JSON.StatusKey) | ||
parser = parsers.NewJSONParser(f, jsonKeys, opts.QueryString, opts.QueryStringIgnoreValues) | ||
case "ltsv": | ||
label := parsers.NewLTSVLabel(opts.LTSV.UriLabel, opts.LTSV.MethodLabel, opts.LTSV.TimeLabel, | ||
opts.LTSV.ApptimeLabel, opts.LTSV.ReqtimeLabel, opts.LTSV.SizeLabel, opts.LTSV.StatusLabel, | ||
) | ||
parser = parsers.NewLTSVParser(f, label, opts.QueryString, opts.QueryStringIgnoreValues) | ||
case "regexp": | ||
names := parsers.NewSubexpNames(opts.Regexp.UriSubexp, opts.Regexp.MethodSubexp, opts.Regexp.TimeSubexp, | ||
opts.Regexp.ResponseTimeSubexp, opts.Regexp.RequestTimeSubexp, opts.Regexp.BodyBytesSubexp, opts.Regexp.StatusSubexp) | ||
parser, err = parsers.NewRegexpParser(f, opts.Regexp.Pattern, names, opts.QueryString, opts.QueryStringIgnoreValues) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
cnter.SetParser(parser) | ||
|
||
err = cnter.CountAndPrint(opts.Count.Keys) | ||
|
||
return err | ||
}, | ||
} | ||
} | ||
|
||
flags.defineCountOptions(countCmd) | ||
|
||
// TODO: Remove these after implementing `alp (json|ltsv|regex) count`. | ||
countCmd.PersistentFlags().StringP("pattern", "", options.DefaultPatternOption, "Regular expressions pattern matching the log") | ||
countCmd.PersistentFlags().StringP("format", "", "json", "Log format (json,ltsv,regexp)") | ||
|
||
countCmd.Flags().SortFlags = false | ||
countCmd.PersistentFlags().SortFlags = false | ||
countCmd.InheritedFlags().SortFlags = false | ||
|
||
return countCmd | ||
func runCount(counter *counter.Counter, parser parsers.Parser, opts *options.Options) error { | ||
counter.SetParser(parser) | ||
return counter.CountAndPrint(opts.Count.Keys) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.