当前位置: 首页>>代码示例>>Golang>>正文


Golang Graph.SetDir方法代码示例

本文整理汇总了Golang中github.com/awalterschulze/gographviz.Graph.SetDir方法的典型用法代码示例。如果您正苦于以下问题:Golang Graph.SetDir方法的具体用法?Golang Graph.SetDir怎么用?Golang Graph.SetDir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/awalterschulze/gographviz.Graph的用法示例。


在下文中一共展示了Graph.SetDir方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
	var (
		bytes   []byte
		data    map[string]service
		err     error
		graph   *gographviz.Graph
		project string
	)
	logger = stdlog.GetFromFlags()
	project = ""

	// Load docker-compose.yml
	bytes, err = ioutil.ReadFile("docker-compose.yml")
	if err != nil {
		abort(err.Error())
	}

	// Parse it as YML
	data = make(map[string]service, 5)
	yaml.Unmarshal(bytes, &data)
	if err != nil {
		abort(err.Error())
	}

	// Create directed graph
	graph = gographviz.NewGraph()
	graph.SetName(project)
	graph.SetDir(true)

	// Add legend
	graph.AddSubGraph(project, "cluster_legend", map[string]string{"label": "Legend"})
	graph.AddNode("cluster_legend", "legend_service", map[string]string{"label": "service"})
	graph.AddNode("cluster_legend", "legend_service_with_ports",
		map[string]string{
			"label": "\"service with exposed ports\\n80:80 443:443\\n--volume1[:host_dir1]\\n--volume2[:host_dir2]\"",
			"shape": "box"})
	graph.AddEdge("legend_service", "legend_service_with_ports", true, map[string]string{"label": "links"})
	graph.AddEdge("legend_service_with_ports", "legend_service", true, map[string]string{"label": "volumes_from", "style": "dashed"})

	// Round one: populate nodes
	for name, service := range data {
		var attrs = map[string]string{"label": name}
		if service.Ports != nil {
			attrs["label"] += "\\n" + strings.Join(service.Ports, " ")
			attrs["shape"] = "box"
		}
		if service.Volumes != nil {
			attrs["label"] += "\\n--" + strings.Join(service.Volumes, "\\n--")
		}
		attrs["label"] = fmt.Sprintf("\"%s\"", attrs["label"])
		graph.AddNode(project, name, attrs)
	}

	// Round two: populate connections
	for name, service := range data {
		// links
		if service.Links != nil {
			for _, linkTo := range service.Links {
				if strings.Contains(linkTo, ":") {
					linkTo = strings.Split(linkTo, ":")[0]
				}
				graph.AddEdge(name, linkTo, true, nil)
			}
		}
		// volumes_from
		if service.VolumesFrom != nil {
			for _, linkTo := range service.VolumesFrom {
				graph.AddEdge(name, linkTo, true, map[string]string{"style": "dotted"})
			}
		}
	}

	fmt.Print(graph)
}
开发者ID:t0mk,项目名称:docker-compose-graphviz,代码行数:74,代码来源:docker-compose-graphviz.go


注:本文中的github.com/awalterschulze/gographviz.Graph.SetDir方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。