blob: 68076044a649acf59216b494da3ec528626e578a [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"strings"
"fuchsia.googlesource.com/tools/gndoc"
)
type stringsFlag []string
func (s *stringsFlag) String() string {
return strings.Join(*s, ", ")
}
func (s *stringsFlag) Set(value string) error {
*s = append(*s, value)
return nil
}
var (
keyArgs stringsFlag
inputFiles stringsFlag
outFile string
sourcesFile string
)
func init() {
flag.Var(&keyArgs, "key", "a label for output")
flag.Var(&inputFiles, "in", "path to an input file")
flag.StringVar(&outFile, "out", "", "path to output file (default stdout)")
flag.StringVar(&sourcesFile, "s", "", "path to json file generated by Jiri projects")
}
func main() {
flag.Parse()
if flag.NArg() != 0 {
flag.PrintDefaults()
os.Exit(2)
}
if sourcesFile == "" {
fmt.Printf("Error: no Jiri project reference file specified.\nTo generate, use jiri project -json-output=<file>.\n")
os.Exit(1)
}
// Open the json file.
sources, err := os.Open(sourcesFile)
if err != nil {
os.Exit(1)
}
defer sources.Close()
ctx := context.Background()
sourceMap := gndoc.NewSourceMap(sources)
argMap := gndoc.NewArgMap(len(inputFiles), sourceMap)
args, keys, errs := gndoc.ParseGNArgs(ctx, inputFiles, keyArgs)
argMap.AddArgs(args, keys)
if err := <-errs; err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
var out io.Writer
if outFile != "" {
var err error
out, err = os.Create(outFile)
if err != nil {
fmt.Printf("Error opening file: %s", err)
os.Exit(1)
}
} else {
out = os.Stdout
}
argMap.EmitMarkdown(out)
}