Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
adoublef committed Dec 22, 2023
1 parent 895b590 commit fe586b4
Show file tree
Hide file tree
Showing 21 changed files with 1,197 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
26 changes: 26 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
workflow_dispatch:
pull_request:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: 📦 Checkout Repository
uses: actions/checkout@v3
- name: 🐁 Go Installation
uses: actions/setup-go@v2
with:
go-version: 1.21
- name: 🔬 Run Tests
run: go test -v -race -timeout=10m -cover ./...

35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# sdk
<center>
<img height="200" src="https://golang.org/doc/gopher/gopherbw.png" alt="Gopher logo">
<h1>Prelude</h1>
</center>

The **Prelude Package** is a lightweight and modular utility library for Golang, designed to provide a set of common functions and tools to kickstart your Golang projects. It's not meant to be an extensive framework but rather a collection of useful helpers that can be used as a prelude to your Golang code.

## Installation

To use this prelude package in your Golang project, you can simply run:

```bash
go get -u github.com/adoublef/prelude
```

## Additional Packages

- [matryer/is](https://github.com/matryer/is) - Package is provides a lightweight extension to the standard library's testing capabilities.
- [samber/lo](https://github.com/samber/lo) - A Lodash-style Go library.
- [testcontainers](https://github.com/testcontainers/testcontainers-go) - Testcontainers is a package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests.

## Contribution

Contributions are welcome! If you have any ideas, bug fixes, or improvements, feel free to open an issue or submit a pull request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/new-feature`)
3. Commit your changes (`git commit -m 'Add new feature'`)
4. Push to the branch (`git push origin feature/new-feature`)
5. Open a pull request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
12 changes: 12 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 3

tasks:
default:
desc: list all
cmds:
- task --list-all

test:
desc: test runner
cmds:
- go test -v -count 1 {{.CLI_ARGS}}
43 changes: 43 additions & 0 deletions bytest/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// source https://medium.com/@nicksoetaert/using-io-reader-to-simulate-an-arbitrary-sized-file-in-golang-4df6287cae51
package bytest

import (
"io"
)

const (
B = 1 << (10 * iota)
KB
MB
GB // 1048576000 1073741824
)

func NewReader(n int) *Reader {
return &Reader{n: n}
}

// Reader simulates a fake read-only file of an arbitrary size.
// The contents of this file cycles through the lowercase alphabetical ascii characters (abcdefghijklmnopqrstuvwxyz)
// Data is generated as requested, so it is safe to generate a 100GB file with 1GB of memory free on your machine.
type Reader struct {
n int
off int
}

// Read reads lowercase ascii characters into b from f.
// n is number of bytes read.
// If a Read is attempted at the end of the file, io.EOF is returned.
func (r *Reader) Read(p []byte) (n int, err error) {
if r.n == r.off {
return 0, io.EOF
}
m := len(p)
if (r.n - r.off) < m {
m = r.n - r.off
}
for i := 0; i < m; i++ {
p[i] = byte('a' + (r.off+i)%26)
}
r.off += m
return m, nil
}
35 changes: 35 additions & 0 deletions bytest/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package bytest_test

import (
"io"
"testing"

"github.com/adoublef/sdk/bytest"
"github.com/matryer/is"
)

func TestReader(t *testing.T) {
t.Parallel()

tt := []struct {
name string
r *bytest.Reader
n int
}{
{
name: "1024 Bytes",
r: bytest.NewReader(bytest.KB),
n: bytest.KB,
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
is := is.NewRelaxed(t)

n, err := io.ReadAll(tc.r)
is.NoErr(err) // read all
is.Equal(len(n), tc.n)
})
}
}
43 changes: 43 additions & 0 deletions errgroup/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package errgroup

import (
"context"
"os/signal"
"syscall"

"golang.org/x/sync/errgroup"
)

// A Group is a collection of goroutines working on subtasks that are part of the same overall task.
type Group struct {
g *errgroup.Group
ctx context.Context
}

// A New Group with a shared Context
func New(ctx context.Context, funcs ...func(context.Context) error) *Group {
g := &Group{}
g.g, g.ctx = errgroup.WithContext(ctx)
g.Go(funcs...)
return g
}

// Go calls the given function in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the group exceeding the configured limit.
func (g *Group) Go(funcs ...func(context.Context) error) {
for _, f := range funcs {
fn := f
g.g.Go(func() error {
return fn(g.ctx)
})
}
}

// Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.
func (g *Group) Wait() error {
return g.g.Wait()
}

// NotifyContext returns a copy of the parent context that is marked done when one of the listed signals arrives
func NotifyContext(ctx context.Context) (context.Context, context.CancelFunc) {
return signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/adoublef/sdk

go 1.21.3

require (
github.com/maragudk/migrate v0.4.3
github.com/matryer/is v1.4.1
github.com/mattn/go-sqlite3 v1.14.18
golang.org/x/sync v0.5.0
)
Loading

0 comments on commit fe586b4

Please sign in to comment.