aboutsummaryrefslogtreecommitdiffstats
path: root/bommom.go
blob: 11b47c4b958ee647cbd2be8b4d58b1190886fa81 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main

// CLI for bommom tools. Also used to launch web interface.

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"path"
	"time"
)

// Globals
var bomstore BomStore
var auth AuthService
var anonUser = &User{name: "common"}

// Command line flags
var (
	templatePath  = flag.String("templatepath", "./templates", "path to template directory")
	fileStorePath = flag.String("path", "./filestore", "path to flat file data store top-level directory")
	verbose       = flag.Bool("verbose", false, "print extra info")
	helpFlag      = flag.Bool("help", false, "print full help info")
	outFormat     = flag.String("format", "", "command output format (for 'dump' etc)")
	inFormat      = ""
)

func main() {

	// Parse configuration
	flag.Parse()
	if *verbose {
		log.Println("template dir:", *templatePath)
		log.Println("filestore dir:", *fileStorePath)
		log.Println("anon user:", anonUser.name)
	}

	// Process command
	if *helpFlag {
		printUsage()
		return
	}

	if flag.NArg() < 1 {
		printUsage()
		fmt.Println()
		log.Fatal("Error: No command specified")
	}

	switch flag.Arg(0) {
	default:
		log.Fatal("Error: unknown command: ", flag.Arg(0))
	case "serve":
		log.Fatal("Error: Unimplemented, sorry")
	case "init":
		log.Println("Initializing...")
		initCmd()
	case "dump":
		dumpCmd()
	case "load":
		loadCmd()
	case "convert":
		convertCmd()
	case "list":
		listCmd()
	}
}

func openBomStore() {
	// defaults to JSON file store
	var err error
	bomstore, err = OpenJSONFileBomStore(*fileStorePath)
	if err != nil {
		log.Fatal(err)
	}
}

func dumpOut(fname string, bm *BomMeta, b *Bom) {
	var outFile io.Writer
	if fname == "" {
		outFile = os.Stdout
	} else {
		// if no outFormat defined, infer from file extension
		if *outFormat == "" {
			switch ext := path.Ext(fname); ext {
			case "", ".txt", ".text":
				// pass
			case ".json":
				*outFormat = "json"
			case ".csv":
				*outFormat = "csv"
			case ".xml":
				*outFormat = "xml"
			default:
				log.Fatal("Unknown file extention (use -format): " + ext)
			}
		}
		f, err := os.Create(fname)
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		outFile = io.Writer(f)
	}

	switch *outFormat {
	case "text", "":
		DumpBomAsText(bm, b, outFile)
	case "json":
		DumpBomAsJSON(bm, b, outFile)
	case "csv":
		DumpBomAsCSV(b, outFile)
	case "xml":
		DumpBomAsXML(bm, b, outFile)
	default:
		log.Fatal("Error: unknown/unimplemented format: " + *outFormat)
	}

}

func loadIn(fname string) (bm *BomMeta, b *Bom) {

	if inFormat == "" {
		switch ext := path.Ext(fname); ext {
		case ".json", ".JSON":
			inFormat = "json"
		case ".csv", ".CSV":
			inFormat = "csv"
		case ".xml", ".XML":
			inFormat = "xml"
		default:
			log.Fatal("Unknown file extention (use -format): " + ext)
		}
	}

	infile, err := os.Open(fname)
	if err != nil {
		log.Fatal(err)
	}
	defer infile.Close()

	switch inFormat {
	case "json":
		bm, b, err = LoadBomFromJSON(infile)
	case "csv":
		b, err = LoadBomFromCSV(infile)
	case "xml":
		bm, b, err = LoadBomFromXML(infile)
	default:
		log.Fatal("Error: unknown/unimplemented format: " + *outFormat)
	}
	if err != nil {
		log.Fatal(err)
	}
	return bm, b
}

func initCmd() {

	openBomStore()
	bm, err := bomstore.GetBomMeta(ShortName("common"), ShortName("gizmo"))
	if err == nil {
		// dummy BomMeta already exists?
		return
	}
	b := makeTestBom()
	b.Version = "v001"
	bm = &BomMeta{Name: "gizmo",
		Owner:        "common",
		Description:  "fancy stuff",
		HeadVersion:  b.Version,
		IsPublicView: true,
		IsPublicEdit: true}
	bomstore.Persist(bm, b, "v001")
}

