diff --git a/README.md b/README.md index e69de29..04a2fb0 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,70 @@ +# Common Library + +[![Go Report Card](https://goreportcard.com/badge/github.com/nikhilsbhat/common)](https://goreportcard.com/report/github.com/nikhilsbhat/common) +[![shields](https://img.shields.io/badge/license-MIT-blue)](https://github.com/nikhilsbhat/common/blob/master/LICENSE) +[![shields](https://godoc.org/github.com/nikhilsbhat/common?status.svg)](https://godoc.org/github.com/nikhilsbhat/common) +[![shields](https://img.shields.io/github/v/tag/nikhilsbhat/common.svg)](https://github.com/nikhilsbhat/common/tags) + +## Introduction + +Generic functions that would be handy while building your Golang utility. + +This library stands on the shoulders of various libraries built by some awesome folks. + +## Installation + +Get the latest version of GoCD sdk using `go get` command. + +```shell +go get github.com/nikhilsbhat/common@latest +``` + +Get specific version of the same. + +```shell +go get github.com/nikhilsbhat/common@v0.0.2 +``` + +## Usage + +```go +package main + +import ( + "github.com/nikhilsbhat/common/renderer" + "github.com/sirupsen/logrus" + "log" + "os" +) + +type Object struct { + Name string + Date string +} + +func main() { + newObject := []Object{ + {Name: "nikhil", Date: "01-01-2024"}, + {Name: "john", Date: "01-02-2024"}, + } + + logger := logrus.New() + render := renderer.GetRenderer(os.Stdout, logger, true, false, false, false) + + if err := render.Render(newObject); err != nil { + log.Fatal(err) + } +} +``` + +Above code should generate yaml as below: + +```yaml +--- +- Date: 01-01-2024 + Name: nikhil +- Date: 01-02-2024 + Name: john +``` + +More example of the libraries can be found [here](https://github.com/nikhilsbhat/common/blob/main/example). diff --git a/content/content.go b/content/content.go index fa5ff53..b2ed0c0 100644 --- a/content/content.go +++ b/content/content.go @@ -68,6 +68,7 @@ func (obj Object) CheckFileType(log *logrus.Logger) string { return FileTypeUnknown } +// String should return the string equivalent of Object. func (obj Object) String() string { return string(obj) } diff --git a/content/content_test.go b/content/content_test.go index 2b003cd..edc7853 100644 --- a/content/content_test.go +++ b/content/content_test.go @@ -3,12 +3,12 @@ package content_test import ( "bytes" "fmt" - "github.com/nikhilsbhat/common/renderer" "os" "testing" "github.com/nikhilsbhat/common/content" - goCdLogger "github.com/nikhilsbhat/gocd-sdk-go/pkg/logger" + "github.com/nikhilsbhat/common/renderer" + "github.com/nikhilsbhat/gocd-sdk-go/pkg/logger" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -17,11 +17,11 @@ var log *logrus.Logger //nolint:gochecknoinits func init() { - logger := logrus.New() - logger.SetLevel(goCdLogger.GetLoglevel("info")) - logger.WithField("gocd-cli", true) - logger.SetFormatter(&logrus.JSONFormatter{}) - log = logger + logrusLogger := logrus.New() + logrusLogger.SetLevel(logger.GetLoglevel("info")) + logrusLogger.WithField("gocd-cli", true) + logrusLogger.SetFormatter(&logrus.JSONFormatter{}) + log = logrusLogger } func TestObject_CheckFileType(t *testing.T) { @@ -81,10 +81,9 @@ name: "testing"`) {"D", "The Gopher", "800"}, } - logger := logrus.New() strReader := new(bytes.Buffer) - render := renderer.GetRenderer(strReader, logger, false, false, false, true) + render := renderer.GetRenderer(strReader, log, false, false, false, true) err := render.Render(data) assert.NoError(t, err) diff --git a/example/content_yaml.go b/example/content_yaml.go new file mode 100644 index 0000000..7e14aa5 --- /dev/null +++ b/example/content_yaml.go @@ -0,0 +1,25 @@ +package main //nolint:typecheck + +import ( + "fmt" + "log" + "os" + + "github.com/nikhilsbhat/common/content" + "github.com/sirupsen/logrus" +) + +func main() { + logger := logrus.New() + + fileData, err := os.ReadFile("../fixtures/sample.yaml") + if err != nil { + log.Fatal(err) + } + + obj := content.Object(fileData) + fileType := obj.CheckFileType(logger) + + fmt.Println(fileType) + // Above would identify the file content as yaml. +} diff --git a/example/render_json.go b/example/render_json.go new file mode 100644 index 0000000..0b1d00c --- /dev/null +++ b/example/render_json.go @@ -0,0 +1,42 @@ +package main + +import ( + "log" + "os" + + "github.com/nikhilsbhat/common/renderer" + "github.com/sirupsen/logrus" +) + +type Object struct { + Name string + Date string +} + +func main() { + newObject := []Object{ + {Name: "nikhil", Date: "01-01-2024"}, + {Name: "john", Date: "01-02-2024"}, + } + + logger := logrus.New() + render := renderer.GetRenderer(os.Stdout, logger, false, true, false, false) + + if err := render.Render(newObject); err != nil { + log.Fatal(err) + } +} + +/* +The above code should generate below json +[ + { + "Name": "nikhil", + "Date": "01-01-2024" + }, + { + "Name": "john", + "Date": "01-02-2024" + } +] +*/ diff --git a/example/render_yaml.go b/example/render_yaml.go new file mode 100644 index 0000000..e647879 --- /dev/null +++ b/example/render_yaml.go @@ -0,0 +1,32 @@ +package main //nolint:typecheck + +import ( + "log" + "os" + + "github.com/nikhilsbhat/common/renderer" + "github.com/sirupsen/logrus" +) + +func main() { + newObject := []Object{ + {Name: "nikhil", Date: "01-01-2024"}, + {Name: "john", Date: "01-02-2024"}, + } + + logger := logrus.New() + render := renderer.GetRenderer(os.Stdout, logger, true, false, false, false) + + if err := render.Render(newObject); err != nil { + log.Fatal(err) + } +} + +/* +The above code should generate below json +--- +- Date: 01-01-2024 + Name: nikhil +- Date: 01-02-2024 + Name: john +*/ diff --git a/prompt/shell_prompts.go b/prompt/shell_prompts.go index 28dc957..5ee696f 100644 --- a/prompt/shell_prompts.go +++ b/prompt/shell_prompts.go @@ -67,12 +67,12 @@ func (cfg *ReadConfig) Reader() (bool, Options) { return true, option } - cfg.logger.Errorf("the options should be one of '%s'", funk.FlattenDeep(cfg.InputOptions)) + cfg.logger.Errorf("the options should be one of '%s'", flattenedInputs) } } // Contains checks if user passed input is part of predefined Options. -// If yes returns true else returns false. +// If yes returns 'true' else returns 'false'. func (inputOptions Option) Contains(input string) (bool, Options) { var option Options diff --git a/prompt/shell_prompts_test.go b/prompt/shell_prompts_test.go new file mode 100644 index 0000000..ffed876 --- /dev/null +++ b/prompt/shell_prompts_test.go @@ -0,0 +1,32 @@ +package prompt_test + +import ( + "testing" + + "github.com/nikhilsbhat/common/prompt" + "github.com/stretchr/testify/assert" +) + +func TestOption_Contains(t *testing.T) { + options := []prompt.Options{ + { + Name: "testing", + Short: "t", + }, + { + Name: "coding", + Short: "c", + }, + } + + t.Run("should be able to find an option", func(t *testing.T) { + actual, option := prompt.Option(options).Contains("t") + assert.True(t, actual) + assert.Equal(t, options[0], option) + }) + t.Run("should be able to find an option", func(t *testing.T) { + actual, option := prompt.Option(options).Contains("test") + assert.False(t, actual) + assert.Equal(t, prompt.Options{}, option) + }) +} diff --git a/renderer/render_test.go b/renderer/render_test.go index 437b9d8..7cb9e8a 100644 --- a/renderer/render_test.go +++ b/renderer/render_test.go @@ -2,12 +2,15 @@ package renderer_test import ( "bytes" + "log" + "os" + "testing" + "github.com/nikhilsbhat/common/content" "github.com/nikhilsbhat/common/prompt" "github.com/nikhilsbhat/common/renderer" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" - "testing" ) func TestGetRenderer(t *testing.T) { @@ -108,4 +111,23 @@ func TestGetRenderer(t *testing.T) { err := render.Render([]prompt.ReadConfig{*cliShellReadConfig}) assert.NoError(t, err) }) + + t.Run("", func(t *testing.T) { + type Object struct { + Name string + Date string + } + + newObject := []Object{ + {Name: "nikhil", Date: "01-01-2024"}, + {Name: "jon", Date: "01-02-2024"}, + } + + logger := logrus.New() + render := renderer.GetRenderer(os.Stdout, logger, true, false, false, false) + + if err := render.Render(newObject); err != nil { + log.Fatal(err) + } + }) }