// GET /api/graph.:ext func apiGraph(c *gin.Context) { var ( err error output []byte ) ext := c.Params.ByName("ext") factory := context.CollectionFactory() factory.RemoteRepoCollection().RLock() defer factory.RemoteRepoCollection().RUnlock() factory.LocalRepoCollection().RLock() defer factory.LocalRepoCollection().RUnlock() factory.SnapshotCollection().RLock() defer factory.SnapshotCollection().RUnlock() factory.PublishedRepoCollection().RLock() defer factory.PublishedRepoCollection().RUnlock() graph, err := deb.BuildGraph(factory) if err != nil { c.JSON(500, err) return } buf := bytes.NewBufferString(graph.String()) command := exec.Command("dot", "-T"+ext) command.Stderr = os.Stderr stdin, err := command.StdinPipe() if err != nil { c.Fail(500, err) return } _, err = io.Copy(stdin, buf) if err != nil { c.Fail(500, err) return } err = stdin.Close() if err != nil { c.Fail(500, err) return } output, err = command.Output() if err != nil { c.Fail(500, fmt.Errorf("unable to execute dot: %s (is graphviz package installed?)", err)) return } mimeType := mime.TypeByExtension("." + ext) if mimeType == "" { mimeType = "application/octet-stream" } c.Data(200, mimeType, output) }
func aptlyGraph(cmd *commander.Command, args []string) error { var err error if len(args) != 0 { cmd.Usage() return commander.ErrCommandError } fmt.Printf("Generating graph...\n") graph, err := deb.BuildGraph(context.CollectionFactory()) if err != nil { return err } buf := bytes.NewBufferString(graph.String()) tempfile, err := ioutil.TempFile("", "aptly-graph") if err != nil { return err } tempfile.Close() os.Remove(tempfile.Name()) format := context.Flags().Lookup("format").Value.String() output := context.Flags().Lookup("output").Value.String() if filepath.Ext(output) != "" { format = filepath.Ext(output)[1:] } tempfilename := tempfile.Name() + "." + format command := exec.Command("dot", "-T"+format, "-o"+tempfilename) command.Stderr = os.Stderr stdin, err := command.StdinPipe() if err != nil { return err } err = command.Start() if err != nil { return fmt.Errorf("unable to execute dot: %s (is graphviz package installed?)", err) } _, err = io.Copy(stdin, buf) if err != nil { return err } err = stdin.Close() if err != nil { return err } err = command.Wait() if err != nil { return err } if output != "" { err = utils.CopyFile(tempfilename, output) if err != nil { return fmt.Errorf("unable to copy %s -> %s: %s", tempfilename, output, err) } _ = os.Remove(tempfilename) fmt.Printf("Output saved to %s\n", output) } else { fmt.Printf("Rendered to %s file: %s, trying to open it...\n", format, tempfilename) _ = exec.Command("open", tempfilename).Run() } return err }