func dumpCmd() {
	if flag.NArg() != 3 && flag.NArg() != 4 {
		log.Fatal("Error: wrong number of arguments (expected user and BOM name, optional file)")
	}

	userStr := flag.Arg(1)
	nameStr := flag.Arg(2)

	if !isShortName(userStr) || !isShortName(nameStr) {
		log.Fatal("Error: not valid ShortName: " + userStr +
			" and/or " + nameStr)
	}

	var fname string
	if flag.NArg() == 4 {
		fname = flag.Arg(3)
	} else {
		fname = ""
	}

	openBomStore()

	if auth == nil {
		auth = DummyAuth(true)
	}
	bm, b, err := bomstore.GetHead(ShortName(userStr), ShortName(nameStr))
	if err != nil {
		log.Fatal(err)
	}

	dumpOut(fname, bm, b)
}

func loadCmd() {
	if flag.NArg() != 5 {
		log.Fatal("Error: wrong number of arguments (expected input file, username, bomname, version)")
	}

	var userName, bomName, version string
	inFname := flag.Arg(1)
	userName = flag.Arg(2)
	bomName = flag.Arg(3)
	version = flag.Arg(4)

	if !(isShortName(userName) && isShortName(bomName) && isShortName(version)) {
		log.Fatal("user, name, and version must be ShortNames")
	}

	bm, b := loadIn(inFname)
	if inFormat == "csv" && bm == nil {
		// TODO: from inname? if ShortName?
		bm = &BomMeta{}
	}

	bm.Owner = userName
	bm.Name = bomName
	b.Progeny = "File import from " + inFname + " (" + inFormat + ")"
	b.Created = time.Now()
    b.Version = version

	openBomStore()

	if err := bomstore.Persist(bm, b, ShortName(version)); err != nil {
        log.Fatal(err)
    }
}

func convertCmd() {
	if flag.NArg() != 2 && flag.NArg() != 3 {
		log.Fatal("Error: wrong number of arguments (expected input and output files)")
	}

	// should refactor this to open both files first, then do processing? not
	// sure what best practice is.

	inFname := flag.Arg(1)
	outFname := flag.Arg(2)

	bm, b := loadIn(inFname)

	if b == nil {
		log.Fatal("null bom")
	}
	if inFormat == "csv" && bm == nil {
		// TODO: from inname? if ShortName?
		bm = &BomMeta{Name: "untitled",
			Owner: anonUser.name}
		b.Version = "unversioned"
	}

	b.Created = time.Now()
	b.Progeny = "File import from " + inFname + " (" + inFormat + ")"

	if err := bm.Validate(); err != nil {
		log.Fatal("loaded bommeta not valid: " + err.Error())
	}
	if err := b.Validate(); err != nil {
		log.Fatal("loaded bom not valid: " + err.Error())
	}

	dumpOut(outFname, bm, b)
}

func listCmd() {

	openBomStore()
	var bomMetas []BomMeta
	var err error
	if flag.NArg() > 2 {
		log.Fatal("Error: too many arguments...")
	}
	if flag.NArg() == 2 {
		name := flag.Arg(1)
		if !isShortName(name) {
			log.Fatal("Error: not a possible username: " + name)
		}
		bomMetas, err = bomstore.ListBoms(ShortName(name))
		if err != nil {
			log.Fatal(err)
		}
	} else {
		// list all boms from all names
		bomMetas, err = bomstore.ListBoms("")
		if err != nil {
			log.Fatal(err)
		}
	}
	for _, bm := range bomMetas {
		fmt.Println(bm.Owner + "/" + bm.Name)
	}
}

func printUsage() {
	fmt.Println("bommom is a tool for managing and publishing electronics BOMs")
	fmt.Println("")
	fmt.Println("Usage (flags must go first?):")
	fmt.Println("\tbommom [options] command [arguments]")
	fmt.Println("")
	fmt.Println("Commands:")
	fmt.Println("")
	fmt.Println("\tinit \t\t initialize BOM and authentication datastores")
	fmt.Println("\tlist [user]\t\t list BOMs, optionally filtered by user")
	fmt.Println("\tload <file.type> <user> <bom_name> <version>\t import a BOM")
	fmt.Println("\tdump <user> <name> [file.type]\t dump a BOM to stdout")
	fmt.Println("\tconvert <infile.type> <outfile.type>\t convert a BOM file")
	fmt.Println("\tserve\t\t serve up web interface over HTTP")
	fmt.Println("")
	fmt.Println("Extra command line options:")
	fmt.Println("")
	flag.PrintDefaults()
}