Skip to content

Commit

Permalink
feat: Add Hub publisher (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
dobarx authored Nov 22, 2024
1 parent e91b708 commit 379e2f8
Show file tree
Hide file tree
Showing 21 changed files with 1,187 additions and 59 deletions.
8 changes: 8 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ packages:
config:
interfaces:
Client:
github.com/blackstork-io/fabric/internal/microsoft/client:
config:
interfaces:
Client:
github.com/blackstork-io/fabric/internal/builtin/hubapi:
config:
interfaces:
Client:
github.com/blackstork-io/fabric/internal/microsoft:
config:
interfaces:
Expand Down
69 changes: 69 additions & 0 deletions docs/plugins/builtin/publishers/hub.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: "`hub` publisher"
plugin:
name: blackstork/builtin
description: "Publish documents to Blackstork Hub."
tags: []
version: "v0.4.2"
source_github: "https://github.com/blackstork-io/fabric/tree/main/internal/builtin/"
resource:
type: publisher
type: docs
---

{{< breadcrumbs 2 >}}

{{< plugin-resource-header "blackstork/builtin" "builtin" "v0.4.2" "hub" "publisher" >}}

The publisher is built-in, which means it's a part of `fabric` binary. It's available out-of-the-box, no installation required.

#### Formats

The publisher supports the following document formats:

- `unknown`

To set the output format, specify it inside `publish` block with `format` argument.


#### Configuration

The publisher supports the following configuration arguments:

```hcl
config publish hub {
# API url.
#
# Required string.
# Must be non-empty
# For example:
api_url = "some string"
# API url.
#
# Required string.
# Must be non-empty
# For example:
api_token = "some string"
}
```

#### Usage

The publisher supports the following execution arguments:

```hcl
# In addition to the arguments listed, `publish` block accepts `format` argument.
publish hub {
# Hub Document title override. By default uses title configured in the document.
#
# Optional string.
# Must be non-empty
# Default value:
title = null
}
```

11 changes: 11 additions & 0 deletions docs/plugins/plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@
"url"
]
},
{
"name": "hub",
"type": "publisher",
"config_params": [
"api_token",
"api_url"
],
"arguments": [
"title"
]
},
{
"name": "image",
"type": "content-provider",
Expand Down
4 changes: 4 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func (e *Engine) FileMap() map[string]*hcl.File {
return e.fileMap
}

func (e *Engine) ParsedBlocks() *parser.DefinedBlocks {
return e.blocks
}

func (e *Engine) Install(ctx context.Context, upgrade bool) (diags diagnostics.Diag) {
ctx, span := e.tracer.Start(ctx, "Engine.Install", trace.WithAttributes(
attribute.Bool("upgrade", upgrade),
Expand Down
72 changes: 72 additions & 0 deletions examples/templates/hub/example.fabric
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
document "example" {

title = "Example Report, {{ now | date \"Jan 02\" }}"

publish hub {
config {
api_url = env.HUB_API_URL
api_token = env.HUB_API_TOKEN
}
}

content title {
value = "TOC"
}

content toc {}

content title {
value = "Subtitle 0"
}

section {
title = "Section #1"

content title {
value = "Section TOC"
}

content toc {
start_level = 1
end_level = 4
ordered = true
}

content text {
value = "Text value 1"
}

content title {
value = "Subtitle 1"
}

content text {
value = "Text value 2"
}

section {
title = "Subsection 1"

content text {
value = "Text value 3"
}
}

content title {
value = "Subtitle 2"
}
}

section {
title = "Section 2"

section {
title = "Subsection 2"

content text {
value = "Text value 4"
}
}
}
}

40 changes: 40 additions & 0 deletions internal/builtin/content_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package builtin

import (
"bytes"
"fmt"
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"

"github.com/blackstork-io/fabric/plugin"
"github.com/blackstork-io/fabric/plugin/plugindata"
)
Expand Down Expand Up @@ -78,3 +85,36 @@ func findDepth(parent *plugin.ContentSection, id uint32, depth int) int {
}
return -1
}

func firstTitle(el plugin.Content) (string, bool) {
switch el := el.(type) {
case *plugin.ContentSection:
for _, c := range el.Children {
if title, ok := firstTitle(c); ok {
return title, true
}
}
case *plugin.ContentElement:
meta := el.Meta()
if meta.Plugin == "blackstork/builtin" && meta.Provider == "title" {
return string(bytes.TrimSpace(
bytes.TrimPrefix(el.AsMarkdownSrc(), []byte("#")),
)), true
}
}
return "", false
}

func templateString(str string, datactx plugindata.Map) (string, error) {
tmpl, err := template.New("pattern").Funcs(sprig.FuncMap()).Parse(str)
if err != nil {
return "", fmt.Errorf("failed to parse a text template: %w", err)
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, datactx.Any())
if err != nil {
return "", fmt.Errorf("failed to execute a text template: %w", err)
}
return strings.TrimSpace(buf.String()), nil
}
Loading

0 comments on commit 379e2f8

Please sign in to comment.