-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.go
63 lines (51 loc) · 1.39 KB
/
create.go
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
// Copyright 2022 Jonas Kwiedor. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
// Package notifile is used to generate a json file that
// is prepared in such a way that it can be read by echgo.
package notifile
import (
"encoding/json"
"errors"
"math/rand"
"os"
"time"
)
// Set prefix, char, extension is to build the filename.
const (
prefix = "notifile-"
char = "abcdefghijklmnopqrstuvwxyz1234567890"
extension = ".json"
)
// Data is to structure the file data.
type Data struct {
Channels []string `json:"channels"`
Headline string `json:"headline"`
Message string `json:"message"`
}
// Create is to create a new notification file.
// First we create a new file with a timestamp
// after that we check the channel & add the data.
func Create(data Data, path string) error {
if data.Channels == nil {
return errors.New("unfortunately, no channels are specified")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return errors.New("unfortunately the directory does not exist")
}
name := prefix
for i := 0; i < 56; i++ {
rand.Seed(time.Now().UnixNano())
name += string(char[rand.Intn(len(char))])
}
name += extension
content, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
err = os.WriteFile(path+"/"+name, content, 0644)
if err != nil {
return err
}
return nil
}