@@ -2,7 +2,10 @@ package cd
22
33import (
44 "fmt"
5+ iofs "io/fs"
6+ "os"
57 "path/filepath"
8+ "sort"
69 "strings"
710
811 "github.com/pluralsh/plural-cli/pkg/common"
@@ -128,6 +131,10 @@ func (p *Plural) cdServiceCommands() []cli.Command {
128131 Name : "lua-file" ,
129132 Usage : "The .lua file you want to attempt to template." ,
130133 },
134+ cli.StringFlag {
135+ Name : "lua-dir" ,
136+ Usage : "A directory of lua library files to include in the final script used" ,
137+ },
131138 cli.StringFlag {
132139 Name : "context" ,
133140 Usage : "A yaml context file to imitate the internal service template context" ,
@@ -318,6 +325,7 @@ func (p *Plural) handleLuaTemplate(c *cli.Context) error {
318325 }
319326
320327 luaFile := c .String ("lua-file" )
328+ luaDir := c .String ("lua-dir" )
321329 context := c .String ("context" )
322330 dir := c .String ("dir" )
323331 if dir == "" {
@@ -333,6 +341,15 @@ func (p *Plural) handleLuaTemplate(c *cli.Context) error {
333341 return err
334342 }
335343
344+ if luaDir != "" {
345+ luaFiles , err := luaFolder (luaDir )
346+ if err != nil {
347+ return err
348+ }
349+
350+ luaStr = luaFiles + "\n \n " + luaStr
351+ }
352+
336353 ctx := map [string ]interface {}{}
337354 if context != "" {
338355 if err := utils .YamlFile (context , & ctx ); err != nil {
@@ -383,6 +400,45 @@ func (p *Plural) handleLuaTemplate(c *cli.Context) error {
383400 return nil
384401}
385402
403+ func luaFolder (folder string ) (string , error ) {
404+ luaFiles := make ([]string , 0 )
405+ if err := filepath .WalkDir (folder , func (path string , info iofs.DirEntry , err error ) error {
406+ if err != nil {
407+ return err
408+ }
409+ if info .IsDir () {
410+ return nil
411+ }
412+
413+ if strings .HasSuffix (info .Name (), ".lua" ) {
414+ luaPath , err := filepath .Rel (folder , path )
415+ if err != nil {
416+ return err
417+ }
418+ luaFiles = append (luaFiles , luaPath )
419+ }
420+
421+ return nil
422+ }); err != nil {
423+ return "" , fmt .Errorf ("failed to walk lua folder %s: %w" , folder , err )
424+ }
425+
426+ sort .Slice (luaFiles , func (i , j int ) bool {
427+ return luaFiles [i ] < luaFiles [j ]
428+ })
429+
430+ luaFileContents := make ([]string , 0 )
431+ for _ , file := range luaFiles {
432+ luaContents , err := os .ReadFile (file )
433+ if err != nil {
434+ return "" , fmt .Errorf ("failed to read lua file %s: %w" , file , err )
435+ }
436+ luaFileContents = append (luaFileContents , string (luaContents ))
437+ }
438+
439+ return strings .Join (luaFileContents , "\n \n " ), nil
440+ }
441+
386442func (p * Plural ) handleCloneClusterService (c * cli.Context ) error {
387443 if err := p .InitConsoleClient (consoleToken , consoleURL ); err != nil {
388444 return err
0 commit comments