@@ -39,7 +39,7 @@ func NewScriptRunner(scriptPath, sqlPath string) (*ScriptRunner, error) {
3939 // Validate SQL path if provided
4040 if sqlPath != "" {
4141 if _ , err := os .Stat (sqlPath ); err != nil {
42- return nil , fmt .Errorf ("SQL file not found: %w" , err )
42+ return nil , fmt .Errorf ("SQL file %q not found: %w" , sqlPath , err )
4343 }
4444 }
4545
@@ -81,57 +81,47 @@ func (r *ScriptRunner) Run(ctx context.Context) error {
8181 r .logger .Info ("Working directory" , zap .String ("path" , tempDir ))
8282
8383 // Copy static files to temp directory
84- if err := r . copyStaticFiles (tempDir ); err != nil {
84+ if err := static . CopyAllStaticFilesToPath (tempDir , common . FileMode ); err != nil {
8585 return fmt .Errorf ("failed to copy static files: %w" , err )
8686 }
8787
8888 // Copy user's script to temp directory
8989 scriptName := filepath .Base (r .scriptPath )
90- if err := r .copyFile (r .scriptPath , path .Join (tempDir , scriptName )); err != nil {
91- return fmt .Errorf ("failed to copy script: %w" , err )
92- }
90+ sqlName := filepath .Base (r .sqlPath )
9391
94- // Copy SQL file if explicitly provided
95- if r .sqlPath != "" {
96- sqlName := filepath .Base (r .sqlPath )
97- if err := r .copyFile (r .sqlPath , path .Join (tempDir , sqlName )); err != nil {
98- return fmt .Errorf ("failed to copy SQL file: %w" , err )
92+ if r .sqlPath == "" { // copy single ts file
93+ if err := copyFile (r .scriptPath , path .Join (tempDir , scriptName )); err != nil {
94+ return fmt .Errorf ("failed to copy script: %w" , err )
95+ }
96+ } else { // copy ts + sql + add name in variable
97+ if err := copyFileWithPrepend (
98+ r .scriptPath ,
99+ path .Join (tempDir , scriptName ),
100+ fmt .Sprintf (`const __SQL_FILE = %q;` , sqlName ),
101+ ); err != nil {
102+ return fmt .Errorf ("failed to copy script: %w" , err )
99103 }
100- }
101104
102- // Copy all SQL files from script's directory (for scripts that use open())
103- if err := r . copySQLFilesFromScriptDir ( tempDir ); err != nil {
104- r . logger . Warn ( "Could not copy SQL files from script directory" , zap . Error ( err ))
105+ if err := copyFile ( r . sqlPath , path . Join ( tempDir , sqlName )); err != nil {
106+ return fmt . Errorf ( "failed to copy SQL file %q: %w" , sqlName , err )
107+ }
105108 }
106109
107- // Build k6 arguments
108110 args := []string {"run" , scriptName }
109111
110- // Build environment variables
111112 envs := r .buildEnvVars ()
112113
113- // Add OTLP exporter args if configured
114114 if r .config .GlobalConfig .GetExporter ().GetOtlpExport () != nil {
115115 args , envs = r .addOtelExportArgs (args , envs )
116116 }
117117
118118 r .logger .Debug ("Running k6" , zap .Strings ("args" , args ))
119119
120- // Run k6
121120 return r .runK6Binary (ctx , tempDir , args , envs )
122121}
123122
124- // copyStaticFiles copies required static files to the temp directory.
125- func (r * ScriptRunner ) copyStaticFiles (tempDir string ) error {
126- return static .CopyStaticFilesToPath (
127- tempDir ,
128- common .FileMode ,
129- static .StaticFiles ... ,
130- )
131- }
132-
133123// copyFile copies a file from src to dst.
134- func ( r * ScriptRunner ) copyFile (src , dst string ) error {
124+ func copyFile (src , dst string ) error {
135125 data , err := os .ReadFile (src )
136126 if err != nil {
137127 return err
@@ -140,31 +130,13 @@ func (r *ScriptRunner) copyFile(src, dst string) error {
140130 return os .WriteFile (dst , data , common .FileMode )
141131}
142132
143- // copySQLFilesFromScriptDir copies all SQL files from the script's directory.
144- func (r * ScriptRunner ) copySQLFilesFromScriptDir (tempDir string ) error {
145- scriptDir := filepath .Dir (r .scriptPath )
146-
147- entries , err := os .ReadDir (scriptDir )
133+ func copyFileWithPrepend (src , dst , prepend string ) error {
134+ data , err := os .ReadFile (src )
148135 if err != nil {
149136 return err
150137 }
151138
152- for _ , entry := range entries {
153- if entry .IsDir () {
154- continue
155- }
156-
157- if filepath .Ext (entry .Name ()) == ".sql" {
158- srcPath := filepath .Join (scriptDir , entry .Name ())
159-
160- dstPath := path .Join (tempDir , entry .Name ())
161- if err := r .copyFile (srcPath , dstPath ); err != nil {
162- return err
163- }
164- }
165- }
166-
167- return nil
139+ return os .WriteFile (dst , append ([]byte (prepend ), data ... ), common .FileMode )
168140}
169141
170142// buildEnvVars builds environment variables for k6 execution.
@@ -226,7 +198,6 @@ func (r *ScriptRunner) addOtelExportArgs(args, envs []string) (argsOut, envsOut
226198}
227199
228200// runK6Binary executes the k6 binary.
229- // TODO: pass sql file name
230201func (r * ScriptRunner ) runK6Binary (ctx context.Context , workdir string , args , envs []string ) error {
231202 binaryPath := path .Join (workdir , static .K6PluginFileName .String ())
232203
0 commit comments