Skip to content
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

User journey tests and documentation update #25

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .hookz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@
exec: go
args: ["tool", "cover", "-func=coverage.out"]
- name: "syft: Generate a Software Bill of Materials (SBoM)"
exec: syft
args: [".", "-o", "cyclonedx-json=sbom/gardener.cyclonedx.json"]
script: "
#!/bin/bash \n
syft . -o cyclonedx-json=sbom/hookz.cyclonedx.json &> /dev/null \n
"
- name: "gardener: Generate image links for PlantUML diagrams"
exec: ./gardener
args: ["generate", "--deflate", "."]
- name: "git: Add all changed files during the pre-commit stage"
exec: git
args: ["add", "."]
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A utility that renders PlantUML diagram source in Markdown image tags.

[PlantUML](https://plantuml.com/) is a great application that allows users to create "Diagrams as Code". It has a simple syntax, is a fantastic way to render any diagram, and abstracts a lot of the tedious work needed to draw diagrams in an editor.

At [DKFM](https://github.com/devops-kung-fu), when we develop PlantUML documents, we like to have a way for readers to view them without having to download code and generate diagrams. ```gardener``` helps us do this. We can either run the executable manually, or use a pre-commit hook helper like [hooks](https://github.com/devops-kung-fu/hookz) to continuously generate diagrams before code is committed to a central repository.
At [DKFM](https://github.com/devops-kung-fu), when we develop PlantUML documents, we like to have a way for readers to view them in our Markdown without having to download code and generate diagrams themselves. ```gardener``` helps us do this. We can either run the executable manually, or use a pre-commit hook helper like [hooks](https://github.com/devops-kung-fu/hookz) to continuously generate diagrams before code is committed to a central repository.

## Installation

Expand Down Expand Up @@ -84,12 +84,21 @@ Alice <-- Bob: Another authentication Response
``` markdown
# My Markdown File

Below is the image tag that gardner will update
Below is the image tag that gardener will update

![diagrams/src/alicenbob.pu]()
![diagrams/src/aliceandbob.pu]()

```

Gardener will change that link to:

```
![diagrams/src/aliceandbob.pu](http://www.plantuml.com/plantuml/png/1C3HQSGm40JGVrKn3QW14uwJ8Gqu5SZAW1RaNPyrWfIVzvwgZzOry6FNFOZllthTWwaCQqH-OOzpGrdgT5chid0S_2nEfJrEvFn26TQueInrkgnLMJWEpZjyCgKoh17vXJtE3SL3dG_gKgVBMfM5u-1dSIgLOOtuoXbxd1kAXpeVrALEbxKg2yV1UOTFfZVzTrqZ_GC00F__0G00__y=)
```
Which will render like this:

![diagrams/src/simple.pu](http://www.plantuml.com/plantuml/png/1C3HQSGm40JGVrKn3QW14uwJ8Gqu5SZAW1RaNPyrWfIVzvwgZzOry6FNFOZllthTWwaCQqH-OOzpGrdgT5chid0S_2nEfJrEvFn26TQueInrkgnLMJWEpZjyCgKoh17vXJtE3SL3dG_gKgVBMfM5u-1dSIgLOOtuoXbxd1kAXpeVrALEbxKg2yV1UOTFfZVzTrqZ_GC00F__0G00__y=)

## Usage

### CLI Usage
Expand Down Expand Up @@ -141,22 +150,25 @@ hooks:
- type: pre-commit
actions:
- name: "Generate Images for PlantUML"
url: https://github.com/devops-kung-fu/gardener/releases/download/v1.0.0/gardener-1.0.0-linux-amd64
args: ["deflate"]
exec: gardener
args: ["generate", "--deflate", "."]
```

## Examples

Here are some examples from processed from [diagrams/src/example1.pu](diagrams/src/example1.pu) and [diagrams/src/example2.pu](diagrams/src/example2.pu). View the source of this README.md page to see the URL's that ```gardener``` has created.

`diagrams/src/example1.pu`
`diagrams/src/sequence.pu`

![diagrams/src/sequence.pu](http://www.plantuml.com/plantuml/png/1C3DRXJD3WRWVLs55TQpO8N8OaJ_PAGZHQZ526NzuZPphEvskLskGBZwx_dM4f7zsEakPnTZeLdn21pqyXU7x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3nKE33-hMeCjwbONZvdTpAPLfdVZB6LUq0yL3Wm_grg3BUfM5u-RwX2-c5_r_lsVw0G00__y1003__m==)
`diagrams/src/class.pu`

`diagrams/src/example2.pu`
![diagrams/src/class.pu](http://www.plantuml.com/plantuml/png/1C3DQjj04C3n-pp5_sXZjD072AIqQM-vc1vo4eCqbGNx8NPcxPgGT-_lrKDxZ9BvaTMTdxjkNGjl_xGSsMJ9wiwlrhxnAG0VRR1ePRW9WDlTkcRoNiq582K1KEVP1W-j8G1QLxRUnY40Boy24BUzRawqpU9cFGd0F0l0HniiMhdftL2dQ7sozJOE5m1rdctmq1e2C4q2y5PNsb-MhExo9R9aTUUFM-TJ05BYkfSZ6xyjmZef2S25pU9TYvtE0d1Xi_Y-hjtSJsS1I8bhAuPRX7LI4e0BRl6knKvd0PXdZhxVDOnLGvbd0TXhe9i9m3HXTUdF8snbcWJW6dsl6uUwFrfVvKjUhQwZvFy0003__m400F__)

![diagrams/src/example2.pu](http://www.plantuml.com/plantuml/png/1C3HZSCW40JGVwgO1cZ0Ebd19RW3u4PY9RARcA7_lDTIVRJVCvLfdSWdhcW7ojQWotgLXUFcTtCfNT6GyuaohVD0sHfqMQ-oSDnSd_35bCgqJkGJLxG3nKE33-hMeCjwbONZvdTpAPLfdVZB6LUq0yL3Wm_grg3BUfM5u-RwX2-c5_r_lsVw0G00__y1003__m==)
`diagrams/src/ditta.pu`

![diagrams/src/ditaa.pu](http://www.plantuml.com/plantuml/png/1C0nIbnR083X_gpYipxy6tZ5Gy4KWPJMWOipaI5U9-WT8kHax_dkFuxj_RZjhnw_FZqy-FAvxRzUp-DqERPjpAggYLbLJPXLrHmBO5RJwN9icrd_Mu0r5a_dpuE5MLLDMAVBzlA-xMkin-lpRJ-_7GlhRjzUBizt2nPWZGN-dFukBC221LXZLhDgObPL4sPLDGVy1m00uFk01VXsFJvSV_XzVVy9h06pggggggggEUxFRwVR_leF003__m400F__)

## Software Bill of Materials

Expand Down
36 changes: 6 additions & 30 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gookit/color"
"github.com/spf13/cobra"

src "github.com/devops-kung-fu/gardener/lib"
"github.com/devops-kung-fu/gardener/lib"
)

var (
Expand Down Expand Up @@ -37,45 +37,21 @@ func init() {
}

func generate(path string) {
diagramFiles, e := src.FindFiles(Afs, path, ".*\\.(pu|puml|plantuml|iuml|wsd)")
diagramFiles, e := lib.FindFiles(Afs, path, ".*\\.(pu|puml|plantuml|iuml|wsd)")
if e != nil {
log.Fatal(e)
}
util.DoIf(Verbose, func() {
color.Style{color.FgLightBlue, color.OpBold}.Print("Generating Links...\n\n")
util.PrintInfo(fmt.Sprintf("Found %x diagrams", len(diagramFiles)))
for _, file := range diagramFiles {
util.PrintTabbed(file)
}
util.PrintInfo("Processing Markdown files")
})

markdownFiles, e := src.FindFiles(Afs, path, ".*\\.md")
if e != nil {
log.Fatal(e)
}
for _, markdownFile := range markdownFiles {
util.DoIf(Verbose, func() {
log.Print("Working on ", markdownFile)
util.PrintTabbed(markdownFile)
})
_, _ = lib.Generate(Afs, diagramFiles, path, Verbose, deflate)

for _, diagramFile := range diagramFiles {
diagramContent, err := src.ReadFileContentBytes(Afs, diagramFile)
if util.IsErrorBool(err) {
log.Fatal(err)
}
var url string
if deflate {
log.Print("Deflate Encoding Diagram for: ", diagramFile)
url = src.DeflateEncodedURL(diagramContent)
} else {
log.Print("Hex Encoding Diagram for: ", diagramFile)
url = src.HexEncodedURL(diagramContent)
}

searchImageStub := fmt.Sprintf("\\!\\[%s\\]\\(.*\\)", diagramFile)
replaceImageStub := fmt.Sprintf("![%s](%s)", diagramFile, url)
_, _ = src.ReplaceLineInFile(Afs, markdownFile, searchImageStub, replaceImageStub)
}
}
util.DoIf(Verbose, func() {
util.PrintSuccess("Done!\n")
})
Expand Down
28 changes: 28 additions & 0 deletions diagrams/src/class.pu
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@startuml Class Diagram Example
class Foo1 {
You can use
several lines
..
as you want
and group
==
things together.
__
You can have as many groups
as you want
--
End of class
}

class User {
.. Simple Getter ..
+ getName()
+ getAddress()
.. Some setter ..
+ setName()
__ private data __
int age
-- encrypted --
String password
}
@enduml
12 changes: 12 additions & 0 deletions diagrams/src/ditaa.pu
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@startuml DITAA Example
ditaa
+--------+ +-------+ +-------+
| +---+ ditaa +--> | |
| Text | +-------+ |diagram|
|Document| |!magic!| | |
| {d}| | | | |
+---+----+ +-------+ +-------+
: ^
| Lots of work |
+-------------------------+
@enduml
7 changes: 0 additions & 7 deletions diagrams/src/example2.pu

This file was deleted.

2 changes: 1 addition & 1 deletion diagrams/src/test.pu → diagrams/src/sequence.pu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@startuml
@startuml Sequence Diagram Example
skinparam backgroundColor #EEEBDC
skinparam handwritten true

Expand Down
2 changes: 1 addition & 1 deletion diagrams/src/example1.pu → diagrams/src/simple.pu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@startuml
@startuml Simple Example
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response

Expand Down
2 changes: 1 addition & 1 deletion lib/gardener.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package src
package lib

import (
"bytes"
Expand Down
7 changes: 4 additions & 3 deletions lib/gardener_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package src
package lib

import (
"testing"
Expand Down Expand Up @@ -61,8 +61,9 @@ func TestHexEncodedURL(t *testing.T) {

func TestReplaceLineInFile(t *testing.T) {
afs := &afero.Afero{Fs: afero.NewMemMapFs()}
afs.WriteFile("README.md", []byte("\\!\\[example.pu\\]\\(.*\\)"), 0644)
result, err := ReplaceLineInFile(afs, "README.md", "\\!\\[example.pu\\]\\(.*\\)", "![example.pu](https://example.com)")

afs.WriteFile("README.md", []byte("![example.pu]()"), 0644)
result, err := ReplaceLineInFile(afs, "README.md", "![example.pu]()", "![example.pu](https://example.com)")

assert.True(t, result)
assert.NoError(t, err)
Expand Down
43 changes: 43 additions & 0 deletions lib/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lib

import (
"fmt"
"log"

"github.com/devops-kung-fu/common/util"
"github.com/spf13/afero"
)

func Generate(afs *afero.Afero, diagramFiles []string, path string, verbose bool, deflate bool) (markdownFiles []string, err error) {
markdownFiles, err = FindFiles(afs, path, ".*\\.md")
if err != nil {
log.Fatal(err)
}
for _, markdownFile := range markdownFiles {
util.DoIf(verbose, func() {
log.Print("Working on ", markdownFile)
util.PrintTabbed(markdownFile)
})

for _, diagramFile := range diagramFiles {
diagramContent, err := ReadFileContentBytes(afs, diagramFile)
if util.IsErrorBool(err) {
log.Fatal(err)
}
var url string
if deflate {

log.Print("Deflate Encoding Diagram for: ", diagramFile)
url = DeflateEncodedURL(diagramContent)
} else {
log.Print("Hex Encoding Diagram for: ", diagramFile)
url = HexEncodedURL(diagramContent)
}

searchImageStub := fmt.Sprintf("\\!\\[%s\\]\\(.*\\)", diagramFile)
replaceImageStub := fmt.Sprintf("![%s](%s)", diagramFile, url)
_, _ = ReplaceLineInFile(afs, markdownFile, searchImageStub, replaceImageStub)
}
}
return
}
37 changes: 37 additions & 0 deletions lib/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lib

import (
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

func TestGenerate(t *testing.T) {
afs := &afero.Afero{Fs: afero.NewMemMapFs()}
afs.WriteFile("example.pu", samplePlantUMLFile(), 0644)
afs.WriteFile("README.md", []byte("![example.pu]()"), 0644)

diagramFiles, err := FindFiles(afs, ".", ".*\\.(pu|puml|plantuml|iuml|wsd)")

assert.NoError(t, err)
result, err := Generate(afs, diagramFiles, ".", true, true)

assert.NoError(t, err)
assert.NotNil(t, result)
file, _ := afs.ReadFile("README.md")
assert.Equal(t, file, []byte("![example.pu](http://www.plantuml.com/plantuml/png/1C3XQSGm30NWt_8KRm4l4Cfn3dI1T0B7VM218-LY6Jf-loJodb6VMDV0Zrxt8Bx_wdKF9f4oj17vXTtF3ML5fuMs6kg6Wv_56KbaznCvlr26DcueApejjDLGDnoSjzjaIY9bQ2Fo2xkV6ufvT3weApejjDLGDnpyBCv88cLe8xvonkxdXc8UdG_gYauBRJNK3GVdxJQPHDwqttMD9Fy0003__m400F__)"))
}

func samplePlantUMLFile() []byte {
test := `
@startuml Simple Example
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response

Alice -> Bob: Another authentication Request
Alice <-- Bob: Another authentication Response
@enduml
`
return []byte(test)
}
Loading