about summary refs log tree commit diff
path: root/tools/eaglemode/wrapper.go
blob: 841642b6d93f03291355e1c9656631d349466d90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Eagle Mode configuration wrapper that recreates the required directory
// structure for Eagle Mode based on the output of depot.tools.eaglemode.etcDir
//
// This will replace *all* symlinks in the Eagle Mode configuration directory,
// but it will not touch actual files. Missing folders will be created.
package main

import (
	"flag"
	"fmt"
	"io/fs"
	"log"
	"os"
	"os/user"
	"path"
	"path/filepath"
	"strings"
)

func configDir() (string, error) {
	v := os.Getenv("EM_USER_CONFIG_DIR")
	if v != "" {
		return v, nil
	}

	usr, err := user.Current()
	if err != nil {
		return "", fmt.Errorf("failed to get current user: %w", err)
	}

	return path.Join(usr.HomeDir, ".eaglemode"), nil
}

// cleanupConfig removes *all* existing symlinks in the configuration which do
// not point into the right Nix store path.
func cleanupConfig(conf string, dir string) (map[string]bool, error) {
	// In case of first launch, we might have to create the directory.
	_ = os.MkdirAll(dir, 0755)
	c := 0

	currentFiles := map[string]bool{}

	walker := func(p string, d fs.DirEntry, e error) error {
		if e != nil {
			return fmt.Errorf("could not walk %s in config directory: %w", p, e)
		}

		if d.Type()&fs.ModeSymlink != 0 {
			target, err := os.Readlink(p)
			if err != nil {
				return fmt.Errorf("could not read link for %s: %w", p, err)
			}

			if !strings.HasPrefix(target, conf) {
				err = os.Remove(p)
				c++
				if err != nil {
					return fmt.Errorf("could not remove stale link %q: %w", p, err)
				}
				log.Printf("removed stale symlink %q", p)
			} else {
				currentFiles[p] = false
			}
		}

		if d.Type().IsRegular() {
			currentFiles[p] = true
		}

		return nil
	}

	err := filepath.WalkDir(dir, walker)
	if err != nil {
		return nil, err
	}

	if c > 0 {
		log.Printf("removed %v stale symlinks", c)
	}

	return currentFiles, nil
}

// linkConfig traverses the given Eagle Mode configuration and links everything
// to the expected location in the user's configuration directory.
//
// If the user placed actual files in the configuration directory at paths that
// would be overwritten, they will not be touched.
func linkConfig(conf string, dir string, existing map[string]bool) error {
	walker := func(p string, d fs.DirEntry, e error) error {
		if e != nil {
			return fmt.Errorf("could not walk %s in config directory: %w", p, e)
		}

		target := path.Join(dir, strings.TrimPrefix(p, conf))

		if d.Type().IsDir() {
			err := os.MkdirAll(target, 0755)
			if err != nil {
				return fmt.Errorf("could not create directory %q: %w", target, err)
			}

			return nil
		}

		if shadow, exists := existing[target]; exists {
			if shadow {
				log.Printf("WARN: file %q already exists and shadows a file from configuration", target)
			}

			return nil
		}

		err := os.Symlink(p, target)
		if err != nil {
			return fmt.Errorf("failed to link %q: %w", target, err)
		}

		return nil
	}

	return filepath.WalkDir(conf, walker)
}

func main() {
	emConfig := flag.String("em-config", "", "path to em-config dir")

	flag.Parse()
	log.Println("verifying current Eagle Mode configuration")

	if *emConfig == "" {
		log.Fatalf("Eagle Mode configuration must be given")
	}

	if !strings.HasPrefix(*emConfig, "/nix/store/") {
		log.Fatalf("Eagle Mode configuration must be in Nix store")
	}

	dir, err := configDir()
	if err != nil {
		log.Fatalf("could not determine Eagle Mode config dir: %v", err)
	}

	currentFiles, err := cleanupConfig(*emConfig, dir)
	if err != nil {
		log.Fatalf("failed to remove stale symlinks: %v", err)
	}

	err = linkConfig(*emConfig, dir, currentFiles)
	if err != nil {
		log.Fatalf("failed to link new configuration: %v", err)
	}

	log.Println("Eagle Mode configuration updated")
}