-
Notifications
You must be signed in to change notification settings - Fork 11
/
start.go
executable file
·80 lines (71 loc) · 2.51 KB
/
start.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/akwick/gotcha/worklist"
)
func main() {
// Explains how to use the command line flags
flag.Usage = func() {
fmt.Printf("Usage and defaults of %s: \n", os.Args[0])
fmt.Printf("The flags allpkgs, path and ssf are optional. \n")
fmt.Printf("The flag sourceFilesFlag is mandatory.\n")
flag.PrintDefaults()
}
var ssf = flag.String("ssf", "./sourcesAndSinks.txt", "Changes the file which holds the sources and sinks")
var path = flag.String("path", "github.com/akwick/gotcha", "The path to the .go-files starting at $GOPATH/src: e.g. the path for $GOPATH/src/example/example.go will be example")
var sourceFilesFlag sourcefiles
var allpkgs = flag.Bool("allpkgs", false, "If it is set all packages of the source file will be analyzed, else only the main package.")
var pkgs = flag.String("pkgs", "", "Specify some packages in addition to the main package which should be analyzed.")
var ptr = flag.Bool("ptr", true, "If is is set we perfom a pointer analysis, else not")
flag.Var(&sourceFilesFlag, "src", "comma-seperated list of .go-files which should be analzed")
flag.Parse()
if !srcFlagCalled {
flag.PrintDefaults()
} else {
// analyse with the given values
err := worklist.DoAnalysis(*path, sourceFilesFlag, *ssf, *allpkgs, *pkgs, *ptr)
if err != nil {
switch err := err.(type) {
case *worklist.ErrInFlows:
fmt.Printf("err.NumberOfFlows: %d \n", err.NumberOfFlows())
fmt.Printf("err.Error() %s\n", err.Error())
default:
fmt.Printf("Errors: %+v\n", err)
os.Exit(1)
}
} else {
fmt.Printf("Gongrats. Gotcha has not found an error.\n")
fmt.Printf("Your parameters are: \n")
fmt.Printf("path: %s\n", *path)
fmt.Printf("source file: %s\n", sourceFilesFlag)
fmt.Printf("sources and sinks file: %s\n", *ssf)
os.Exit(0)
}
}
}
// sourcefiles is a flag type which handles multiple .go-Files
type sourcefiles []string
var srcFlagCalled = false
// String is part of the flag.Value interface
// The output will be used in diagnostics
func (s *sourcefiles) String() string {
return fmt.Sprint(*s)
}
// Set is part of the flag.Value interface and set the flag value
// Only files with suffix .go are allowed.
func (s *sourcefiles) Set(value string) error {
srcFlagCalled = true
for _, file := range strings.Split(value, ",") {
isGoFile := strings.HasSuffix(file, ".go")
if !isGoFile {
errorMessage := file + "is not a .go file"
return errors.New(errorMessage)
}
*s = append(*s, file)
}
return nil
}