File: main.go

package info (click to toggle)
dmarc-cat 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 164 kB
  • sloc: xml: 104; makefile: 27
file content (92 lines) | stat: -rw-r--r-- 1,658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"runtime"

	"github.com/keltia/archive"
)

var (
	// MyName is the application
	MyName = filepath.Base(os.Args[0])
	// MyVersion is our version
	MyVersion = "0.9.2"
	// Author should be abvious
	Author = "Ollivier Robert"

	fDebug    bool
	fJobs     int
	fNoResolv bool
	fSort     string
	fVerbose  bool
	fVersion  bool
)

// Context is passed around rather than being a global var/struct
type Context struct {
	r    Resolver
	jobs int
}

func init() {
	flag.BoolVar(&fDebug, "D", false, "Debug mode")
	flag.BoolVar(&fNoResolv, "N", false, "Do not resolve IPs")
	flag.IntVar(&fJobs, "j", runtime.NumCPU(), "Parallel jobs")
	flag.StringVar(&fSort, "S", `"Count" "dsc"`, "Sort results")
	flag.BoolVar(&fVerbose, "v", false, "Verbose mode")
	flag.BoolVar(&fVersion, "version", false, "Display version")
}

func Version() {
	fmt.Printf("%s version %s/j%d archive/%s\n", MyName, MyVersion, fJobs, archive.Version())
}

// Setup creates our context and check stuff
func Setup(a []string) *Context {
	// Exist early if -version
	if fVersion {
		Version()
		return nil
	}

	if fDebug {
		fVerbose = true
		debug("debug mode")
	}

	if len(a) < 1 {
		log.Println("You must specify at least one file.")
		return nil
	}

	ctx := &Context{RealResolver{}, fJobs}

	// Make it easier to sub it out
	if fNoResolv {
		ctx.r = NullResolver{}
	}

	return ctx
}

func main() {
	flag.Parse()

	ctx := Setup(flag.Args())
	if ctx == nil {
		return
	}

	file := flag.Arg(0)
	txt, err := HandleSingleFile(ctx, file)
	if err != nil {
		log.Printf("error handling %s: %v", file, err)
		return
	}
	fmt.Println(txt)
}