-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
130 lines (109 loc) · 2.52 KB
/
Copy pathmain.go
File metadata and controls
130 lines (109 loc) · 2.52 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"strings"
)
var (
inputFilename string
outputFilename string
inputfile io.Reader
outputfile *bufio.Writer
prefix string
showversion bool
)
const VERSION = "0.0.1"
type ManifestEntry struct {
Url string `json:"url"`
Mandatory bool `json:"mandatory"`
}
type Manifest struct {
Entries []ManifestEntry `json:"entries"`
}
func NewManifest() (m *Manifest) {
m = &Manifest{}
m.Entries = make([]ManifestEntry, 0, 0)
return m
}
func (m *Manifest) AddEntry(url string) {
entry := ManifestEntry{url, true}
m.Entries = append(m.Entries, entry)
}
func (m *Manifest) String() string {
s := ""
for _, e := range m.Entries {
s = fmt.Sprintf("%s%s\n", s, e.Url)
}
return s
}
func init() {
flag.BoolVar(&showversion, "version", false, "print version string")
flag.StringVar(&inputFilename, "i", "", "input filename (stdout if none provided)")
flag.StringVar(&outputFilename, "o", "", "output filename (stdout if none provided)")
flag.StringVar(&prefix, "p", "", "s3 bucketname or prefix")
}
func main() {
flag.Usage = usage
flag.Parse()
if showversion {
version()
return
}
if inputFilename != "" {
file, err := os.Open(inputFilename)
if err != nil {
exitWithError(fmt.Errorf("Unable to open input %s: %s", inputFilename, err))
}
defer file.Close()
inputfile = bufio.NewReader(file)
} else {
inputfile = bufio.NewReader(os.Stdin)
}
// output to Stdout if no file given
if outputFilename != "" {
file, err := os.Create(outputFilename)
if err != nil {
exitWithError(fmt.Errorf("Unable to create output %s: %s", outputFilename, err))
}
defer file.Close()
outputfile = bufio.NewWriter(file)
} else {
outputfile = bufio.NewWriter(os.Stdout)
}
defer outputfile.Flush()
manifest := NewManifest()
scanner := bufio.NewScanner(inputfile)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if prefix != "" {
line = fmt.Sprintf("%s%s", prefix, line)
}
manifest.AddEntry(line)
}
b, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
exitWithError(fmt.Errorf("Unable to create JSON manifest: %s", err))
}
fmt.Fprintf(outputfile, "%s", b)
}
// display usage message
func usage() {
fmt.Fprintf(os.Stderr, "usage: mani [flags]\n")
flag.PrintDefaults()
}
// display error and exit
func exitWithError(err error) {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// print application version
func version() {
fmt.Printf("v%s\n", VERSION)
}