-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b7c4a4
commit 8efad9f
Showing
9 changed files
with
235 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters