@@ -20,7 +20,6 @@ import (
2020 "fmt"
2121 "os"
2222 "path/filepath"
23- "slices"
2423 "sort"
2524 "strings"
2625 "text/template"
@@ -86,7 +85,7 @@ func GenerateDockerArtifacts(dir string, projectType ProjectType, settingsMap ma
8685
8786 // TODO: (@rektdeckard) support Node entrypoint validation
8887 if projectType .IsPython () {
89- dockerfileContent , err = validateEntrypoint (dir , dockerfileContent , dockerIgnoreContent , projectType , settingsMap )
88+ dockerfileContent , err = validateEntrypoint (dir , dockerfileContent , dockerIgnoreContent , projectType )
9089 if err != nil {
9190 return nil , nil , err
9291 }
@@ -95,82 +94,73 @@ func GenerateDockerArtifacts(dir string, projectType ProjectType, settingsMap ma
9594 return dockerfileContent , dockerIgnoreContent , nil
9695}
9796
98- func validateEntrypoint (dir string , dockerfileContent []byte , dockerignoreContent []byte , projectType ProjectType , settingsMap map [string ]string ) ([]byte , error ) {
99- valFile := func (fileName string ) (string , error ) {
100- // NOTE: we need to recurse to find entrypoints which may exist in src/ or some other directory.
101- // This could be a lot of files, so we omit any files in .dockerignore, since they cannot be
102- // used as entrypoints.
97+ func validateEntrypoint (dir string , dockerfileContent []byte , dockerignoreContent []byte , projectType ProjectType ) ([]byte , error ) {
98+ // Build matcher from the Dockerignore content so we don't consider ignored files
99+ reader := bytes .NewReader (dockerignoreContent )
100+ patterns , err := ignorefile .ReadAll (reader )
101+ if err != nil {
102+ return nil , err
103+ }
104+ matcher , err := patternmatcher .New (patterns )
105+ if err != nil {
106+ return nil , err
107+ }
103108
104- reader := bytes . NewReader ( dockerignoreContent )
105- patterns , err := ignorefile . ReadAll ( reader )
109+ var fileList [] string
110+ if err := filepath . WalkDir ( dir , func ( path string , d os. DirEntry , err error ) error {
106111 if err != nil {
107- return "" , err
112+ return err
108113 }
109- matcher , err := patternmatcher .New (patterns )
110- if err != nil {
111- return "" , err
114+ if ignored , err := matcher .MatchesOrParentMatches (path ); ignored {
115+ return nil
116+ } else if err != nil {
117+ return err
112118 }
113-
114- var fileList []string
115- if err := filepath .WalkDir (dir , func (path string , d os.DirEntry , err error ) error {
116- if err != nil {
117- return err
118- }
119- if ignored , err := matcher .MatchesOrParentMatches (path ); ignored {
119+ if ! d .IsDir () && strings .HasSuffix (d .Name (), projectType .FileExt ()) {
120+ // Exclude double-underscore files (e.g., __init__.py) which cannot be entrypoint
121+ // except for __main__.py, which is the default entrypoint for Python.
122+ if strings .HasPrefix (d .Name (), "__" ) && d .Name () != "__main__.py" {
120123 return nil
121- } else if err != nil {
122- return err
123124 }
124- if ! d .IsDir () && strings .HasSuffix (d .Name (), projectType .FileExt ()) {
125- // Exclude double-underscore files (e.g., __init__.py) which cannot be entrypoint
126- // except for __main__.py, which is the default entrypoint for Python.
127- if strings .HasPrefix (d .Name (), "__" ) && d .Name () != "__main__.py" {
128- return nil
129- }
130- fileList = append (fileList , path )
131- }
132- return nil
133- }); err != nil {
134- return "" , fmt .Errorf ("error walking directory %s: %w" , dir , err )
135- }
136- if slices .Contains (fileList , fileName ) {
137- return fileName , nil
138- }
139-
140- // If no matching files found, return early
141- if len (fileList ) == 0 {
142- return "" , nil
125+ fileList = append (fileList , path )
143126 }
127+ return nil
128+ }); err != nil {
129+ return nil , fmt .Errorf ("error walking directory %s: %w" , dir , err )
130+ }
144131
145- // Prioritize common entrypoint filenames at the top of the list
146- if len (fileList ) > 1 {
147- priority := func (p string ) int {
148- name := filepath .Base (p )
149- switch name {
150- case "main.py" :
151- return 0
152- case "agent.py" :
153- return 1
154- default :
155- return 2
156- }
132+ // Prioritize common entrypoint filenames at the top of the list
133+ if len (fileList ) > 1 {
134+ priority := func (p string ) int {
135+ name := filepath .Base (p )
136+ switch name {
137+ case "__main__.py" :
138+ return 0
139+ case "main.py" :
140+ return 1
141+ case "agent.py" :
142+ return 2
143+ default :
144+ return 3
157145 }
158- sort .SliceStable (fileList , func (i , j int ) bool {
159- pi := priority (fileList [i ])
160- pj := priority (fileList [j ])
161- if pi != pj {
162- return pi < pj
163- }
164- return fileList [i ] < fileList [j ]
165- })
166- }
167-
168- // If there's only one candidate, select it automatically
169- if len (fileList ) == 1 {
170- return fileList [0 ], nil
171146 }
147+ sort .SliceStable (fileList , func (i , j int ) bool {
148+ pi := priority (fileList [i ])
149+ pj := priority (fileList [j ])
150+ if pi != pj {
151+ return pi < pj
152+ }
153+ return fileList [i ] < fileList [j ]
154+ })
155+ }
172156
173- var selected string
157+ var newEntrypoint string
158+ if len (fileList ) == 0 {
159+ newEntrypoint = "main.py"
160+ } else if len (fileList ) == 1 {
161+ newEntrypoint = fileList [0 ]
162+ } else {
163+ selected := fileList [0 ]
174164 form := huh .NewForm (
175165 huh .NewGroup (
176166 huh .NewSelect [string ]().
@@ -180,26 +170,12 @@ func validateEntrypoint(dir string, dockerfileContent []byte, dockerignoreConten
180170 WithTheme (util .Theme ),
181171 ),
182172 )
183-
184173 if err := form .Run (); err != nil {
185- return "" , err
174+ return nil , err
186175 }
187- return selected , nil
188- }
189-
190- if err := validateSettingsMap (settingsMap , []string {"python_entrypoint" }); err != nil {
191- return nil , err
176+ newEntrypoint = selected
192177 }
193178
194- pythonEntrypoint := settingsMap ["python_entrypoint" ]
195- newEntrypoint , err := valFile (pythonEntrypoint )
196- if err != nil {
197- return nil , err
198- }
199-
200- if newEntrypoint == "" {
201- newEntrypoint = pythonEntrypoint
202- }
203179 fmt .Printf ("Using entrypoint file [%s]\n " , util .Accented (newEntrypoint ))
204180
205181 tpl := template .Must (template .New ("Dockerfile" ).Parse (string (dockerfileContent )))
0 commit comments