-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredirects.go
More file actions
48 lines (37 loc) · 880 Bytes
/
redirects.go
File metadata and controls
48 lines (37 loc) · 880 Bytes
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
package shorts
import (
"errors"
"fmt"
"log"
"os"
"github.com/pelletier/go-toml/v2"
)
type RedirectsMap struct {
Temporary map[string]string
Permanent map[string]string
}
var Redirects RedirectsMap
const RedirectsFile string = "config/redirects.toml"
func ReadRedirects() {
file, err := os.ReadFile(RedirectsFile)
if err != nil {
log.Fatal(err)
}
err = toml.Unmarshal([]byte(file), &Redirects)
if err != nil {
log.Fatal(err)
}
log.Printf("Loaded %d temporary and %d permanent redirects", len(Redirects.Temporary), len(Redirects.Permanent))
}
func WriteRedirects() error {
file, err := os.Create(RedirectsFile)
if err != nil {
return fmt.Errorf("unable to create redirects.toml: %w", err)
}
defer file.Close()
err = toml.NewEncoder(file).Encode(Redirects)
if err != nil {
return errors.New("unable to encode redirects.toml")
}
return nil
}