Skip to content
Open
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
3 changes: 3 additions & 0 deletions flog_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"os"
"path/filepath"
"syscall"

"github.com/brianvoe/gofakeit"
)

// Run checks overwrite flag and generates logs with given options
func Run(option *Option) error {
gofakeit.Seed(option.Seed)
logDir := filepath.Dir(option.Output)
oldMask := syscall.Umask(0000)
if err := os.MkdirAll(logDir, 0766); err != nil {
Expand Down
4 changes: 0 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package main

import (
"math/rand"
"time"

"github.com/mingrammer/cfmt"
)

func main() {
rand.Seed(time.Now().UnixNano())
opts := ParseOptions()
if err := Run(opts); err != nil {
cfmt.Warningln(err.Error())
Expand Down
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Options:
with "byte" option, the logs will be split whenever the maximum size in bytes is reached.
-w, --overwrite overwrite the existing log files.
-l, --loop loop output forever until killed.
-r, --seed seed to initialise the random number generator, defaults to the current timestamp.
`

var validFormats = []string{"apache_common", "apache_combined", "apache_error", "rfc3164", "rfc5424", "common_log", "json"}
Expand All @@ -60,6 +61,7 @@ type Option struct {
SplitBy int
Overwrite bool
Forever bool
Seed int64
}

func init() {
Expand Down Expand Up @@ -166,6 +168,10 @@ func ParseSplitBy(splitBy int) (int, error) {

// ParseOptions parses given parameters from command line
func ParseOptions() *Option {
return parseOptions(time.Now)
}

func parseOptions(now func() time.Time) *Option {
var err error

opts := defaultOptions()
Expand All @@ -182,6 +188,7 @@ func ParseOptions() *Option {
splitBy := pflag.IntP("split", "p", opts.SplitBy, "Maximum number of lines or size of a log file")
overwrite := pflag.BoolP("overwrite", "w", false, "Overwrite the existing log files")
forever := pflag.BoolP("loop", "l", false, "Loop output forever until killed")
seed := pflag.Int64P("seed", "r", now().UnixMicro(), "Seed to initialise the random number generator, defaults to the current timestamp")

pflag.Parse()

Expand Down Expand Up @@ -217,5 +224,6 @@ func ParseOptions() *Option {
opts.Output = *output
opts.Overwrite = *overwrite
opts.Forever = *forever
opts.Seed = *seed
return opts
}
6 changes: 5 additions & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func TestParseSplitBy(t *testing.T) {
func TestParseOptions(t *testing.T) {
a := assert.New(t)

option := ParseOptions()
now := func() time.Time {
return time.Unix(0, 0)
}

option := parseOptions(now)
a.Equal(defaultOptions(), option, "without flags, option should be default one")
}