From 6cd1769c9d4e1ccadf366d92f811149305745f0e Mon Sep 17 00:00:00 2001 From: eikendev Date: Thu, 14 Jan 2021 17:42:30 +0100 Subject: [PATCH] Initialize repository --- .editorconfig | 18 ++++++++++++++++ .gitignore | 19 +++++++++++++++++ .vscode/tasks.json | 35 +++++++++++++++++++++++++++++++ LICENSE | 7 +++++++ Makefile | 21 +++++++++++++++++++ README.md | 21 +++++++++++++++++++ cmd/application/create.go | 22 ++++++++++++++++++++ cmd/application/delete.go | 22 ++++++++++++++++++++ cmd/application/list.go | 19 +++++++++++++++++ cmd/application/mod.go | 8 +++++++ cmd/application/show.go | 22 ++++++++++++++++++++ cmd/credentials.go | 44 +++++++++++++++++++++++++++++++++++++++ cmd/pbcli.go | 36 ++++++++++++++++++++++++++++++++ cmd/settings/mod.go | 12 +++++++++++ cmd/user/create.go | 23 ++++++++++++++++++++ cmd/user/delete.go | 22 ++++++++++++++++++++ cmd/user/list.go | 19 +++++++++++++++++ cmd/user/mod.go | 8 +++++++ cmd/user/show.go | 22 ++++++++++++++++++++ go.mod | 8 +++++++ go.sum | 12 +++++++++++ 21 files changed, 420 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .vscode/tasks.json create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 cmd/application/create.go create mode 100644 cmd/application/delete.go create mode 100644 cmd/application/list.go create mode 100644 cmd/application/mod.go create mode 100644 cmd/application/show.go create mode 100644 cmd/credentials.go create mode 100644 cmd/pbcli.go create mode 100644 cmd/settings/mod.go create mode 100644 cmd/user/create.go create mode 100644 cmd/user/delete.go create mode 100644 cmd/user/list.go create mode 100644 cmd/user/mod.go create mode 100644 cmd/user/show.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d23df43 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = tab +insert_final_newline = true +max_line_length = 120 +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_style = space + +[Makefile] +indent_style = tab diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49df2b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +pbcli + +### 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/ + diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..44f409f --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,35 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "make build", + "presentation": { + "panel": "shared", + "reveal": "always", + "focus": true + }, + "problemMatcher": [], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "test", + "type": "shell", + "command": "make test", + "presentation": { + "panel": "shared", + "reveal": "always", + "focus": true + }, + "problemMatcher": [], + "group": { + "kind": "test", + "isDefault": true + } + } + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..889e3fe --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +ISC License (ISC) + +Copyright 2021 eikendev + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8557a05 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +.PHONY: build +build: + go build -ldflags="-w -s" -o pbcli ./cmd + +.PHONY: test +test: + stdout=$$(gofmt -l . 2>&1); \ + if [ "$$stdout" ]; then \ + exit 1; \ + fi + gocyclo -over 10 $(shell find . -iname '*.go' -type f) + go test -v -cover ./... + stdout=$$(golint ./... 2>&1); \ + if [ "$$stdout" ]; then \ + exit 1; \ + fi + +.PHONY: setup +setup: + go get -u github.com/fzipp/gocyclo/cmd/gocyclo + go get -u golang.org/x/lint/golint diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0e8b08 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +![License](https://img.shields.io/github/license/pushbits/cli) + +# PushBits CLI + +## About + +PushBits is a relay server for push notifications. +It enables your services to send notifications via a simple web API, and delivers them to you through [Matrix](https://matrix.org/). +This is similar to what [PushBullet](https://www.pushbullet.com/), [Pushover](https://pushover.net/), and [Gotify](https://gotify.net/) offer, but a lot less complex. + +This command line tool enables users to create and modify applications. +Further, it can be used by administrators to administrate to add and remove users. + +## Development + +The source code is located on [GitHub](https://github.com/pushbits/cli). +You can retrieve it by checking out the repository as follows. + +```bash +git clone https://github.com/pushbits/cli.git +``` diff --git a/cmd/application/create.go b/cmd/application/create.go new file mode 100644 index 0000000..c6d3e6d --- /dev/null +++ b/cmd/application/create.go @@ -0,0 +1,22 @@ +package application + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type createCommand struct { + Arguments struct { + Name string `positional-arg-name:"name" description:"The name of the application"` + } `required:"true" positional-args:"true"` +} + +func (c *createCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *createCommand) Run(s settings.Settings) { + log.Printf("createCommand") +} diff --git a/cmd/application/delete.go b/cmd/application/delete.go new file mode 100644 index 0000000..10bf488 --- /dev/null +++ b/cmd/application/delete.go @@ -0,0 +1,22 @@ +package application + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type deleteCommand struct { + Arguments struct { + ID uint `positional-arg-name:"id" description:"The ID of the application"` + } `required:"true" positional-args:"true"` +} + +func (c *deleteCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *deleteCommand) Run(s settings.Settings) { + log.Printf("deleteCommand") +} diff --git a/cmd/application/list.go b/cmd/application/list.go new file mode 100644 index 0000000..bcb76d1 --- /dev/null +++ b/cmd/application/list.go @@ -0,0 +1,19 @@ +package application + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type listCommand struct { +} + +func (c *listCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *listCommand) Run(s settings.Settings) { + log.Printf("listCommand") +} diff --git a/cmd/application/mod.go b/cmd/application/mod.go new file mode 100644 index 0000000..a923dc9 --- /dev/null +++ b/cmd/application/mod.go @@ -0,0 +1,8 @@ +package application + +type Command struct { + Create createCommand `command:"create" alias:"c" description:"Create a new application for a user"` + Delete deleteCommand `command:"delete" alias:"d" description:"Delete an existing application for a user"` + List listCommand `command:"list" alias:"l" description:"List all existing applications of the user"` + Show showCommand `command:"show" alias:"s" description:"Show details of an existing application of a user"` +} diff --git a/cmd/application/show.go b/cmd/application/show.go new file mode 100644 index 0000000..f01743e --- /dev/null +++ b/cmd/application/show.go @@ -0,0 +1,22 @@ +package application + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type showCommand struct { + Arguments struct { + ID uint `positional-arg-name:"id" description:"The ID of the application"` + } `required:"true" positional-args:"true"` +} + +func (c *showCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *showCommand) Run(s settings.Settings) { + log.Printf("showCommand") +} diff --git a/cmd/credentials.go b/cmd/credentials.go new file mode 100644 index 0000000..9d4cca2 --- /dev/null +++ b/cmd/credentials.go @@ -0,0 +1,44 @@ +package main + +import ( + "flag" + "fmt" + "log" + "syscall" + + "golang.org/x/crypto/ssh/terminal" +) + +type Credentials struct { + Url string + Username string + Password string +} + +func getCredentials(args []string) *Credentials { + credFlags := flag.NewFlagSet("cred", flag.ExitOnError) + url := credFlags.String("url", "", "The URL on which the server is listening") + username := credFlags.String("username", "", "The username to authenticate on the server") + + credFlags.Parse(args) + + if *url == "" { + log.Fatal("No URL was supplied.") + } + + if *username == "" { + log.Fatal("No username was supplied.") + } + + fmt.Print("Password: ") + password, err := terminal.ReadPassword(int(syscall.Stdin)) + if err != nil { + log.Fatal("No password was supplied.") + } + + return &Credentials{ + Url: *url, + Username: *username, + Password: string(password), + } +} diff --git a/cmd/pbcli.go b/cmd/pbcli.go new file mode 100644 index 0000000..016ded5 --- /dev/null +++ b/cmd/pbcli.go @@ -0,0 +1,36 @@ +package main + +import ( + "os" + + "github.com/jessevdk/go-flags" + + "github.com/pushbits/cli/cmd/application" + "github.com/pushbits/cli/cmd/settings" + "github.com/pushbits/cli/cmd/user" +) + +type Commands struct { + settings.Settings + Application application.Command `command:"application" alias:"a" description:"Configure applications"` + User user.Command `command:"user" alias:"u" description:"Configure users"` +} + +var ( + commands Commands + parser = flags.NewParser(&commands, flags.Default) +) + +func main() { + _, err := parser.Parse() + if err != nil { + os.Exit(1) + } + + s := settings.Settings{ + URL: commands.URL, + Username: commands.Username, + } + + settings.Runner.Run(s) +} diff --git a/cmd/settings/mod.go b/cmd/settings/mod.go new file mode 100644 index 0000000..85f1054 --- /dev/null +++ b/cmd/settings/mod.go @@ -0,0 +1,12 @@ +package settings + +type Settings struct { + URL string `long:"url" description:"The URL where the server listens for requests" required:"true"` + Username string `long:"username" description:"The username for authenticating on the server" required:"true"` +} + +type Runnable interface { + Run(Settings) +} + +var Runner Runnable diff --git a/cmd/user/create.go b/cmd/user/create.go new file mode 100644 index 0000000..d1d5af5 --- /dev/null +++ b/cmd/user/create.go @@ -0,0 +1,23 @@ +package user + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type createCommand struct { + Arguments struct { + Name string `positional-arg-name:"name" description:"The name of the user"` + MatrixID string `positional-arg-name:"matrixid" description:"The Matrix ID of the user"` + } `required:"true" positional-args:"true"` +} + +func (c *createCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *createCommand) Run(s settings.Settings) { + log.Printf("createCommand") +} diff --git a/cmd/user/delete.go b/cmd/user/delete.go new file mode 100644 index 0000000..90d290b --- /dev/null +++ b/cmd/user/delete.go @@ -0,0 +1,22 @@ +package user + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type deleteCommand struct { + Arguments struct { + ID uint `positional-arg-name:"id" description:"The ID of the user"` + } `required:"true" positional-args:"true"` +} + +func (c *deleteCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *deleteCommand) Run(s settings.Settings) { + log.Printf("deleteCommand") +} diff --git a/cmd/user/list.go b/cmd/user/list.go new file mode 100644 index 0000000..cabcb5d --- /dev/null +++ b/cmd/user/list.go @@ -0,0 +1,19 @@ +package user + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type listCommand struct { +} + +func (c *listCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *listCommand) Run(s settings.Settings) { + log.Printf("listCommand") +} diff --git a/cmd/user/mod.go b/cmd/user/mod.go new file mode 100644 index 0000000..2f17909 --- /dev/null +++ b/cmd/user/mod.go @@ -0,0 +1,8 @@ +package user + +type Command struct { + Create createCommand `command:"create" alias:"c" description:"Create a new user"` + Delete deleteCommand `command:"delete" alias:"d" description:"Delete an existing user"` + List listCommand `command:"list" alias:"l" description:"List all existing users"` + Show showCommand `command:"show" alias:"s" description:"Show details of an existing user"` +} diff --git a/cmd/user/show.go b/cmd/user/show.go new file mode 100644 index 0000000..f65b85a --- /dev/null +++ b/cmd/user/show.go @@ -0,0 +1,22 @@ +package user + +import ( + "log" + + "github.com/pushbits/cli/cmd/settings" +) + +type showCommand struct { + Arguments struct { + ID uint `positional-arg-name:"id" description:"The ID of the user"` + } `required:"true" positional-args:"true"` +} + +func (c *showCommand) Execute(args []string) error { + settings.Runner = c + return nil +} + +func (c *showCommand) Run(s settings.Settings) { + log.Printf("showCommand") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5cbbbe5 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/pushbits/cli + +go 1.14 + +require ( + github.com/jessevdk/go-flags v1.4.0 + golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bfbbdfa --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= +golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=