From 532a4a99221b35f94170d9352c0de478afc767a1 Mon Sep 17 00:00:00 2001 From: Samuel Pua Date: Sat, 10 Sep 2022 02:05:15 +0800 Subject: [PATCH] Feat(messenger): Initial commit --- .gitignore | 74 +++++++++++++++++++++++++++++++++++++++++++ README.md | 23 ++++++++++++++ cmd/messenger/main.go | 66 ++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++ go.sum | 2 ++ 5 files changed, 170 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 cmd/messenger/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed28d25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,74 @@ +# Output +/messenger + +# dotenv +.env + +# VSCode +.vscode + + +# ---> Go +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# ---> Vim +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# ---> VisualStudioCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# ---> Linux +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + diff --git a/README.md b/README.md new file mode 100644 index 0000000..4842760 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Messenger + +## About +The project's goal is build a generic messenger that notifies through multiple channels + +## Usage +``` +usage: print [-h|--help] [--webhook] [--url ""] + + Prints provided string to stdout + +Arguments: + + -h --help Print help information + --webhook Use this flag to enable webhook sending + --url URL to be used for webhook +``` + +## Build +Use the following command to build the application: +``` +go build git.samuelpua.com/telboon/messenger/cmd/messenger +``` diff --git a/cmd/messenger/main.go b/cmd/messenger/main.go new file mode 100644 index 0000000..4091be8 --- /dev/null +++ b/cmd/messenger/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" + "strings" + + "github.com/akamensky/argparse" +) + +func main() { + // Create new parser object + parser := argparse.NewParser("print", "Prints provided string to stdout") + + // Create webhook flag + webhookFlag := parser.Flag("", "webhook", &argparse.Options{Help: "Use this flag to enable webhook sending"}) + webhookURL := parser.String("", "url", &argparse.Options{Help: "URL to be used for webhook"}) + + // Parse input + err := parser.Parse(os.Args) + if err != nil { + fmt.Print(parser.Usage(err)) + return + } + + // Check inputs are set + if *webhookFlag { + if webhookURL == nil || *webhookURL == "" { + fmt.Println("Webhook URL needs to be set if webhook is enabled.") + return + } + } + + // Checks if at least one flag is enabled + if *webhookFlag || false { + handleStdIn(webhookFlag, webhookURL) + } else { + fmt.Print(parser.Usage(err)) + } +} + +func handleStdIn(webhookFlag *bool, webhookURL *string) { + for { + var currStr string + _, err := fmt.Scanf("%s", &currStr) + if err != nil { + return + } + + // Handle string + if *webhookFlag { + err = sendWebhook(&currStr, webhookURL) + if err != nil { + log.Println(err) + } + } + } +} + +func sendWebhook(currStr *string, webhookURL *string) error { + bodyReader := strings.NewReader(*currStr) + _, err := http.Post(*webhookURL, "text/plain", bodyReader) + return err +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9106877 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.samuelpua.com/telboon/messenger + +go 1.17 + +require github.com/akamensky/argparse v1.4.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5bf91e2 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc= +github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=