Skip to content

Commit

Permalink
Merge pull request #3 from echgo/development
Browse files Browse the repository at this point in the history
fix: Update filename & add package documentation.
  • Loading branch information
gowizzard authored Oct 5, 2022
2 parents 8cacc25 + 7c59a97 commit 585aade
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ jobs:

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./..
16 changes: 0 additions & 16 deletions .github/workflows/greetings.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# First irgnore all unused folders.
# After that we ignore all other files.
.idea/
notifile_test.go
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<div align="center">

# notifile

</div>

[![GitHub go.mod Go version of a Go module](https://img.shields.io/github/go-mod/go-version/echgo/notifile.svg)](https://golang.org/) [![Go](https://github.com/echgo/notifile/actions/workflows/go.yml/badge.svg)](https://github.com/echgo/notifile/actions/workflows/go.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/echgo/notifile)](https://goreportcard.com/report/github.com/echgo/notifile) [![Go Doc](https://godoc.org/github.com/echgo/notifile?status.svg)](https://pkg.go.dev/github.com/echgo/notifile) [![GitHub issues](https://img.shields.io/github/issues/echgo/notifile)](https://github.com/echgo/notifile/issues) [![GitHub forks](https://img.shields.io/github/forks/echgo/notifile)](https://github.com/echgo/notifile/network) [![GitHub stars](https://img.shields.io/github/stars/echgo/notifile)](https://github.com/echgo/notifile/stargazers) [![GitHub license](https://img.shields.io/github/license/echgo/notifile)](https://github.com/echgo/notifile/blob/master/LICENSE)

With this library you can easily create notification files for your Go projects. These must then be stored in the directory you specify so that echGo can read them.
Expand All @@ -12,7 +16,7 @@ go get github.com/echgo/notifile

## How to use?

You can use the following example to create a file. Please note that there are currently only the following notification channels: **gotify, matrix, telegram, trello, smtp & webhook**.
You can use the following example to create a file. Please note that there are currently only the following notification channels: `gotify`, `pushover`, `matrix`, `telegram`, `discord`, `slack`, `trello`, `zendesk`, `osticket`, `twillo`, `smtp` & `webhook`.

```go
data := notifile.Data{
Expand All @@ -21,7 +25,7 @@ data := notifile.Data{
Message: "Here you will find your message.",
}

err := notifile.Create(data, "/var/lib/echgo/notification")
err := notifile.Create(data, filepath.Join("var", "lib", "echgo", "notification")
if err != nil {
log.Fatalln(err)
}
Expand Down
25 changes: 19 additions & 6 deletions create.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
// 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, extension is to build the filename
// Set prefix, extension is to build the filename.
const (
prefix = "notifile-"
char = "abcdefghijklmnopqrstuvwxyz1234567890"
extension = ".json"
)

// Data is to structure the file data
// 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
// 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 {
Expand All @@ -33,7 +41,12 @@ func Create(data Data, path string) error {
return errors.New("unfortunately the directory does not exist")
}

name := prefix + time.Now().Format("20060102150405") + extension
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 {
Expand Down
52 changes: 45 additions & 7 deletions create_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,60 @@
// 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_test

import (
"fmt"
"github.com/echgo/notifile"
"os"
"testing"
)

// TestCreate is to test the create function
func TestCreate(t *testing.T) {

data := notifile.Data{
Channels: nil,
Headline: "Testing",
Message: "This is a message about test corners.",
tests := []struct {
path string
channels []string
headline string
message string
}{
{
path: os.TempDir(),
channels: []string{"gotify", "smtp"},
headline: "Testing",
message: "This is the fist message about test corners.",
},
{
path: os.TempDir(),
channels: []string{"smtp"},
headline: "Testing",
message: "This is the second message about test corners.",
},
{
path: os.TempDir(),
channels: []string{"trello"},
headline: "Testing",
message: "This is the third message about test corners.",
},
}

err := notifile.Create(data, "/path/to/your/folder")
if err != nil {
t.Fatal(err)
for _, value := range tests {

data := notifile.Data{
Channels: value.channels,
Headline: value.headline,
Message: value.message,
}

fmt.Println(value.path)

err := notifile.Create(data, value.path)
if err != nil {
t.Fatal(err)
}

}

}

0 comments on commit 585aade

Please sign in to comment.