Skip to content

Commit

Permalink
Merge pull request #82 from tkuchiki/add-test-for-flags
Browse files Browse the repository at this point in the history
Add test for flags
  • Loading branch information
tkuchiki authored Sep 24, 2023
2 parents 6a9c52c + 6052499 commit b53cca7
Show file tree
Hide file tree
Showing 18 changed files with 1,380 additions and 609 deletions.
8 changes: 4 additions & 4 deletions cmd/alp/cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
func TestCommonFlags(t *testing.T) {
tempDir := t.TempDir()

tempLog, err := testutil.CreateTempDirAndFile(tempDir, testutil.JsonLog(testutil.NewJsonLogKeys()))
tempLog, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_log", testutil.JsonLog(testutil.NewJsonLogKeys()))
if err != nil {
t.Fatal(err)
}

tempConfig, err := testutil.CreateTempDirAndFile(tempDir, testutil.ConfigFile())
tempConfig, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_config", testutil.ConfigFile())
if err != nil {
t.Fatal(err)
}

tempPos, err := testutil.CreateTempDirAndFile(tempDir, "")
tempPos, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_pos", "")
if err != nil {
t.Fatal(err)
}

tempDump, err := testutil.CreateTempDirAndFile(tempDir, "")
tempDump, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_dump", "")
if err != nil {
t.Fatal(err)
}
Expand Down
47 changes: 19 additions & 28 deletions cmd/alp/cmd/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,41 @@ package cmd
import (
"os"

"github.com/tkuchiki/alp/options"

"github.com/spf13/cobra"
"github.com/tkuchiki/alp/counter"
"github.com/tkuchiki/alp/helpers"
"github.com/tkuchiki/alp/options"
"github.com/tkuchiki/alp/parsers"
)

func NewCountCmd() *cobra.Command {
func NewCountCmd(commandFlags *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 {
file, err := cmd.PersistentFlags().GetString("file")
opts, err := commandFlags.createCountOptions(cmd)
if err != nil {
return err
}

reverse, err := cmd.PersistentFlags().GetBool("reverse")
// TODO: start
// Remove these after implementing `alp (json|ltsv|regex) count`.
pattern, err := cmd.PersistentFlags().GetString("pattern")
if err != nil {
return err
}

pattern, err := cmd.PersistentFlags().GetString("pattern")
format, err := cmd.PersistentFlags().GetString("format")
if err != nil {
return err
}

opts := options.NewOptions()

opts = options.SetOptions(opts,
options.File(file),
options.Reverse(reverse),
options.Pattern(pattern),
options.Format(format),
)

format, err := cmd.PersistentFlags().GetString("format")
if err != nil {
return err
}

keysStr, err := cmd.PersistentFlags().GetString("keys")
if err != nil {
return err
}

keys := helpers.SplitCSV(keysStr)
// TODO: end

cnter := counter.NewCounter(os.Stdout, os.Stderr, opts.Reverse)

Expand Down Expand Up @@ -81,18 +69,21 @@ func NewCountCmd() *cobra.Command {

cnter.SetParser(parser)

err = cnter.CountAndPrint(keys)
err = cnter.CountAndPrint(opts.Count.Keys)

return err
},
}

commandFlags.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.PersistentFlags().StringP("pattern", "", options.DefaultPatternOption, "Regular expressions pattern matching the log (only use with --format=regexp)")
countCmd.PersistentFlags().StringP("file", "", "", "The access log file")
countCmd.PersistentFlags().BoolP("reverse", "r", false, "Sort results in reverse order")
countCmd.PersistentFlags().StringP("keys", "", "", "Log key names (comma separated)")
countCmd.MarkPersistentFlagRequired("keys")

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

return countCmd
}
29 changes: 9 additions & 20 deletions cmd/alp/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,17 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/tkuchiki/alp/options"
"github.com/tkuchiki/alp/stats"
)

func NewDiffCmd() *cobra.Command {
func NewDiffCmd(commandFlags *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 {
sortOptions := stats.NewSortOptions()
var opts *options.Options

config, err := cmd.PersistentFlags().GetString("config")
if err != nil {
return err
}

if config != "" {
bindCommonFlags(cmd)
opts, err = createOptionsFromConfig(cmd, sortOptions, config)
} else {
opts, err = createCommonOptionsFromFlags(cmd, sortOptions)
}

opts, err := commandFlags.createDiffOptions(cmd)
if err != nil {
return err
}
Expand All @@ -45,7 +30,7 @@ func NewDiffCmd() *cobra.Command {
}

sts.SetOptions(opts)
sts.SetSortOptions(sortOptions)
sts.SetSortOptions(commandFlags.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 @@ -72,7 +57,7 @@ func NewDiffCmd() *cobra.Command {
}

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

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

defineOptions(diffCmd)
commandFlags.defineDiffOptions(diffCmd)

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

return diffCmd
}
Loading

0 comments on commit b53cca7

Please sign in to comment.