This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
117 lines (95 loc) · 2.99 KB
/
main.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// This is a simple script which is designed to output a list of
// static resources to a golang source-file which can be then compiled
// into your program.
//
// The program basically reads all files in a given directory and spits
// out a templated file.
//
package main
import (
"flag"
"fmt"
"os"
"strings"
)
//
// Our version number.
//
var (
version = "master/latest"
)
// inputFlags is a helper so that multiple input-directories may be accecpted.
type inputFlags []string
// String convert our input-flags to a string.
func (i *inputFlags) String() string {
return strings.Join(*i, ",")
}
// Set adds an -input-flag to our list of directories to scan.
func (i *inputFlags) Set(value string) error {
*i = append(*i, strings.TrimSpace(value))
return nil
}
// ConfigOptions is the globally visible structure which is designed to
// hold our configuration-options - as set by the command-line flags.
//
// It is perhaps poor practice to do things this way, but it eases coding.
var ConfigOptions struct {
// Input contains the directories we should search for files to include
Input inputFlags
// Output is used to hold the name of the file we generate.
Output string
// Exclude contains a regular expression of files to exclude from
// the implantation process
Exclude string
// Package contains the package to which the generated file should be
// a member of. (By default `main`.)
Package string
// Format controls whether we format the generated file with `go fmt`
Format bool
// Verbose is set to increase verbosity.
Verbose bool
// Version is used to determine whether we should just report our
// version-number then terminate.
Version bool
}
// parseArguments parses our arguments, via the flag-package
func parseArguments() {
flag.BoolVar(&ConfigOptions.Format, "format", true, "Should we pipe our template through 'gofmt'?")
flag.BoolVar(&ConfigOptions.Verbose, "verbose", false, "Should we be verbose.")
flag.BoolVar(&ConfigOptions.Version, "version", false, "Should we report our version and exit?")
flag.StringVar(&ConfigOptions.Exclude, "exclude", "", "A regular expression of files to ignore, for example '.git'.")
flag.StringVar(&ConfigOptions.Output, "output", "static.go", "The output file to generate.")
flag.StringVar(&ConfigOptions.Package, "package", "main", "The (go) package that the generated file is part of.")
flag.Var(&ConfigOptions.Input, "input", "The directory to read from.")
//
// Parse the flags
//
flag.Parse()
}
// main is our entry-point.
func main() {
//
// Parse our arguments
//
parseArguments()
//
// If we received no input-directory/input-directories then we
// should default to processing the contents of the ./data
// directory, which was our previous default.
//
if len(ConfigOptions.Input) == 0 {
ConfigOptions.Input = append(ConfigOptions.Input, "./data")
}
//
// Showing our version?
//
if ConfigOptions.Version {
fmt.Printf("implant %s\n", version)
os.Exit(0)
}
//
// Otherwise process the directories.
//
Implant()
}