@@ -516,57 +516,68 @@ func downloadConfigFiles(ctx context.Context, client stainless.Client, config Wo
516516 group .Property ("- " , key )
517517 }
518518
519- {
520- var content string
521- if try , ok := (* configRes )["stainless.yml" ]; ok {
522- content = try .Content
523- } else if try , ok := (* configRes )["openapi.stainless.yml" ]; ok {
524- content = try .Content
525- } else {
526- content = "" // Write an empty file
527- }
528-
519+ // Helper function to write a file with confirmation if it exists
520+ writeFileWithConfirm := func (path string , content []byte , description string ) error {
529521 // Create parent directories if they don't exist
530- dir := filepath .Dir (config . StainlessConfig )
522+ dir := filepath .Dir (path )
531523 if err := os .MkdirAll (dir , 0755 ); err != nil {
532524 return fmt .Errorf ("failed to create directory for config file: %w" , err )
533525 }
534526
535- err = os .WriteFile (config .StainlessConfig , []byte (content ), 0644 )
527+ // If the file exists and is nonempty, ask the user before writing over it
528+ if fileInfo , err := os .Stat (path ); err == nil && fileInfo .Size () > 0 {
529+ shouldOverwrite , err := Confirm (nil , "" , fmt .Sprintf ("File %s already exists" , path ), "Do you want to overwrite it?" , true )
530+ if err != nil {
531+ return fmt .Errorf ("failed to confirm file overwrite: %w" , err )
532+ }
533+ if ! shouldOverwrite {
534+ group .Property ("Note" , fmt .Sprintf ("Keeping existing file at %s" , path ))
535+ return nil
536+ }
537+ }
538+
539+ err = os .WriteFile (path , content , 0644 )
536540 if err != nil {
537- group .Error ("Failed to save project config to %s: %v" , config .StainlessConfig , err )
538- return fmt .Errorf ("Stainless configuration could not write to file: %v" , err )
541+ group .Error ("Failed to save project config to %s: %v" , path , err )
542+ return fmt .Errorf ("%s could not write to file: %v" , description , err )
543+ }
544+ group .Success ("%s downloaded to %s" , description , path )
545+ return nil
546+ }
547+
548+ // Handle Stainless config file
549+ {
550+ var stainlessConfig string
551+ if try , ok := (* configRes )["stainless.yml" ]; ok {
552+ stainlessConfig = try .Content
553+ } else if try , ok := (* configRes )["openapi.stainless.yml" ]; ok {
554+ stainlessConfig = try .Content
555+ } else {
556+ stainlessConfig = "" // Write an empty file
539557 }
540558
541- group .Success ("Stainless configuration downloaded to %s" , config .StainlessConfig )
559+ if err := writeFileWithConfirm (config .StainlessConfig , []byte (stainlessConfig ), "Stainless configuration" ); err != nil {
560+ return err
561+ }
542562 }
543563
564+ // Handle OpenAPI spec file
544565 {
545- var specContent [] byte
566+ var openAPISpec string
546567 if try , ok := (* configRes )["openapi.json" ]; ok {
547- specContent = [] byte ( try .Content )
568+ openAPISpec = try .Content
548569 } else if try , ok := (* configRes )["openapi.yml" ]; ok {
549- specContent = [] byte ( try .Content )
570+ openAPISpec = try .Content
550571 } else if try , ok := (* configRes )["openapi.yaml" ]; ok {
551- specContent = [] byte ( try .Content )
572+ openAPISpec = try .Content
552573 } else {
553- return fmt . Errorf ( "could not find OpenAPI specification in response" )
574+ openAPISpec = ""
554575 }
555- fmt .Printf ("OAS:\n %s\n \n " , string (specContent ))
556576
557- // Create parent directories if they don't exist
558- dir := filepath .Dir (config .OpenAPISpec )
559- if err := os .MkdirAll (dir , 0755 ); err != nil {
560- return fmt .Errorf ("failed to create directory for config file: %w" , err )
561- }
562-
563- err = os .WriteFile (config .OpenAPISpec , specContent , 0644 )
564- if err != nil {
565- group .Error ("Failed to save project config to %s: %v" , config .OpenAPISpec , err )
566- return fmt .Errorf ("OpenAPI specification could not write to file: %v" , err )
577+ // TODO: we should warn or confirm if the downloaded file has a different file extension than the destination filename
578+ if err := writeFileWithConfirm (config .OpenAPISpec , []byte (openAPISpec ), "OpenAPI specification" ); err != nil {
579+ return err
567580 }
568-
569- group .Success ("OpenAPI specification downloaded to %s" , config .OpenAPISpec )
570581 }
571582
572583 return nil
0 commit comments