-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Architecture Diagrams #34
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
## Architecture Diagram | ||
```mermaid | ||
architecture-beta | ||
service left_disk(disk)[Disk] | ||
service top_disk(disk)[Disk] | ||
service bottom_disk(disk)[Disk] | ||
service top_gateway(internet)[Gateway] | ||
service bottom_gateway(internet)[Gateway] | ||
junction junctionCenter | ||
junction junctionRight | ||
|
||
left_disk:R -- L:junctionCenter | ||
top_disk:B -- T:junctionCenter | ||
bottom_disk:T -- B:junctionCenter | ||
junctionCenter:R -- L:junctionRight | ||
top_gateway:B -- T:junctionRight | ||
bottom_gateway:T -- B:junctionRight | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//go:build linux || darwin | ||
|
||
// Package main is generating mermaid sequence diagram. | ||
package main | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/nao1215/markdown" | ||
"github.com/nao1215/markdown/mermaid/arch" | ||
) | ||
|
||
//go:generate go run main.go | ||
|
||
func main() { | ||
f, err := os.Create("generated.md") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
|
||
diagram := arch.NewArchitecture(io.Discard). | ||
Service("left_disk", arch.IconDisk, "Disk"). | ||
Service("top_disk", arch.IconDisk, "Disk"). | ||
Service("bottom_disk", arch.IconDisk, "Disk"). | ||
Service("top_gateway", arch.IconInternet, "Gateway"). | ||
Service("bottom_gateway", arch.IconInternet, "Gateway"). | ||
Junction("junctionCenter"). | ||
Junction("junctionRight"). | ||
LF(). | ||
Edges( | ||
arch.Edge{ | ||
ServiceID: "left_disk", | ||
Position: arch.PositionRight, | ||
Arrow: arch.ArrowNone, | ||
}, | ||
arch.Edge{ | ||
ServiceID: "junctionCenter", | ||
Position: arch.PositionLeft, | ||
Arrow: arch.ArrowNone, | ||
}). | ||
Edges( | ||
arch.Edge{ | ||
ServiceID: "top_disk", | ||
Position: arch.PositionBottom, | ||
Arrow: arch.ArrowNone, | ||
}, | ||
arch.Edge{ | ||
ServiceID: "junctionCenter", | ||
Position: arch.PositionTop, | ||
Arrow: arch.ArrowNone, | ||
}). | ||
Edges( | ||
arch.Edge{ | ||
ServiceID: "bottom_disk", | ||
Position: arch.PositionTop, | ||
Arrow: arch.ArrowNone, | ||
}, | ||
arch.Edge{ | ||
ServiceID: "junctionCenter", | ||
Position: arch.PositionBottom, | ||
Arrow: arch.ArrowNone, | ||
}). | ||
Edges( | ||
arch.Edge{ | ||
ServiceID: "junctionCenter", | ||
Position: arch.PositionRight, | ||
Arrow: arch.ArrowNone, | ||
}, | ||
arch.Edge{ | ||
ServiceID: "junctionRight", | ||
Position: arch.PositionLeft, | ||
Arrow: arch.ArrowNone, | ||
}). | ||
Edges( | ||
arch.Edge{ | ||
ServiceID: "top_gateway", | ||
Position: arch.PositionBottom, | ||
Arrow: arch.ArrowNone, | ||
}, | ||
arch.Edge{ | ||
ServiceID: "junctionRight", | ||
Position: arch.PositionTop, | ||
Arrow: arch.ArrowNone, | ||
}). | ||
Edges( | ||
arch.Edge{ | ||
ServiceID: "bottom_gateway", | ||
Position: arch.PositionTop, | ||
Arrow: arch.ArrowNone, | ||
}, | ||
arch.Edge{ | ||
ServiceID: "junctionRight", | ||
Position: arch.PositionBottom, | ||
Arrow: arch.ArrowNone, | ||
}).String() //nolint | ||
|
||
err = markdown.NewMarkdown(f). | ||
H2("Architecture Diagram"). | ||
CodeBlocks(markdown.SyntaxHighlightMermaid, diagram). | ||
Build() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Package arch is mermaid architecture diagram builder. | ||
// The building blocks of an architecture are groups, services, edges, and junctions. | ||
// The arch package incorporates beta features of Mermaid, so the specifications are subject to significant changes. | ||
package arch | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/nao1215/markdown/internal" | ||
) | ||
|
||
// Architecture is a architecture diagram builder. | ||
type Architecture struct { | ||
// body is architecture diagram body. | ||
body []string | ||
// config is the configuration for the architecture diagram. | ||
config *config | ||
// dest is output destination for architecture diagram body. | ||
dest io.Writer | ||
// err manages errors that occur in all parts of the architecture building. | ||
err error | ||
} | ||
|
||
// NewArchitecture returns a new Architecture. | ||
func NewArchitecture(w io.Writer, opts ...Option) *Architecture { | ||
c := newConfig() | ||
|
||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
|
||
return &Architecture{ | ||
body: []string{"architecture-beta"}, | ||
dest: w, | ||
config: c, | ||
} | ||
} | ||
|
||
// String returns the architecture diagram body. | ||
func (a *Architecture) String() string { | ||
return strings.Join(a.body, internal.LineFeed()) | ||
} | ||
|
||
// Build writes the architecture diagram body to the output destination. | ||
func (a *Architecture) Build() error { | ||
if _, err := a.dest.Write([]byte(a.String())); err != nil { | ||
if a.err != nil { | ||
return fmt.Errorf("failed to write: %w: %s", err, a.err.Error()) //nolint:wrapcheck | ||
} | ||
return fmt.Errorf("failed to write: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// Error returns the error that occurred during the architecture diagram building. | ||
func (a *Architecture) Error() error { | ||
return a.err | ||
} | ||
|
||
// LF add a line feed to the architecture diagram body. | ||
func (a *Architecture) LF() *Architecture { | ||
a.body = append(a.body, "") | ||
return a | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace panic with proper error handling
Using panic for error handling is not recommended. Consider returning errors instead.
Also applies to: 99-106