func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, genSwaggerDocLong+"\n", os.Args[0])
		fmt.Fprintf(os.Stderr, genSwaggerDocUsage+"\n", os.Args[0])
		fmt.Fprintf(os.Stderr, genSwaggerDocExamples+"\n", os.Args[0])
		fmt.Fprintln(os.Stderr, "Options:")
		flag.PrintDefaults()
		os.Exit(2)
	}

	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()

	var output io.Writer
	if outputFile == defaultOutputFile {
		output = os.Stdout
	} else {
		file, err := os.OpenFile(outputFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error reading output file: %v\n", err)
		}
		defer file.Close()
		output = file
	}

	documentationForTypes := kruntime.ParseDocumentationFrom(inputFile)

	if verify {
		if numMissingDocs, err := kruntime.VerifySwaggerDocsExist(documentationForTypes, output); err != nil {
			fmt.Fprintf(os.Stderr, "Error verifying documentation: %v\n", err)
			os.Exit(1)
		} else if numMissingDocs != 0 {
			fmt.Fprintf(os.Stderr, "Found %d types or fields missing documentation in %q.\n", numMissingDocs, inputFile)
			os.Exit(1)
		}
		fmt.Fprintf(os.Stdout, "Found no types or fields missing documentation in %q.\n", inputFile)
		os.Exit(0)
	}

	if err := kruntime.WriteSwaggerDocFunc(documentationForTypes, output); err != nil {
		fmt.Fprintf(os.Stderr, "Error writing generated Swagger documentation to file: %v\n", err)
		os.Exit(1)
	}
}
Example #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()

	if *typeSrc == "" {
		glog.Fatalf("Please define -s flag as it is the source file")
	}

	var funcOut io.Writer
	if *functionDest == "-" {
		funcOut = os.Stdout
	} else {
		file, err := os.Create(*functionDest)
		if err != nil {
			glog.Fatalf("Couldn't open %v: %v", *functionDest, err)
		}
		defer file.Close()
		funcOut = file
	}

	docsForTypes := kruntime.ParseDocumentationFrom(*typeSrc)

	if *verify == true {
		rc, err := kruntime.VerifySwaggerDocsExist(docsForTypes, funcOut)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error in verification process: %s\n", err)
		}
		os.Exit(rc)
	}

	if docsForTypes != nil && len(docsForTypes) > 0 {
		if err := kruntime.WriteSwaggerDocFunc(docsForTypes, funcOut); err != nil {
			fmt.Fprintf(os.Stderr, "Error when writing swagger documentation functions: %s\n", err)
			os.Exit(-1)
		}
	}
}