go.ligato.io/vpp-agent/v3@v3.5.0/pkg/graphviz/graphviz_nocgo.go (about)

     1  //  Copyright (c) 2020 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  //go:build !cgo
    16  
    17  package graphviz
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"os/exec"
    23  )
    24  
    25  // TODO: add fallback that uses online tool: https://dreampuf.github.io/GraphvizOnline/
    26  
    27  func RenderFilename(outfname, format string, dot []byte) error {
    28  	cmd := exec.Command("dot", fmt.Sprintf("-T%s", format), "-o", outfname)
    29  	cmd.Stdin = bytes.NewReader(dot)
    30  	if _, err := cmd.Output(); err != nil {
    31  		return err
    32  	}
    33  
    34  	return nil
    35  }
    36  
    37  func RenderDot(dot []byte) ([]byte, error) {
    38  	cmd := exec.Command("dot", "-Tdot")
    39  	cmd.Stdin = bytes.NewReader(dot)
    40  	out, err := cmd.Output()
    41  	if err != nil {
    42  		return out, err
    43  	}
    44  
    45  	return out, nil
    46  }