@@ -15,10 +15,12 @@ import (
15
15
"oss.terrastruct.com/d2/lib/textmeasure"
16
16
)
17
17
18
+ // D2LangFormatter is struct defined for D2 Language Output use-case
18
19
type D2LangFormatter struct {
19
20
config * Config
20
21
}
21
22
23
+ // Format the data to D2 Language and output it to a D2 Language file
22
24
func (f * D2LangFormatter ) Format (td * TemplateData , templateContent string ) (err error ) {
23
25
ruler , _ := textmeasure .NewRuler ()
24
26
layoutResolver := func (engine string ) (d2graph.LayoutGraph , error ) {
@@ -50,22 +52,22 @@ func (f *D2LangFormatter) Format(td *TemplateData, templateContent string) (err
50
52
return err
51
53
}
52
54
53
- hostId := hex .EncodeToString (fnv .Sum (nil ))
54
- graph , _ , _ = d2oracle .Create (graph , nil , hostId )
55
- graph , _ = d2oracle .Set (graph , nil , hostId + ".label" , nil , & hostLabel )
56
- graph , _ = d2oracle .Set (graph , nil , "nmap -> " + hostId , nil , nil )
55
+ hostID := hex .EncodeToString (fnv .Sum (nil ))
56
+ graph , _ , _ = d2oracle .Create (graph , nil , hostID )
57
+ graph , _ = d2oracle .Set (graph , nil , hostID + ".label" , nil , & hostLabel )
58
+ graph , _ = d2oracle .Set (graph , nil , "nmap -> " + hostID , nil , nil )
57
59
58
60
for j := range host .Port {
59
61
port := & host .Port [j ]
60
- portId := fmt .Sprintf ("%s-port%d" , hostId , port .PortID )
61
- graph , _ , _ = d2oracle .Create (graph , nil , portId )
62
+ portID := fmt .Sprintf ("%s-port%d" , hostID , port .PortID )
63
+ graph , _ , _ = d2oracle .Create (graph , nil , portID )
62
64
portLabel := fmt .Sprintf ("%d/%s\n %s\n %s" , port .PortID , port .Protocol , port .State .State , port .Service .Name )
63
- graph , _ = d2oracle .Set (graph , nil , portId + ".label" , nil , & portLabel )
65
+ graph , _ = d2oracle .Set (graph , nil , portID + ".label" , nil , & portLabel )
64
66
shape := "circle"
65
- graph , _ = d2oracle .Set (graph , nil , portId + ".shape" , nil , & shape )
67
+ graph , _ = d2oracle .Set (graph , nil , portID + ".shape" , nil , & shape )
66
68
width := "25"
67
- graph , _ = d2oracle .Set (graph , nil , portId + ".width" , nil , & width )
68
- graph , _ = d2oracle .Move (graph , nil , portId , hostId + "." + portId , true )
69
+ graph , _ = d2oracle .Set (graph , nil , portID + ".width" , nil , & width )
70
+ graph , _ = d2oracle .Move (graph , nil , portID , hostID + "." + portID , true )
69
71
}
70
72
}
71
73
_ , err = f .config .Writer .Write ([]byte (d2format .Format (graph .AST )))
0 commit comments