Skip to content

Commit

Permalink
Add examples and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jan 20, 2024
1 parent 8b7c4a4 commit 8efad9f
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 12 deletions.
70 changes: 70 additions & 0 deletions 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).
1 change: 1 addition & 0 deletions content/content.go
Expand Up @@ -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)
}
17 changes: 8 additions & 9 deletions content/content_test.go
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions 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.
}
42 changes: 42 additions & 0 deletions 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"
}
]
*/
32 changes: 32 additions & 0 deletions 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
*/
4 changes: 2 additions & 2 deletions prompt/shell_prompts.go
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions 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)
})
}
24 changes: 23 additions & 1 deletion renderer/render_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}

0 comments on commit 8efad9f

Please sign in to comment.