From 214f17f90f822dfcaa13c4c02ec22d68faf26db9 Mon Sep 17 00:00:00 2001 From: Luther Date: Thu, 21 May 2020 03:00:59 -0700 Subject: [PATCH 1/2] vendor --- go.mod | 1 + go.sum | 2 + .../logrus-easy-formatter/.gitignore | 1 + .../t-tomalak/logrus-easy-formatter/LICENSE | 21 +++++ .../t-tomalak/logrus-easy-formatter/README.md | 81 +++++++++++++++++++ .../logrus-easy-formatter/formatter.go | 61 ++++++++++++++ .../t-tomalak/logrus-easy-formatter/go.mod | 5 ++ .../t-tomalak/logrus-easy-formatter/go.sum | 9 +++ vendor/modules.txt | 2 + 9 files changed, 183 insertions(+) create mode 100644 vendor/github.com/t-tomalak/logrus-easy-formatter/.gitignore create mode 100644 vendor/github.com/t-tomalak/logrus-easy-formatter/LICENSE create mode 100644 vendor/github.com/t-tomalak/logrus-easy-formatter/README.md create mode 100644 vendor/github.com/t-tomalak/logrus-easy-formatter/formatter.go create mode 100644 vendor/github.com/t-tomalak/logrus-easy-formatter/go.mod create mode 100644 vendor/github.com/t-tomalak/logrus-easy-formatter/go.sum diff --git a/go.mod b/go.mod index 5deb91b..9bb3d1e 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,6 @@ require ( github.com/goodhosts/hostsfile v0.0.1 github.com/olekukonko/tablewriter v0.0.4 github.com/sirupsen/logrus v1.4.2 + github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 github.com/urfave/cli/v2 v2.1.1 ) diff --git a/go.sum b/go.sum index 4c9dc14..5b7d554 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk= +github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA= github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= diff --git a/vendor/github.com/t-tomalak/logrus-easy-formatter/.gitignore b/vendor/github.com/t-tomalak/logrus-easy-formatter/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/vendor/github.com/t-tomalak/logrus-easy-formatter/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/vendor/github.com/t-tomalak/logrus-easy-formatter/LICENSE b/vendor/github.com/t-tomalak/logrus-easy-formatter/LICENSE new file mode 100644 index 0000000..fa7821c --- /dev/null +++ b/vendor/github.com/t-tomalak/logrus-easy-formatter/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Tomasz Tomalak + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/t-tomalak/logrus-easy-formatter/README.md b/vendor/github.com/t-tomalak/logrus-easy-formatter/README.md new file mode 100644 index 0000000..71488a3 --- /dev/null +++ b/vendor/github.com/t-tomalak/logrus-easy-formatter/README.md @@ -0,0 +1,81 @@ +[![status](https://github.com/t-tomalak/logrus-easy-formatter/workflows/Go/badge.svg)](https://github.com/t-tomalak/logrus-easy-formatter/actions) +[![Go Report Card](https://goreportcard.com/badge/github.com/t-tomalak/logrus-easy-formatter)](https://goreportcard.com/report/github.com/t-tomalak/logrus-easy-formatter) +## Logrus Easy Formatter +Provided formatter allow to easily format [Logrus](https://github.com/sirupsen/logrus) log output +Some inspiration taken from [logrus-prefixed-formatter](https://github.com/x-cray/logrus-prefixed-formatter) + +## Default output +When format options are not provided `Formatter` will output +```bash +[INFO]: 2006-01-02T15:04:05Z07:00 - Log message +``` + +## Sample Usage +Sample usage using available option to format output +```go +package main + +import ( + "os" + + "github.com/sirupsen/logrus" + "github.com/t-tomalak/logrus-easy-formatter" +) + +func main() { + logger := &logrus.Logger{ + Out: os.Stderr, + Level: logrus.DebugLevel, + Formatter: &easy.Formatter{ + TimestampFormat: "2006-01-02 15:04:05", + LogFormat: "[%lvl%]: %time% - %msg%", + }, + } + + logger.Printf("Log message") +} +``` +Above sample will produce: +```bash +[INFO]: 27-02-2018 19:16:55 - Log message +``` + +##### Usage with custom fields +Package also allows to include custom fields and format them(for now only limited to strings) + +```go +package main + +import ( + "os" + + "github.com/sirupsen/logrus" + "github.com/t-tomalak/logrus-easy-formatter" +) + +func main() { + logger := &logrus.Logger{ + Out: os.Stderr, + Level: logrus.DebugLevel, + Formatter: &easy.Formatter{ + LogFormat: "[%lvl%]: %time% - %msg% {%customField%}", + }, + } + + logger.WithField("customField", "Sample value").Printf("Log message") +} +``` +And after running will output +```bash +[INFO]: 27-02-2018 19:16:55 - Log message - {Sample value} +``` + +## ToDo +- [x] Customizable timestamp formats +- [x] Customizable output formats +- [x] Add tests +- [ ] Support for custom fields other then `string` +- [ ] Tests against all characters + +## License +This project is under the MIT License. See the LICENSE file for the full license text. \ No newline at end of file diff --git a/vendor/github.com/t-tomalak/logrus-easy-formatter/formatter.go b/vendor/github.com/t-tomalak/logrus-easy-formatter/formatter.go new file mode 100644 index 0000000..37d3d36 --- /dev/null +++ b/vendor/github.com/t-tomalak/logrus-easy-formatter/formatter.go @@ -0,0 +1,61 @@ +// Package easy allows to easily format output of Logrus logger +package easy + +import ( + "strconv" + "strings" + "time" + + "github.com/sirupsen/logrus" +) + +const ( + // Default log format will output [INFO]: 2006-01-02T15:04:05Z07:00 - Log message + defaultLogFormat = "[%lvl%]: %time% - %msg%" + defaultTimestampFormat = time.RFC3339 +) + +// Formatter implements logrus.Formatter interface. +type Formatter struct { + // Timestamp format + TimestampFormat string + // Available standard keys: time, msg, lvl + // Also can include custom fields but limited to strings. + // All of fields need to be wrapped inside %% i.e %time% %msg% + LogFormat string +} + +// Format building log message. +func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { + output := f.LogFormat + if output == "" { + output = defaultLogFormat + } + + timestampFormat := f.TimestampFormat + if timestampFormat == "" { + timestampFormat = defaultTimestampFormat + } + + output = strings.Replace(output, "%time%", entry.Time.Format(timestampFormat), 1) + + output = strings.Replace(output, "%msg%", entry.Message, 1) + + level := strings.ToUpper(entry.Level.String()) + output = strings.Replace(output, "%lvl%", level, 1) + + for k, val := range entry.Data { + switch v := val.(type) { + case string: + output = strings.Replace(output, "%"+k+"%", v, 1) + case int: + s := strconv.Itoa(v) + output = strings.Replace(output, "%"+k+"%", s, 1) + case bool: + s := strconv.FormatBool(v) + output = strings.Replace(output, "%"+k+"%", s, 1) + } + } + + return []byte(output), nil +} diff --git a/vendor/github.com/t-tomalak/logrus-easy-formatter/go.mod b/vendor/github.com/t-tomalak/logrus-easy-formatter/go.mod new file mode 100644 index 0000000..235bd4c --- /dev/null +++ b/vendor/github.com/t-tomalak/logrus-easy-formatter/go.mod @@ -0,0 +1,5 @@ +module github.com/t-tomalak/logrus-easy-formatter + +go 1.12 + +require github.com/sirupsen/logrus v1.4.2 diff --git a/vendor/github.com/t-tomalak/logrus-easy-formatter/go.sum b/vendor/github.com/t-tomalak/logrus-easy-formatter/go.sum new file mode 100644 index 0000000..46e5ef0 --- /dev/null +++ b/vendor/github.com/t-tomalak/logrus-easy-formatter/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/vendor/modules.txt b/vendor/modules.txt index 79a8a33..e4ec11f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -14,6 +14,8 @@ github.com/russross/blackfriday/v2 github.com/shurcooL/sanitized_anchor_name # github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus +# github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 +github.com/t-tomalak/logrus-easy-formatter # github.com/urfave/cli/v2 v2.1.1 github.com/urfave/cli/v2 # golang.org/x/sys v0.0.0-20190422165155-953cdadca894 From d6eb6a615513e2df5d39e2b32ca9a39fb177e059 Mon Sep 17 00:00:00 2001 From: Luther Date: Thu, 21 May 2020 03:01:43 -0700 Subject: [PATCH 2/2] default output acts like fmt.Println, goreleaser project name --- .goreleaser.yml | 4 +--- README.md | 46 ++++++++++++++++++++++++---------------------- cmd/list.go | 4 ++-- main.go | 6 ++++++ 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 777d391..29ce72c 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,6 +1,4 @@ -before: - hooks: - - go mod tidy +project_name: goodhosts builds: - env: - GO111MODULE=on diff --git a/README.md b/README.md index 9a92c97..6f3cf6b 100644 --- a/README.md +++ b/README.md @@ -23,28 +23,30 @@ architecture. Unzip and run in place, put in your system path (linux: `/usr/loca For full usage directions simply call `goodhosts -h` ```shell -$ ./goodhosts -h - NAME: - goodhosts - manage your hosts file goodly - - USAGE: - goodhosts [global options] command [command options] [arguments...] - - COMMANDS: - check, c Check if ip or host exists - list, ls List all entries in the hostsfile - add, a Add an entry to the hostsfile - remove, rm, r Remove ip or host(s) if exists - debug, d Show debug table for hosts file - backup Backup hosts file - restore Restore hosts file from backup - help, h Shows a list of commands or help for one command - - GLOBAL OPTIONS: - --custom value override the default hosts file - --debug, -d Turn on verbose debug logging (default: false) - --quiet, -q Turn on off all logging (default: false) - --help, -h show help (default: false) +$ goodhosts --help +NAME: + goodhosts - manage your hosts file goodly + +USAGE: + goodhosts [global options] command [command options] [arguments...] + +COMMANDS: + check, c Check if ip or host exists + list, ls List all entries in the hostsfile + add, a Add an entry to the hostsfile + remove, rm, r Remove ip or host(s) if exists + debug, d Show debug table for hosts file + backup Backup hosts file + restore Restore hosts file from backup + edit, e Open hosts file in an editor, default vim + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --file value, -f value override the default hosts: /etc/hosts + --debug, -d Turn on verbose debug logging (default: false) + --quiet, -q Turn on off all logging (default: false) + --help, -h show help (default: false) + ``` Each sub-command can be called with a `-h` option to see detailed help information. diff --git a/cmd/list.go b/cmd/list.go index 4c5efd7..a596165 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -36,9 +36,9 @@ func list(c *cli.Context) error { } } - lineOutput := fmt.Sprintf("%s", line.Raw) + lineOutput := fmt.Sprintf("%s\n", line.Raw) if line.IsMalformed() { - lineOutput = fmt.Sprintf("%s # <<< Malformed!", lineOutput) + lineOutput = fmt.Sprintf("%s # <<< Malformed!\n", lineOutput) } logrus.Infof(lineOutput) diff --git a/main.go b/main.go index fb4b033..dc8db67 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/goodhosts/cli/cmd" "github.com/goodhosts/hostsfile" "github.com/sirupsen/logrus" + easy "github.com/t-tomalak/logrus-easy-formatter" "github.com/urfave/cli/v2" ) @@ -20,6 +21,11 @@ func main() { Before: func(ctx *cli.Context) error { if ctx.Bool("debug") { logrus.SetLevel(logrus.DebugLevel) + } else { + // treat logrus like fmt.Print + logrus.SetFormatter(&easy.Formatter{ + LogFormat: "%msg%", + }) } if ctx.Bool("quiet") { logrus.SetOutput(ioutil.Discard)