Skip to content

Commit f90513a

Browse files
authored
[pkgcfg] Remove config if package is removed (#324)
## Summary Removes config and broken symlinks when a package is removed. We could also clean up configs as part of `ensurePackagesAreInstalled` (which makes it more reliable) but this requires a bit of refactoring I didn't want to do as part of this PR. Small unrelated change: made `installMode` type an int using iota (which is a bit more inline with golang quasi-enum pattern) ## How was it tested? Manual testing: * Added nginx and mariadb. * Inspected conf directory * Removed one by one * Inspected conf directory at each step.
1 parent 7eb3f97 commit f90513a

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

devbox.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ func (d *Devbox) Remove(pkgs ...string) error {
147147
return err
148148
}
149149

150+
if featureflag.Get(featureflag.PKGConfig).Enabled() {
151+
if err := pkgcfg.Remove(d.configDir, uninstalledPackages); err != nil {
152+
return err
153+
}
154+
}
155+
150156
if err := d.ensurePackagesAreInstalled(uninstall); err != nil {
151157
return err
152158
}
@@ -452,6 +458,12 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
452458
}
453459
fmt.Println("done.")
454460

461+
if featureflag.Get(featureflag.PKGConfig).Enabled() {
462+
if err := pkgcfg.RemoveInvalidSymlinks(d.configDir); err != nil {
463+
return err
464+
}
465+
}
466+
455467
return nil
456468
}
457469

pkgcfg/pkgcfg.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func createEnvFile(pkg, rootDir string) error {
101101
}
102102
env += fmt.Sprintf("export %s=%s\n", k, escaped)
103103
}
104-
filePath := filepath.Join(rootDir, ".devbox/conf/", pkg, "/env")
104+
filePath := filepath.Join(rootDir, confPath, pkg, "/env")
105105
if err = createDir(filepath.Dir(filePath)); err != nil {
106106
return err
107107
}
@@ -192,7 +192,8 @@ func displaySourceEnvMessage(pkg, rootDir string, w io.Writer) error {
192192
if len(env) > 0 {
193193
_, err = fmt.Fprintf(
194194
w,
195-
"\nTo ensure environment is set, run `source .devbox/conf/%s/env`\n\n",
195+
"\nTo ensure environment is set, run `source %s/%s/env`\n\n",
196+
confPath,
196197
pkg,
197198
)
198199
}

pkgcfg/rm.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pkgcfg
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/pkg/errors"
8+
)
9+
10+
func Remove(rootDir string, pkgs []string) error {
11+
for _, pkg := range pkgs {
12+
if err := os.RemoveAll(filepath.Join(rootDir, confPath, pkg)); err != nil {
13+
return errors.WithStack(err)
14+
}
15+
}
16+
return nil
17+
}
18+
19+
func RemoveInvalidSymlinks(rootDir string) error {
20+
dirEntry, err := os.ReadDir(filepath.Join(rootDir, confPath, "bin"))
21+
if err != nil {
22+
return errors.WithStack(err)
23+
}
24+
for _, entry := range dirEntry {
25+
_, err := os.Stat(filepath.Join(rootDir, confPath, "bin", entry.Name()))
26+
if errors.Is(err, os.ErrNotExist) {
27+
os.Remove(filepath.Join(rootDir, confPath, "bin", entry.Name()))
28+
}
29+
}
30+
return nil
31+
}

0 commit comments

Comments
 (0)