aboutsummaryrefslogtreecommitdiffstats
path: root/bommom.go
blob: 4b6e39d32e669141eb39eb9ef929c80816852870 (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
package main

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

import (
	"flag"
	"fmt"
	"log"
)

// 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", "text", "command output format (for 'dump' etc)")
)

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 "load", "serve":
		log.Fatal("Error: Unimplemented, sorry")
	case "init":
		log.Println("Initializing...")
        initCmd()
    case "dump":
        log.Println("Dumping...")
        dumpCmd()
    case "list":
        listCmd()
	}
}

func initCmd() {
    _, err := NewJSONFileBomStore(*fileStorePath)
    if err != nil {
        log.Fatal(err)
    }
}

func dumpCmd() {
    b := makeTestBom()
    b.Version = "v001"
    bs := &BomStub{Name: "widget",
                    Owner: "common",
                    Description: "fancy stuff",
                    HeadVersion: b.Version,
                    IsPublicView: true,
                    IsPublicEdit: true}
    jfbs, err := OpenJSONFileBomStore(*fileStorePath)
    if err != nil {
        log.Fatal(err)
    }
    jfbs.Persist(bs, b, "v001")
}

func listCmd() {
    jfbs, err := OpenJSONFileBomStore(*fileStorePath)
    if err != nil {
        log.Fatal(err)
    }
    var bomStubs []BomStub
    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)
        }
        bomStubs, err = jfbs.ListBoms(ShortName(name))
        if err != nil {
            log.Fatal(err)
        }
    } else {
        // list all boms from all names
        // TODO: ERROR
        bomStubs, err = jfbs.ListBoms("")
        if err != nil {
            log.Fatal(err)
        }
    }
    for _, bs := range bomStubs {
        fmt.Println(bs.Owner + "/" + bs.Name)
    }
}

func printUsage() {
	fmt.Println("bommom is a tool for managing and publishing electronics BOMs")
	fmt.Println("")
	fmt.Println("Usage:")
	fmt.Println("\tbommom command [options]")
	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>\t import a BOM")
	fmt.Println("\tdump <user> <name>\t dump a BOM to stdout")
	fmt.Println("\tserve\t\t serve up web interface over HTTP")
	fmt.Println("")
	fmt.Println("Extra command line options:")
	fmt.Println("")
	flag.PrintDefaults()
}