@@ -4,13 +4,15 @@ import (
44 "bytes"
55 "context"
66 "fmt"
7+ "log/slog"
78 "os"
89 "path/filepath"
910 "sort"
1011 "strconv"
1112 "strings"
1213
1314 "charm.land/lipgloss/v2"
15+ "github.com/kballard/go-shellquote"
1416 "github.com/libops/sitectl/pkg/config"
1517 "github.com/libops/sitectl/pkg/docker"
1618 "github.com/libops/sitectl/pkg/plugin"
@@ -91,7 +93,7 @@ var debugExtensionCmd = &cobra.Command{
9193 Short : "Internal debug extension command" ,
9294 Hidden : true ,
9395 RunE : func (cmd * cobra.Command , args []string ) error {
94- rendered , err := renderDrupalDebug ()
96+ rendered , err := renderDrupalDebug (cmd . Context () )
9597 if err != nil {
9698 return err
9799 }
@@ -111,21 +113,26 @@ func init() {
111113 debugExtensionCmd .Flags ().StringVar (& drupalRootfsPath , "drupal-rootfs" , "drupal/rootfs/var/www/drupal" , "Drupal rootfs path override" )
112114}
113115
114- func renderDrupalDebug () (string , error ) {
116+ func renderDrupalDebug (runCtx context.Context ) (string , error ) {
117+ slog .Debug ("starting plugin debug" , "plugin" , "drupal" )
115118 if sdk == nil {
116119 return "" , fmt .Errorf ("plugin sdk is not initialized" )
117120 }
118121 ctx , err := sdk .GetContext ()
119122 if err != nil {
120123 return "" , err
121124 }
122- files , err := plugin .NewFileAccessor (ctx )
125+ slog .Debug ("resolved plugin context" , "plugin" , "drupal" , "context" , ctx .Name , "project_dir" , ctx .ProjectDir )
126+ slog .Debug ("creating file accessor" , "plugin" , "drupal" )
127+ files , err := sdk .GetFileAccessor ()
123128 if err != nil {
124129 return "" , err
125130 }
126131 defer files .Close ()
127132
133+ slog .Debug ("resolving drupal root" , "plugin" , "drupal" , "rootfs" , drupalRootfsPath )
128134 drupalRoot := resolveDrupalRoot (files , ctx .ProjectDir , drupalRootfsPath )
135+ slog .Debug ("resolved drupal root" , "plugin" , "drupal" , "drupal_root" , drupalRoot )
129136 configDir := filepath .Join (drupalRoot , "config" , "sync" )
130137 body := []string {
131138 debugDivider (),
@@ -141,15 +148,19 @@ func renderDrupalDebug() (string, error) {
141148 }
142149
143150 if strings .TrimSpace (drupalRoot ) == "" {
151+ slog .Debug ("drupal root unavailable; skipping extension scan" , "plugin" , "drupal" )
144152 body = append (body , "" , "Installed modules: unavailable" )
145153 return renderDebugPanel ("drupal" , strings .Join (body , "\n " )), nil
146154 }
147155
148- modules , themes , err := readCoreExtension (files , filepath .Join (configDir , "core.extension.yml" ))
156+ slog .Debug ("reading core.extension.yml" , "plugin" , "drupal" , "path" , filepath .Join (configDir , "core.extension.yml" ))
157+ modules , themes , err := readCoreExtension (runCtx , files , filepath .Join (configDir , "core.extension.yml" ))
149158 if err != nil {
150159 return "" , err
151160 }
152- cachePageSummary , err := renderCachePageSummary ()
161+ slog .Debug ("read installed extensions" , "plugin" , "drupal" , "modules" , len (modules ), "themes" , len (themes ))
162+ slog .Debug ("rendering cache_page summary" , "plugin" , "drupal" )
163+ cachePageSummary , err := renderCachePageSummary (runCtx )
153164 if err != nil {
154165 body = append (body , "" , debugDivider (), "" , debugTitleStyle .Render ("Cache Page" ), "" , formatDebugRows ([]debugRow {
155166 {Label : "Status" , Value : renderStatus ("warning" )},
@@ -166,18 +177,19 @@ func renderDrupalDebug() (string, error) {
166177 configLines = append (configLines , formatListLines (themes , 3 )... )
167178 body = append (body , "" , strings .Join (configLines , "\n " ))
168179
180+ slog .Debug ("finished plugin debug" , "plugin" , "drupal" )
169181 return renderDebugPanel ("drupal" , strings .Join (body , "\n " )), nil
170182}
171183
172- func renderCachePageSummary () (string , error ) {
173- _ , cli , containerName , err := getDrupalContainerForSDK ()
184+ func renderCachePageSummary (runCtx context. Context ) (string , error ) {
185+ _ , cli , containerName , err := getDrupalContainerForSDK (runCtx )
174186 if err != nil {
175187 return "" , err
176188 }
177189 defer cli .Close ()
178190
179191 query := "SELECT COALESCE(data_length + index_length, 0) FROM information_schema.TABLES WHERE table_schema = DATABASE() AND table_name = 'cache_page';"
180- output , err := execDrupalCommandCapture (cli , containerName , []string {"drush" , "sql:query" , query , "--extra=--batch --skip-column-names" })
192+ output , err := execDrupalCommandCapture (runCtx , cli , containerName , []string {"drush" , "sql:query" , query , "--extra=--batch" , "--extra= --skip-column-names" })
181193 if err != nil {
182194 return "" , err
183195 }
@@ -198,7 +210,7 @@ func renderCachePageSummary() (string, error) {
198210 return formatDebugRows (rows ), nil
199211}
200212
201- func getDrupalContainerForSDK () (ctx * config.Context , cli * docker.DockerClient , containerName string , err error ) {
213+ func getDrupalContainerForSDK (runCtx context. Context ) (ctx * config.Context , cli * docker.DockerClient , containerName string , err error ) {
202214 if sdk == nil {
203215 return nil , nil , "" , fmt .Errorf ("plugin sdk is not initialized" )
204216 }
@@ -213,7 +225,7 @@ func getDrupalContainerForSDK() (ctx *config.Context, cli *docker.DockerClient,
213225 return nil , nil , "" , err
214226 }
215227
216- containerName , err = cli .GetContainerName ( ctx , * drupalServiceName )
228+ containerName , err = cli .GetContainerNameContext ( runCtx , ctx , * drupalServiceName )
217229 if err != nil {
218230 cli .Close ()
219231 return nil , nil , "" , err
@@ -222,13 +234,17 @@ func getDrupalContainerForSDK() (ctx *config.Context, cli *docker.DockerClient,
222234 return ctx , cli , containerName , nil
223235}
224236
225- func execDrupalCommandCapture (cli * docker.DockerClient , containerName string , cmd []string ) (string , error ) {
237+ func execDrupalCommandCapture (runCtx context.Context , cli * docker.DockerClient , containerName string , cmd []string ) (string , error ) {
238+ slog .Debug (strings .Join (cmd , " " ), "plugin" , "drupal" , "container" , containerName )
226239 var stdout bytes.Buffer
227240 var stderr bytes.Buffer
228241
229- exitCode , err := cli .Exec (context .Background (), docker.ExecOptions {
242+ wrappedCmd := []string {"bash" , "-lc" , fmt .Sprintf ("cd /var/www/drupal && %s" , shellquote .Join (cmd ... ))}
243+
244+ exitCode , err := cli .Exec (runCtx , docker.ExecOptions {
230245 Container : containerName ,
231- Cmd : cmd ,
246+ Cmd : wrappedCmd ,
247+ WorkingDir : "/var/www/drupal" ,
232248 AttachStdout : true ,
233249 AttachStderr : true ,
234250 Stdout : & stdout ,
@@ -295,8 +311,8 @@ func resolveDrupalRoot(files *plugin.FileAccessor, projectDir, drupalRootPath st
295311 return ""
296312}
297313
298- func readCoreExtension (files * plugin.FileAccessor , path string ) ([]string , []string , error ) {
299- data , err := files .ReadFile ( path )
314+ func readCoreExtension (runCtx context. Context , files * plugin.FileAccessor , path string ) ([]string , []string , error ) {
315+ data , err := files .ReadFileContext ( runCtx , path )
300316 if err != nil {
301317 if os .IsNotExist (err ) {
302318 return nil , nil , nil
0 commit comments