Message
fix: executable path in hooks (#459)
* fix: executable path in hooks
On platforms, where soft-serve executable gets installed in a path that
is not the same throughout installation, or upgrades, hooks break. Some
examples of such a situation is when moving soft-serve repositories from
one OS to another OS, or on NixOS/Guix
This commit passes the path to the current executable as an environment
variable $SOFT_SERVE_BIN_PATH to the hooks.
To fix/update existing repository hooks:
[/path/to/soft-serve/repos] $ grep -ERl \
'^[^[:space:]]+soft-serve[^[:space:]]+' . | xargs sed -r -e \
's,^([^[:space:]]*soft-serve[^[:space:]]*),"${SOFT_SERVE_BIN_PATH}",g' -i
Signed-off-by: Ashish SHUKLA <ashish.is@lostca.se>
Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com>
* fix: update pkg/config/config.go
* fix: update pkg/config/config.go
---------
Signed-off-by: Ashish SHUKLA <ashish.is@lostca.se>
Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com>
Diff
1diff --git a/pkg/config/config.go b/pkg/config/config.go
2index ecbd940c7d0b05fa69bbabf1a94f48b3085887fb..467b00d2e0acb56ddc0619ac91d43a86ea5785e5 100644
3--- a/pkg/config/config.go
4+++ b/pkg/config/config.go
5@@ -14,6 +14,8 @@ import (
6 "gopkg.in/yaml.v3"
7 )
8
9+var binPath = "soft"
10+
11 // SSHConfig is the configuration for the SSH server.
12 type SSHConfig struct {
13 // ListenAddr is the address on which the SSH server will listen.
14@@ -151,7 +153,9 @@ type Config struct {
15
16 // Environ returns the config as a list of environment variables.
17 func (c *Config) Environ() []string {
18- envs := []string{}
19+ envs := []string{
20+ fmt.Sprintf("SOFT_SERVE_BIN_PATH=%s", binPath),
21+ }
22 if c == nil {
23 return envs
24 }
25@@ -419,3 +423,9 @@ func parseAuthKeys(aks []string) []ssh.PublicKey {
26 func (c *Config) AdminKeys() []ssh.PublicKey {
27 return parseAuthKeys(c.InitialAdminKeys)
28 }
29+
30+func init() {
31+ if ex, err := os.Executable(); err == nil {
32+ binPath = filepath.ToSlash(ex)
33+ }
34+}
35diff --git a/pkg/hooks/gen.go b/pkg/hooks/gen.go
36index 467b2f263a32263a016c08f39ea9357d19b2f98c..9e445040a26207b838c6e2d7ad45a820e492f1ec 100644
37--- a/pkg/hooks/gen.go
38+++ b/pkg/hooks/gen.go
39@@ -35,14 +35,6 @@ func GenerateHooks(_ context.Context, cfg *config.Config, repo string) error {
40 return err
41 }
42
43- ex, err := os.Executable()
44- if err != nil {
45- return err
46- }
47-
48- // Convert to forward slashes for Windows.
49- ex = filepath.ToSlash(ex)
50-
51 for _, hook := range []string{
52 PreReceiveHook,
53 UpdateHook,
54@@ -78,7 +70,7 @@ func GenerateHooks(_ context.Context, cfg *config.Config, repo string) error {
55 Hook string
56 Args string
57 }{
58- Executable: ex,
59+ Executable: "\"${SOFT_SERVE_BIN_PATH}\"",
60 Hook: hook,
61 Args: args,
62 }); err != nil {
63@@ -88,7 +80,7 @@ func GenerateHooks(_ context.Context, cfg *config.Config, repo string) error {
64
65 // Write the soft-serve hook inside ${hook}.d directory.
66 hp = filepath.Join(hp, "soft-serve")
67- err = os.WriteFile(hp, data.Bytes(), os.ModePerm) //nolint:gosec
68+ err := os.WriteFile(hp, data.Bytes(), os.ModePerm) //nolint:gosec
69 if err != nil {
70 log.WithPrefix("hooks").Error("failed to write hook", "err", err)
71 continue