Skip to content

Commit 5ae5e20

Browse files
committed
[minor] define jobs using sitectls' new jobs SDK
1 parent 97637ce commit 5ae5e20

7 files changed

Lines changed: 640 additions & 132 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ INSTALL_DIR ?= $(or $(dir $(shell which $(BINARY_NAME) 2>/dev/null)),/usr/local/
66
deps: work
77
go mod tidy
88

9-
build: deps
9+
build:
1010
go build -o $(BINARY_NAME) .
1111

1212
install: work build

cmd/backup.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

cmd/extensions.go

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"strings"
1313

1414
"charm.land/lipgloss/v2"
15-
"github.com/kballard/go-shellquote"
1615
"github.com/libops/sitectl/pkg/config"
1716
"github.com/libops/sitectl/pkg/docker"
1817
"github.com/libops/sitectl/pkg/plugin"
@@ -110,7 +109,7 @@ func init() {
110109
componentExtensionCmd.AddCommand(componentExtensionReconcileCmd)
111110
componentExtensionCmd.AddCommand(componentExtensionSetCmd)
112111

113-
debugExtensionCmd.Flags().StringVar(&drupalRootfsPath, "drupal-rootfs", "drupal/rootfs/var/www/drupal", "Drupal rootfs path override")
112+
debugExtensionCmd.Flags().StringVar(&drupalRootfsPath, "drupal-rootfs", "", "Drupal rootfs path override")
114113
}
115114

116115
func renderDrupalDebug(runCtx context.Context) (string, error) {
@@ -130,8 +129,12 @@ func renderDrupalDebug(runCtx context.Context) (string, error) {
130129
}
131130
defer files.Close()
132131

133-
slog.Debug("resolving drupal root", "plugin", "drupal", "rootfs", drupalRootfsPath)
134-
drupalRoot := resolveDrupalRoot(files, ctx.ProjectDir, drupalRootfsPath)
132+
rootfs := strings.TrimSpace(drupalRootfsPath)
133+
if rootfs == "" {
134+
rootfs = ctx.EffectiveDrupalRootfs()
135+
}
136+
slog.Debug("resolving drupal root", "plugin", "drupal", "rootfs", rootfs)
137+
drupalRoot := ctx.ResolveProjectPath(rootfs)
135138
slog.Debug("resolved drupal root", "plugin", "drupal", "drupal_root", drupalRoot)
136139
configDir := filepath.Join(drupalRoot, "config", "sync")
137140
body := []string{
@@ -182,14 +185,14 @@ func renderDrupalDebug(runCtx context.Context) (string, error) {
182185
}
183186

184187
func renderCachePageSummary(runCtx context.Context) (string, error) {
185-
_, cli, containerName, err := getDrupalContainerForSDK(runCtx)
188+
ctx, cli, containerName, err := getDrupalContainerForSDK(runCtx)
186189
if err != nil {
187190
return "", err
188191
}
189192
defer cli.Close()
190193

191194
query := "SELECT COALESCE(data_length + index_length, 0) FROM information_schema.TABLES WHERE table_schema = DATABASE() AND table_name = 'cache_page';"
192-
output, err := execDrupalCommandCapture(runCtx, cli, containerName, []string{"drush", "sql:query", query, "--extra=--batch", "--extra=--skip-column-names"})
195+
output, err := execDrupalCommandCapture(runCtx, cli, containerName, ctx.EffectiveDrupalContainerRoot(), []string{"drush", "sql:query", query, "--extra=--batch", "--extra=--skip-column-names"})
193196
if err != nil {
194197
return "", err
195198
}
@@ -234,17 +237,15 @@ func getDrupalContainerForSDK(runCtx context.Context) (ctx *config.Context, cli
234237
return ctx, cli, containerName, nil
235238
}
236239

237-
func execDrupalCommandCapture(runCtx context.Context, cli *docker.DockerClient, containerName string, cmd []string) (string, error) {
240+
func execDrupalCommandCapture(runCtx context.Context, cli *docker.DockerClient, containerName, containerRoot string, cmd []string) (string, error) {
238241
slog.Debug(strings.Join(cmd, " "), "plugin", "drupal", "container", containerName)
239242
var stdout bytes.Buffer
240243
var stderr bytes.Buffer
241244

242-
wrappedCmd := []string{"bash", "-lc", fmt.Sprintf("cd /var/www/drupal && %s", shellquote.Join(cmd...))}
243-
244245
exitCode, err := cli.Exec(runCtx, docker.ExecOptions{
245246
Container: containerName,
246-
Cmd: wrappedCmd,
247-
WorkingDir: "/var/www/drupal",
247+
Cmd: cmd,
248+
WorkingDir: containerRoot,
248249
AttachStdout: true,
249250
AttachStderr: true,
250251
Stdout: &stdout,
@@ -291,26 +292,6 @@ func humanBytes(size int64) string {
291292
return fmt.Sprintf("%.1f%ciB", float64(size)/float64(div), "KMGTPE"[exp])
292293
}
293294

294-
func resolveDrupalRoot(files *plugin.FileAccessor, projectDir, drupalRootPath string) string {
295-
candidates := []string{}
296-
if trimmed := strings.TrimSpace(drupalRootPath); trimmed != "" {
297-
if filepath.IsAbs(trimmed) {
298-
candidates = append(candidates, filepath.Clean(trimmed))
299-
} else {
300-
candidates = append(candidates, filepath.Join(projectDir, trimmed))
301-
}
302-
}
303-
if strings.TrimSpace(projectDir) != "" {
304-
candidates = append(candidates, projectDir)
305-
}
306-
for _, candidate := range candidates {
307-
if _, err := files.ReadFile(filepath.Join(candidate, "config", "sync", "core.extension.yml")); err == nil {
308-
return candidate
309-
}
310-
}
311-
return ""
312-
}
313-
314295
func readCoreExtension(runCtx context.Context, files *plugin.FileAccessor, path string) ([]string, []string, error) {
315296
data, err := files.ReadFileContext(runCtx, path)
316297
if err != nil {

cmd/extensions_test.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestReadCoreExtensionMissingFileReturnsNilSlices(t *testing.T) {
7070
}
7171
}
7272

73-
func TestResolveDrupalRootFindsConfiguredRootfs(t *testing.T) {
73+
func TestDrupalRootUsesConfiguredRootfs(t *testing.T) {
7474
projectDir := t.TempDir()
7575
drupalRoot := filepath.Join(projectDir, "drupal", "rootfs", "var", "www", "drupal")
7676
configDir := filepath.Join(drupalRoot, "config", "sync")
@@ -81,37 +81,18 @@ func TestResolveDrupalRootFindsConfiguredRootfs(t *testing.T) {
8181
t.Fatalf("WriteFile() error = %v", err)
8282
}
8383

84-
files, err := plugin.NewFileAccessor(&config.Context{DockerHostType: config.ContextLocal})
85-
if err != nil {
86-
t.Fatalf("NewFileAccessor() error = %v", err)
87-
}
88-
defer files.Close()
89-
90-
got := resolveDrupalRoot(files, projectDir, "drupal/rootfs/var/www/drupal")
84+
got := (&config.Context{ProjectDir: projectDir, DrupalRootfs: "drupal/rootfs/var/www/drupal"}).ResolveProjectPath((&config.Context{ProjectDir: projectDir, DrupalRootfs: "drupal/rootfs/var/www/drupal"}).EffectiveDrupalRootfs())
9185
if got != drupalRoot {
92-
t.Fatalf("resolveDrupalRoot() = %q, want %q", got, drupalRoot)
86+
t.Fatalf("drupal root = %q, want %q", got, drupalRoot)
9387
}
9488
}
9589

96-
func TestResolveDrupalRootFallsBackToProjectDir(t *testing.T) {
90+
func TestDrupalRootUsesProjectDirWhenConfigured(t *testing.T) {
9791
projectDir := t.TempDir()
98-
configDir := filepath.Join(projectDir, "config", "sync")
99-
if err := os.MkdirAll(configDir, 0o755); err != nil {
100-
t.Fatalf("MkdirAll() error = %v", err)
101-
}
102-
if err := os.WriteFile(filepath.Join(configDir, "core.extension.yml"), []byte("module: {}\ntheme: {}\n"), 0o644); err != nil {
103-
t.Fatalf("WriteFile() error = %v", err)
104-
}
105-
106-
files, err := plugin.NewFileAccessor(&config.Context{DockerHostType: config.ContextLocal})
107-
if err != nil {
108-
t.Fatalf("NewFileAccessor() error = %v", err)
109-
}
110-
defer files.Close()
111-
112-
got := resolveDrupalRoot(files, projectDir, "drupal/rootfs/var/www/drupal")
92+
ctx := &config.Context{ProjectDir: projectDir, DrupalRootfs: "."}
93+
got := ctx.ResolveProjectPath(ctx.EffectiveDrupalRootfs())
11394
if got != projectDir {
114-
t.Fatalf("resolveDrupalRoot() = %q, want %q", got, projectDir)
95+
t.Fatalf("drupal root = %q, want %q", got, projectDir)
11596
}
11697
}
11798

cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
pluginjobs "github.com/libops/sitectl-drupal/pkg/jobs"
45
"github.com/libops/sitectl/pkg/plugin"
56
)
67

@@ -11,16 +12,16 @@ var (
1112

1213
func init() {
1314
loginCmd.Flags().Uint("uid", 1, "Drupal user ID to provide a direct login link for")
14-
15-
backupCmd.Flags().StringVarP(drupalServiceName, "drupal-service", "d", "drupal", "The name of the drupal service in docker compose")
1615
}
1716

1817
// RegisterCommands registers all drupal commands with the plugin SDK
1918
func RegisterCommands(s *plugin.SDK) {
2019
sdk = s
21-
sdk.AddCommand(backupCmd)
20+
pluginjobs.Register(s)
21+
sdk.AddCommand(sdk.GetMetadataCommand())
2222
sdk.AddCommand(componentExtensionCmd)
2323
sdk.AddCommand(debugExtensionCmd)
2424
sdk.AddCommand(drushCmd)
2525
sdk.AddCommand(loginCmd)
26+
sdk.AddCommand(syncCmd)
2627
}

0 commit comments

Comments
 (0)