@@ -14,6 +14,7 @@ limitations under the License.
1414package standalone
1515
1616import (
17+ "fmt"
1718 "os"
1819 path_filepath "path/filepath"
1920 "runtime"
@@ -68,42 +69,72 @@ func DefaultConfigFilePath() string {
6869}
6970
7071// emptyAndCopyFiles copies files from src to dest. It deletes the existing files in dest before copying from src.
72+ // this method also deletes the components dir and makes it as a symlink to resources directory.
73+ // please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345
7174// TODO: Remove this function when `--components-path` flag is removed.
7275func emptyAndCopyFiles (src , dest string ) error {
7376 if _ , err := os .Stat (src ); err != nil {
74- // if the src directory does not exist, return nil, because there is nothing to copy from.
77+ // if the src directory does not exist, create symlink and return nil, because there is nothing to copy from.
7578 if os .IsNotExist (err ) {
79+ err = createSymLink (dest , src )
80+ if err != nil {
81+ return err
82+ }
7683 return nil
7784 }
78- return err
85+ return fmt . Errorf ( "error reading directory %s: %w" , src , err )
7986 }
8087 files , err := os .ReadDir (dest )
8188 if err != nil {
82- return err
89+ return fmt . Errorf ( "error reading files from %s: %w" , dest , err )
8390 }
8491 for _ , file := range files {
8592 err = os .Remove (path_filepath .Join (dest , file .Name ()))
8693 if err != nil {
87- return err
94+ return fmt . Errorf ( "error removing file %s: %w" , file . Name (), err )
8895 }
8996 }
9097 files , err = os .ReadDir (src )
9198 if err != nil {
92- return err
99+ return fmt . Errorf ( "error reading files from %s: %w" , src , err )
93100 }
94101 if len (files ) > 0 {
95102 print .InfoStatusEvent (os .Stdout , "Moving files from %q to %q" , src , dest )
103+ var content []byte
96104 for _ , file := range files {
97- content , err : = os .ReadFile (path_filepath .Join (src , file .Name ()))
105+ content , err = os .ReadFile (path_filepath .Join (src , file .Name ()))
98106 if err != nil {
99- return err
107+ return fmt . Errorf ( "error reading file %s: %w" , file . Name (), err )
100108 }
101109 // #nosec G306
102110 err = os .WriteFile (path_filepath .Join (dest , file .Name ()), content , 0o644 )
103111 if err != nil {
104- return err
112+ return fmt . Errorf ( "error writing file %s: %w" , file . Name (), err )
105113 }
106114 }
107115 }
116+ // delete the components dir and make it as a symlink to resources directory.
117+ err = os .RemoveAll (src )
118+ if err != nil {
119+ return fmt .Errorf ("error removing directory %s: %w" , src , err )
120+ }
121+ err = createSymLink (dest , src )
122+ if err != nil {
123+ return err
124+ }
125+ return nil
126+ }
127+
128+ func createSymLink (dirName , symLinkName string ) error {
129+ if _ , err := os .Stat (dirName ); err != nil {
130+ if os .IsNotExist (err ) {
131+ return fmt .Errorf ("directory %s does not exist" , dirName )
132+ }
133+ return fmt .Errorf ("error reading directory %s: %w" , dirName , err )
134+ }
135+ err := os .Symlink (dirName , symLinkName )
136+ if err != nil {
137+ return fmt .Errorf ("error creating symlink from %s to %s: %w" , dirName , symLinkName , err )
138+ }
108139 return nil
109140}
0 commit comments