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

	if *outputDir == "" {
		log.Fatal("You must provide a non-empty --output_dir")
	}

	ext := golang.Extractor{
		BuildContext: bc,
		Corpus:       *corpus,
		LocalPath:    *localPath,
	}
	if cleanup, err := setupCampfire(&ext); err != nil {
		log.Fatalf("Error enabling campfire support: %v", err)
	} else {
		defer cleanup()
	}
	locate := ext.Locate
	if *byDir {
		locate = ext.ImportDir
	}
	for _, path := range flag.Args() {
		pkg, err := locate(path)
		if err == nil {
			maybeLog("Found %q in %s", pkg.Path, pkg.BuildPackage.Dir)
		} else {
			maybeFatal("Error locating %q: %v", path, err)
		}
	}

	if err := ext.Extract(); err != nil {
		maybeFatal("Error in extraction: %v", err)
	}

	pack, err := indexpack.CreateOrOpen(context.Background(), *outputDir,
		indexpack.UnitType((*apb.CompilationUnit)(nil)))
	if err != nil {
		log.Fatalf("Unable to open %q: %v", *outputDir, err)
	}

	maybeLog("Writing %d package(s) to %q", len(ext.Packages), pack.Root())
	for _, pkg := range ext.Packages {
		maybeLog("Package %q:\n\t// %s", pkg.Path, pkg.BuildPackage.Doc)
		uf, err := pkg.Store(context.Background(), pack)
		if err != nil {
			maybeFatal("Error writing %q: %v", pkg.Path, err)
		} else {
			maybeLog("Output: %v\n", strings.Join(uf, ", "))
		}
	}
}
Example #2
0
func main() {
	flag.Parse()

	if *toArchive != "" && *fromArchive != "" {
		fmt.Fprintln(os.Stderr, "ERROR: --to_archive and --from_archive are mutually exclusive")
		flag.Usage()
	} else if *toArchive != "" && *viewArchive != "" {
		fmt.Fprintln(os.Stderr, "ERROR: --to_archive and --view_archive are mutually exclusive")
		flag.Usage()
	} else if *fromArchive != "" && *viewArchive != "" {
		fmt.Fprintln(os.Stderr, "ERROR: --from_archive and --view_archive are mutually exclusive")
		flag.Usage()
	} else if *toArchive == "" && *fromArchive == "" && *viewArchive == "" {
		fmt.Fprintln(os.Stderr, "ERROR: One of [--to_archive --from_archive --view_archive] must be specified")
		flag.Usage()
	}

	archiveRoot := *toArchive
	if archiveRoot == "" {
		archiveRoot = *fromArchive
	}
	if archiveRoot == "" {
		archiveRoot = *viewArchive
	}

	ctx := context.Background()
	var err error

	opts := []indexpack.Option{indexpack.UnitType(apb.CompilationUnit{})}
	if strings.HasPrefix(archiveRoot, "gs://") {
		path := strings.Trim(strings.TrimPrefix(archiveRoot, "gs://"), "/")
		parts := strings.SplitN(path, "/", 2)
		ctx, err = oauth2Config.Context(ctx)
		if err != nil {
			log.Fatal(err)
		}
		opts = append(opts, indexpack.FS(gcs.FS{parts[0]}))
		if len(parts) == 2 {
			archiveRoot = parts[1]
		} else {
			archiveRoot = "/"
		}
	}

	pack, err := indexpack.CreateOrOpen(ctx, archiveRoot, opts...)
	if err != nil {
		log.Fatalf("Error opening indexpack at %q: %v", archiveRoot, err)
	}

	if *toArchive != "" {
		if len(flag.Args()) == 0 {
			log.Println("WARNING: no kindex file paths given")
		}
		for _, path := range flag.Args() {
			kindex, err := kindex.Open(ctx, path)
			if err != nil {
				log.Fatalf("Error opening kindex at %q: %v", path, err)
			}
			if err := packIndex(ctx, pack, kindex); err != nil {
				log.Fatalf("Error packing kindex at %q into %q: %v", path, pack.Root(), err)
			}
		}
	} else if *fromArchive != "" {
		var dir string
		if len(flag.Args()) > 1 {
			fmt.Fprintf(os.Stderr, "ERROR: Too many positional arguments for --from_archive: %v\n", flag.Args())
			flag.Usage()
		} else if len(flag.Args()) == 1 {
			dir = flag.Arg(0)
			if err := os.MkdirAll(dir, os.ModePerm); err != nil {
				log.Fatalf("Error creating directory %q: %v", dir, err)
			}
		}
		if err := unpackIndex(ctx, pack, dir); err != nil {
			log.Fatalf("Error unpacking compilation units at %q: %v", pack.Root(), err)
		}
	} else {
		en := json.NewEncoder(os.Stdout)
		fetcher := pack.Fetcher(ctx)
		displayCompilation := func(i interface{}) error {
			cu := i.(*apb.CompilationUnit)
			if *printFiles {
				idx, err := kindex.FromUnit(cu, fetcher)
				if err != nil {
					return fmt.Errorf("error reading files for compilation: %v", err)
				}
				return en.Encode(idx)
			}
			return en.Encode(cu)
		}
		if len(flag.Args()) == 0 {
			if err := pack.ReadUnits(ctx, formatKey, displayCompilation); err != nil {
				log.Fatal(err)
			}
		} else {
			for _, digest := range flag.Args() {
				if err := pack.ReadUnit(ctx, formatKey, digest, displayCompilation); err != nil {
					log.Fatal(err)
				}
			}
		}
	}
}