Skip to content

Commit

Permalink
Merge pull request #83 from tkuchiki/add-diff-subcommand
Browse files Browse the repository at this point in the history
Add (json|ltsv|regexp|pcap) diff command
  • Loading branch information
tkuchiki committed Sep 27, 2023
2 parents b53cca7 + 3d0663c commit 54f59cc
Show file tree
Hide file tree
Showing 20 changed files with 905 additions and 156 deletions.
93 changes: 93 additions & 0 deletions cmd/alp/cmd/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cmd

import (
"github.com/spf13/cobra"
)

type Command struct {
// alp
rootCmd *cobra.Command

// alp diff
diffCmd *cobra.Command

// alp count
countCmd *cobra.Command

// alp json
jsonCmd *cobra.Command
// alp json diff
jsonDiffCmd *cobra.Command

// alp ltsv
ltsvCmd *cobra.Command
// alp ltsv diff
ltsvDiffCmd *cobra.Command

// alp regexp
regexpCmd *cobra.Command
// alp regexp diff
regexpDiffCmd *cobra.Command

// alp pcap
pcapCmd *cobra.Command
// alp pcap diff
pcapDiffCmd *cobra.Command

flags *flags
}

func NewCommand(version string) *Command {
command := &Command{}
command.flags = newFlags()

command.rootCmd = newRootCmd(version)

command.flags.defineGlobalOptions(command.rootCmd)

// alp ltsv
command.ltsvCmd = newLTSVCmd(command.flags)
command.rootCmd.AddCommand(command.ltsvCmd)
// alp ltsv diff
command.ltsvDiffCmd = newLTSVDiffCmd(command.flags)
command.ltsvCmd.AddCommand(command.ltsvDiffCmd)

// alp json
command.jsonCmd = newJSONCmd(command.flags)
command.rootCmd.AddCommand(command.jsonCmd)
// alp json diff
command.jsonDiffCmd = newJsonDiffCmd(command.flags)
command.jsonCmd.AddCommand(command.jsonDiffCmd)

// alp regexp
command.regexpCmd = newRegexpCmd(command.flags)
command.rootCmd.AddCommand(command.regexpCmd)
// alp regexp diff
command.regexpDiffCmd = newRegexpDiffCmd(command.flags)
command.regexpCmd.AddCommand(command.regexpDiffCmd)

// alp pcap
command.pcapCmd = newPcapCmd(command.flags)
command.rootCmd.AddCommand(command.pcapCmd)
// alp pcap diff
command.pcapDiffCmd = newPcapDiffCmd(command.flags)
command.pcapCmd.AddCommand(command.pcapDiffCmd)

// alp diff
command.diffCmd = newDiffCmd(command.flags)
command.rootCmd.AddCommand(command.diffCmd)

// alp count
command.countCmd = newCountCmd(command.flags)
command.rootCmd.AddCommand(command.countCmd)

return command
}

func (c *Command) Execute() error {
return c.rootCmd.Execute()
}

func (c *Command) setArgs(args []string) {
c.rootCmd.SetArgs(args)
}
6 changes: 3 additions & 3 deletions cmd/alp/cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ func TestCommonFlags(t *testing.T) {

for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
rootCmd := NewRootCmd("test")
rootCmd.SetArgs(tt.args)
command := NewCommand("test")
command.setArgs(tt.args)

err := rootCmd.Execute()
err := command.Execute()
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/alp/cmd/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/tkuchiki/alp/parsers"
)

func NewCountCmd(commandFlags *flags) *cobra.Command {
func newCountCmd(flags *flags) *cobra.Command {
var countCmd = &cobra.Command{
Use: "count",
Short: "Count by log entries",
Long: `Count by log entries`,
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := commandFlags.createCountOptions(cmd)
opts, err := flags.createCountOptions(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewCountCmd(commandFlags *flags) *cobra.Command {
},
}

commandFlags.defineCountOptions(countCmd)
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")
Expand Down
6 changes: 3 additions & 3 deletions cmd/alp/cmd/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func TestCountCmd(t *testing.T) {
"--keys", "ua",
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)
command := NewCommand("test")
command.setArgs(args)

err := rootCmd.Execute()
err := command.Execute()
if err != nil {
t.Fatal(err)
}
Expand Down
41 changes: 36 additions & 5 deletions cmd/alp/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/tkuchiki/alp/parsers"
"github.com/tkuchiki/alp/profiler"
"github.com/tkuchiki/alp/stats"
)

func NewDiffCmd(commandFlags *flags) *cobra.Command {
func newDiffCmd(flags *flags) *cobra.Command {
diffCmd := &cobra.Command{
Use: "diff <from> <to>",
Args: cobra.ExactArgs(2),
Short: "Show the difference between the two profile results",
Long: `Show the difference between the two profile results`,
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := commandFlags.createDiffOptions(cmd)
opts, err := flags.createDiffOptions(cmd)
if err != nil {
return err
}
Expand All @@ -30,7 +32,7 @@ func NewDiffCmd(commandFlags *flags) *cobra.Command {
}

sts.SetOptions(opts)
sts.SetSortOptions(commandFlags.sortOptions)
sts.SetSortOptions(flags.sortOptions)

printOptions := stats.NewPrintOptions(opts.NoHeaders, opts.ShowFooters, opts.DecodeUri, opts.PaginationLimit)
printer := stats.NewPrinter(os.Stdout, opts.Output, opts.Format, opts.Percentiles, printOptions)
Expand All @@ -57,7 +59,7 @@ func NewDiffCmd(commandFlags *flags) *cobra.Command {
}

toSts.SetOptions(opts)
toSts.SetSortOptions(commandFlags.sortOptions)
toSts.SetSortOptions(flags.sortOptions)

tof, err := os.Open(to)
if err != nil {
Expand All @@ -77,11 +79,40 @@ func NewDiffCmd(commandFlags *flags) *cobra.Command {
},
}

commandFlags.defineDiffOptions(diffCmd)
flags.defineDiffOptions(diffCmd)

diffCmd.Flags().SortFlags = false
diffCmd.PersistentFlags().SortFlags = false
diffCmd.InheritedFlags().SortFlags = false

return diffCmd
}

func newDiffSubCmd() *cobra.Command {
return &cobra.Command{
Use: "diff [<from>] <to>",
Args: cobra.RangeArgs(1, 2),
Short: "Show the difference between the two access logs",
Long: `Show the difference between the two access logs`,
}
}

func getFromTo(load string, args []string) (string, string) {
if load != "" {
return "", args[0]
}

return args[0], args[1]
}

func runDiff(sortOptions *stats.SortOptions,
fromProf *profiler.Profiler, fromParser parsers.Parser,
toProf *profiler.Profiler, toParser parsers.Parser) error {

fromSts, err := fromProf.Profile(sortOptions, fromParser)
if err != nil {
return err
}

return toProf.Run(sortOptions, toParser, fromSts)
}
6 changes: 3 additions & 3 deletions cmd/alp/cmd/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ func TestDiffCmd(t *testing.T) {
from, to,
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)
command := NewCommand("test")
command.setArgs(args)

err := rootCmd.Execute()
err := command.Execute()
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit 54f59cc

Please sign in to comment.