Skip to content

Commit 3989aad

Browse files
committed
make symlink
Signed-off-by: Pravin Pushkar <[email protected]>
1 parent 5a624af commit 3989aad

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

pkg/standalone/common.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
package standalone
1515

1616
import (
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.
7275
func 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
}

pkg/standalone/standalone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod
308308
}
309309
print.InfoStatusEvent(os.Stdout, "Use `%s ps` to check running containers.", runtimeCmd)
310310
}
311-
// TODO: remove below method when components-path flag is removed.
311+
// TODO: remove below method when usages of components-path flag and components directory removed completely.
312312
err = emptyAndCopyFiles(DefaultComponentsDirPath(), DefaultResourcesDirPath())
313313
if err != nil {
314314
return err

0 commit comments

Comments
 (0)