Skip to content

Commit 6668297

Browse files
authored
Merge pull request #54 from drognisep/sync-webui-docs
Adding link refresh logic
2 parents 3c14b29 + 34dbaa9 commit 6668297

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
4141
- ### As Go module
4242

43-
> [!NOTE]
44-
> Always verify what will be run before using either of these methods.
43+
> [!NOTE]
44+
> Always verify what will be run before using either of these methods.
4545
4646
Run this command to get the latest `go-webui` module version, and to sync the WebUI C library that it is using.
4747

v2/sync-webui/main.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,44 @@ func main() {
5050
// Validate that these paths actually exist
5151
iferrExit(validatePaths(goWebuiPath, webuiPath))
5252
linkName := filepath.Join(goWebuiPath, "webui")
53-
// Exit if link already exists in the directory of the used go-webui version.
53+
// Remove the link if it already exists in the directory of the used go-webui version.
5454
if dirExists(linkName) {
55-
return
55+
iferrExit(tempPerms(goWebuiPath, 0733, func() error {
56+
return os.Remove(linkName)
57+
}))
5658
}
5759

5860
// Store original permissions.
5961
// Not strictly necessary, yet we ensure end without changes to the original permissions.
60-
ogPerms, err := getPerms(goWebuiPath)
61-
iferrExit(err)
62-
iferrExit(os.Chmod(goWebuiPath, 0200+ogPerms))
63-
// Linking allows using WebUI C even in cases of multiple go-webui versions without creating bloat.
64-
if err := os.Symlink(webuiPath, linkName); err != nil {
65-
if runtime.GOOS == "windows" {
66-
// (Requires Administrator privileges or Developer Mode)
67-
errExit("mklink failed. Run as Administrator if needed.")
62+
iferrExit(tempPerms(goWebuiPath, 0733, func() error {
63+
// Linking allows using WebUI C even in cases of multiple go-webui versions without creating bloat.
64+
if err := os.Symlink(webuiPath, linkName); err != nil {
65+
if runtime.GOOS == "windows" {
66+
// (Requires Administrator privileges or Developer Mode)
67+
return fmt.Errorf("%w: mklink failed, Run as Administrator if needed", err)
68+
}
69+
return err
6870
}
69-
errExit(err.Error())
70-
}
71-
// Restore original permissions.
72-
iferrExit(os.Chmod(goWebuiPath, ogPerms))
71+
return nil
72+
}))
7373
iferrExit(goTools.Cmd("mod", "tidy").Run())
7474
}
7575

76+
func tempPerms(path string, newPerms os.FileMode, fn func() error) error {
77+
fi, err := os.Stat(path)
78+
if err != nil {
79+
return fmt.Errorf("failed to stat file '%s': %w", path, err)
80+
}
81+
curPerms := fi.Mode()
82+
if err := os.Chmod(path, newPerms); err != nil {
83+
return fmt.Errorf("failed to set temp perms for file '%s': %w", path, err)
84+
}
85+
defer func() {
86+
_ = os.Chmod(path, curPerms)
87+
}()
88+
return fn()
89+
}
90+
7691
func validatePaths(goWebuiPath, webuiPath string) error {
7792
var errs []error
7893
if !dirExists(goWebuiPath) {

0 commit comments

Comments
 (0)