Skip to content

Commit

Permalink
Update notifile to new json format
Browse files Browse the repository at this point in the history
  • Loading branch information
gowizzard committed Apr 3, 2022
1 parent 1713fa1 commit 4355196
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can use the following example to create a file. Please note that there are c

```go
data := notifile.Data{
Channel: []string{"gotify", "telegram"},
Channels: []string{"gotify", "telegram"},
Headline: "New notification!",
Message: "Here you will find your message.",
}
Expand Down
21 changes: 8 additions & 13 deletions create.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
package notifile

import (
"encoding/json"
"errors"
"os"
"strings"
"time"
)

// Data is to structure the file data
type Data struct {
Channel []string
Headline string
Message string
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 len(data.Channel) == 0 {
if len(data.Channels) == 0 {
return errors.New("unfortunately, no channels are specified. Therefore, the file cannot be created")
}

if _, err := os.Stat(path); os.IsNotExist(err) {
return errors.New("unfortunately the directory does not exist")
}

name := "notifile-" + time.Now().Format("20060102150405") + ".txt"
name := "notifile-" + time.Now().Format("20060102150405") + ".json"

file, err := os.Create(path + "/" + name)
content, err := json.Marshal(data)
if err != nil {
return err
}
defer file.Close()

channel := strings.Join(data.Channel, ",")

content := "channel=" + channel + "\nheadline=" + data.Headline + "\nmessage=" + data.Message

_, err = file.WriteString(content)
err = os.WriteFile(path+"/"+name, content, 0644)
if err != nil {
return err
}
Expand Down

0 comments on commit 4355196

Please sign in to comment.