blob: 51866811684b17c2703c644824c8a36afef4a869 [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"
"io"
"log"
"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()
}
if sourcesFile == "" {
log.Fatalf("Error: no Jiri project reference file specified.\nTo generate, use jiri project -json-output=<file>.\n")
}
// Open the json file.
sources, err := os.Open(sourcesFile)
if err != nil {
log.Fatalf("Error: unable to open %s", sourcesFile)
}
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 {
log.Fatalf("Error: %s\n", err)
}
var out io.Writer
if outFile != "" {
var err error
out, err = os.Create(outFile)
if err != nil {
log.Fatalf("Error opening file: %s", err)
}
} else {
out = os.Stdout
}
argMap.EmitMarkdown(out)
}