Add a command to automatically create a block
This commit is contained in:
parent
7b28fc5661
commit
290273e4ca
5 changed files with 161 additions and 0 deletions
82
cmd/make_block.go
Normal file
82
cmd/make_block.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/cli"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php/extras"
|
||||
"github.com/tkw1536/goprogram/exit"
|
||||
)
|
||||
|
||||
// MakeBlock is the 'make_block' command
|
||||
var MakeBlock wisski_distillery.Command = makeBlock{}
|
||||
|
||||
type makeBlock struct {
|
||||
Title string `short:"t" long:"title" description:"title of block to create"`
|
||||
Region string `short:"r" long:"region" description:"optional region to assign block to"`
|
||||
|
||||
Positionals struct {
|
||||
Slug string `positional-arg-name:"SLUG" required:"1-1" description:"slug of instance to create legal block for"`
|
||||
} `positional-args:"true"`
|
||||
}
|
||||
|
||||
func (makeBlock) Description() wisski_distillery.Description {
|
||||
return wisski_distillery.Description{
|
||||
Requirements: cli.Requirements{
|
||||
NeedsDistillery: true,
|
||||
},
|
||||
Command: "make_block",
|
||||
Description: "Creates a block with html content provided on stdin",
|
||||
}
|
||||
}
|
||||
|
||||
var errBlocksGeneric = exit.Error{
|
||||
Message: "unable to create block",
|
||||
ExitCode: exit.ExitGeneric,
|
||||
}
|
||||
|
||||
var errBlocksNoContent = exit.Error{
|
||||
Message: "unable to read content from standard input",
|
||||
ExitCode: exit.ExitCommandArguments,
|
||||
}
|
||||
|
||||
func (mb makeBlock) Run(context wisski_distillery.Context) error {
|
||||
|
||||
// get the wisski
|
||||
instance, err := context.Environment.Instances().WissKI(context.Context, mb.Positionals.Slug)
|
||||
if err != nil {
|
||||
return errPathbuilderWissKI.Wrap(err)
|
||||
}
|
||||
|
||||
// read the content
|
||||
content, err := io.ReadAll(context.Stdin)
|
||||
if err != nil {
|
||||
return errBlocksNoContent.Wrap(err)
|
||||
}
|
||||
|
||||
id := ""
|
||||
if mb.Region != "" {
|
||||
id = fmt.Sprintf("block-auto-%d", time.Now().Unix())
|
||||
}
|
||||
|
||||
{
|
||||
err := instance.Blocks().Create(context.Context, nil, extras.Block{
|
||||
Info: mb.Title,
|
||||
Content: template.HTML(content),
|
||||
|
||||
Region: mb.Region,
|
||||
BlockID: id,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
context.EPrintln(err.Error())
|
||||
return errBlocksGeneric
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -68,6 +68,8 @@ func init() {
|
|||
|
||||
// status
|
||||
wdcli.Register(cmd.Status)
|
||||
|
||||
wdcli.Register(cmd.MakeBlock)
|
||||
}
|
||||
|
||||
// an error when no arguments are provided.
|
||||
|
|
|
|||
36
internal/wisski/ingredient/php/extras/blocks.go
Normal file
36
internal/wisski/ingredient/php/extras/blocks.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package extras
|
||||
|
||||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
|
||||
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
type Blocks struct {
|
||||
ingredient.Base
|
||||
Dependencies struct {
|
||||
PHP *php.PHP
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed blocks.php
|
||||
var blocksPHP string
|
||||
|
||||
type Block struct {
|
||||
Info string
|
||||
Content template.HTML
|
||||
|
||||
Region string
|
||||
BlockID string
|
||||
}
|
||||
|
||||
// Create creates a new block with the given title and html content
|
||||
func (blocks *Blocks) Create(ctx context.Context, server *phpx.Server, block Block) (err error) {
|
||||
err = blocks.Dependencies.PHP.ExecScript(ctx, server, nil, blocksPHP, "create_basic_block", block.Info, block.Content, block.Region, block.BlockID)
|
||||
return err
|
||||
}
|
||||
36
internal/wisski/ingredient/php/extras/blocks.php
Normal file
36
internal/wisski/ingredient/php/extras/blocks.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Creates a basic block and optionally places it into the right region
|
||||
*/
|
||||
function create_basic_block(string $info, string $html, string $region, string $block_id) {
|
||||
|
||||
// create a custom block
|
||||
$block_content = \Drupal\block_content\Entity\BlockContent::create([
|
||||
'info' => $info,
|
||||
'type' => 'basic',
|
||||
'body' => [
|
||||
'value' => $html,
|
||||
'format' => 'full_html',
|
||||
],
|
||||
]);
|
||||
$block_content->save();
|
||||
|
||||
if ($region === "") {
|
||||
return;
|
||||
}
|
||||
|
||||
// get plugin and theme id
|
||||
$plugin = 'block_content:' . $block_content->uuid();
|
||||
$theme = \Drupal::theme()->getActiveTheme()->getName();
|
||||
|
||||
$block = \Drupal\block\Entity\Block::create([
|
||||
'plugin' => $plugin,
|
||||
'id' => $block_id,
|
||||
'region' => $region,
|
||||
'status' => TRUE,
|
||||
'theme' => $theme,
|
||||
'weight' => 0,
|
||||
]);
|
||||
$block->save();
|
||||
}
|
||||
|
|
@ -86,6 +86,10 @@ func (wisski *WissKI) SSH() *ssh.SSH {
|
|||
return export[*ssh.SSH](wisski)
|
||||
}
|
||||
|
||||
func (wisski *WissKI) Blocks() *extras.Blocks {
|
||||
return export[*extras.Blocks](wisski)
|
||||
}
|
||||
|
||||
//
|
||||
// All components
|
||||
// THESE SHOULD NEVER BE CALLED DIRECTLY
|
||||
|
|
@ -105,6 +109,7 @@ func (wisski *WissKI) allIngredients() []initFunc {
|
|||
auto[*extras.Settings],
|
||||
auto[*extras.Pathbuilder],
|
||||
auto[*extras.Stats],
|
||||
auto[*extras.Blocks],
|
||||
auto[*users.Users],
|
||||
auto[*users.UserPolicy],
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue