Example #1
0
func main() {
	flag.Parse()
	log.SetFlags(0)

	// Variable whose methods we want to wrap
	v := flag.Arg(0)

	gen, err := exportdefault.New(".", v)
	if err != nil {
		log.Fatal(err)
	}

	gen.FuncNamePrefix = *pref

	if expr, err := regexp.Compile(*include); *include != "" && err == nil {
		gen.Include = expr
	} else if *include != "" {
		log.Fatalf("-include contains a invalid regular expression: %s", err.Error())
	}

	if expr, err := regexp.Compile(*include); *exclude != "" && err == nil {
		gen.Exclude = expr
	} else if *exclude != "" {
		log.Fatalf("-exclude contains a invalid regular expression: %s", err.Error())
	}

	w := os.Stdout
	if *out == "" {
		*out = fmt.Sprintf("%s_funcs.go", v)
	}
	if *out != "-" {
		log.Printf("Generating funcs for %s", v)
		*out = strconv.SnakeCase(*out)
		w, err = os.OpenFile(*out, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
		if err != nil {
			log.Fatal(err)
		}
	}

	err = gen.Write(w)
	if err != nil {
		log.Fatal(err)
	}
}
Example #2
0
func main() {
	flag.Parse()
	log.SetFlags(0)

	iface := flag.Arg(0)

	if iface == "" {
		log.Fatal("need to specify an interface name")
	}

	parts := strings.Split(iface, ".")
	switch len(parts) {
	case 1:
	case 2:
		if *pkg != "." {
			log.Fatalf("unexpected -pkg value (%q), package is already defined in the interface name as %s", *pkg, parts[0])
		}
		*pkg = parts[0]
		iface = parts[1]
	default:
		log.Fatalf("invalid interface %q", iface)
	}

	gen, err := automock.NewGenerator(*pkg, iface)
	if err != nil {
		log.Fatal(err)
	}

	gen.SetTemplate(template)

	if *mockName != "" {
		gen.SetName(*mockName)
	}
	inPkg := *pkg == "." && path.Dir(*out) == "."
	gen.SetInternal(inPkg)
	if *mockPkg == "" && !inPkg {
		p, err := importer.Default().Import(".")
		if err != nil {
			log.Fatal(err)
		}
		*mockPkg = p.Name()
	}
	if *mockPkg != "" {
		gen.SetPackage(*mockPkg)
	}

	w := os.Stdout
	if *out == "" {
		*out = fmt.Sprintf("%s_test.go", gen.Name())
		if p := regexp.MustCompile(".*/").ReplaceAllString(*pkg, ""); !inPkg && p != "" && p != "." {
			*out = p + "_" + *out
		}
	}
	if *out != "-" {
		*out = strconv.SnakeCase(*out)
		log.Printf("Generating mock for %s in %s", iface, *out)
		w, err = os.OpenFile(*out, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
		if err != nil {
			log.Fatal(err)
		}
	}

	err = gen.Write(w)
	switch err := err.(type) {
	case automock.GenerationError:
		log.Println(err.CodeWithLineNumbers())
		log.Fatal(err)
	case error:
		log.Fatal(err)
	}
}