/
cli.go
70 lines (59 loc) · 1.77 KB
/
cli.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
package analyze
import (
"fmt"
"os/exec"
"runtime"
"github.com/lkarlslund/adalanche/modules/cli"
"github.com/lkarlslund/adalanche/modules/engine"
"github.com/lkarlslund/adalanche/modules/ui"
"github.com/spf13/cobra"
)
var (
Command = &cobra.Command{
Use: "analyze [-options]",
Short: "Lanunches the interactive discovery tool in your browser",
}
bind = Command.Flags().String("bind", "127.0.0.1:8080", "Address and port of webservice to bind to")
nobrowser = Command.Flags().Bool("nobrowser", false, "Don't launch browser after starting webservice")
localhtml = Command.Flags().StringSlice("localhtml", nil, "Override embedded HTML and use a local folders for webservice (for development)")
WebService = NewWebservice()
)
func init() {
cli.Root.AddCommand(Command)
Command.RunE = Execute
Command.Flags().Lookup("localhtml").Hidden = true
}
func Execute(cmd *cobra.Command, args []string) error {
datapath := cmd.InheritedFlags().Lookup("datapath").Value.String()
// Process what we can in foreground, and the rest in the background
objs, err := engine.Run(datapath)
if err != nil {
return err
}
// Fire up the web interface with incomplete results
err = WebService.Start(*bind, objs, *localhtml)
if err != nil {
return err
}
// Launch browser
if !*nobrowser {
var err error
url := "http://" + *bind
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
ui.Warn().Msgf("Problem launching browser: %v", err)
}
}
// Wait for webservice to end
<-WebService.QuitChan()
return nil
}