Compare commits

...

8 Commits

Author SHA1 Message Date
Will Wang 68867d8fdb
Merge 01182919b3 into a4619f3676 2024-09-17 11:17:56 +01:00
Will Wang 01182919b3
update comments
Signed-off-by: Will Wang <willww64@gmail.com>
2024-07-24 15:15:59 +08:00
Will Wang a85a37e3f3
better to limit the scope of err
Signed-off-by: Will Wang <willww64@gmail.com>
2024-07-24 14:36:34 +08:00
Will Wang 40df26561b
use os.PathError to avoid an extra import
Signed-off-by: Will Wang <willww64@gmail.com>
2024-07-24 13:26:44 +08:00
Will Wang efaf3c92e6
extract path from error when NotExist error occurs
Signed-off-by: Will Wang <willww64@gmail.com>
2024-07-24 13:22:06 +08:00
Will Wang c6d1de3397
allow for dangling symlinks
Signed-off-by: Will Wang <willww64@gmail.com>
2024-07-24 12:10:32 +08:00
Will Wang 115fdd979b
Update cli/config/configfile/file.go
use filepath.EvalSymlink instead of check with filepath.IsAbs

Co-authored-by: Paweł Gronowski <me@woland.xyz>
Signed-off-by: Will Wang <willww64@gmail.com>
2024-07-23 23:17:09 +08:00
Will Wang c90705a413
fix bug with config file being a relative symlink
Signed-off-by: Will Wang <willww64@gmail.com>
2024-07-23 22:16:41 +08:00
2 changed files with 33 additions and 2 deletions

View File

@ -167,10 +167,13 @@ func (configFile *ConfigFile) Save() (retErr error) {
return errors.Wrap(err, "error closing temp file")
}
// Handle situation where the configfile is a symlink
// Handle situation where the configfile is a symlink, and allow for dangling symlinks
cfgFile := configFile.Filename
if f, err := os.Readlink(cfgFile); err == nil {
if f, err := filepath.EvalSymlinks(cfgFile); err == nil {
cfgFile = f
} else if os.IsNotExist(err) {
// extract the path from the error if the configfile does not exist or is a dangling symlink
cfgFile = err.(*os.PathError).Path
}
// Try copying the current config file (if any) ownership and permissions

View File

@ -538,6 +538,34 @@ func TestSaveWithSymlink(t *testing.T) {
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
}
func TestSaveWithRelativeSymlink(t *testing.T) {
dir := fs.NewDir(t, t.Name(), fs.WithFile("real-config.json", `{}`))
defer dir.Remove()
symLink := dir.Join("config.json")
relativeRealFile := "real-config.json"
realFile := dir.Join(relativeRealFile)
err := os.Symlink(relativeRealFile, symLink)
assert.NilError(t, err)
configFile := New(symLink)
err = configFile.Save()
assert.NilError(t, err)
fi, err := os.Lstat(symLink)
assert.NilError(t, err)
assert.Assert(t, fi.Mode()&os.ModeSymlink != 0, "expected %s to be a symlink", symLink)
cfg, err := os.ReadFile(symLink)
assert.NilError(t, err)
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
cfg, err = os.ReadFile(realFile)
assert.NilError(t, err)
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
}
func TestPluginConfig(t *testing.T) {
configFile := New("test-plugin")
defer os.Remove("test-plugin")