Repository: mr-linch/go-tg
Pull request #1 (2022-06-17 10:38:18)

ci: lint, test and coverage


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..e020136
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,39 @@
+name: CI
+
+on:
+  pull_request:
+  push:
+    branches: [main]
+
+jobs:
+  lint:
+    name: Lint
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v2
+
+      - name: Lint
+        uses: golangci/golangci-lint-action@v2
+        with:
+          version: v1.46
+
+  test:
+    name: Test
+    runs-on: ubuntu-latest
+    steps:
+      - name: Install Go
+        uses: actions/setup-go@v2
+        with:
+          go-version: "1.18"
+
+      - name: Checkout
+        uses: actions/checkout@v2
+
+      - name: Test
+        run: go test -v -race -covermode=atomic -coverprofile=coverage.txt ./...
+
+      - name: Upload coverage
+        uses: codecov/codecov-action@v2
+        with:
+          token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
diff --git a/client.go b/client.go
new file mode 100644
index 0000000..8a8005f
--- /dev/null
+++ b/client.go
@@ -0,0 +1,50 @@
+package tg
+
+import "net/http"
+
+// Client is Telegram Bot API client structure.
+// Create new client with NewClient function.
+type Client struct {
+	// bot api token
+	token string
+
+	// bot api server base url,
+	// default values is https://api.telegram.org
+	server string
+
+	// http client,
+	// default values is http.DefaultClient
+	doer *http.Client
+}
+
+// ClientOption is a function that sets some option for Client.
+type ClientOption func(*Client)
+
+// WithServer sets custom server url for Client.
+func WithServer(server string) ClientOption {
+	return func(c *Client) {
+		c.server = server
+	}
+}
+
+// WithDoer sets custom http client for Client.
+func WithDoer(doer *http.Client) ClientOption {
+	return func(c *Client) {
+		c.doer = doer
+	}
+}
+
+// New creates new Client with given token and options.
+func New(token string, options ...ClientOption) *Client {
+	c := &Client{
+		token:  token,
+		server: "https://api.telegram.org",
+		doer:   http.DefaultClient,
+	}
+
+	for _, option := range options {
+		option(c)
+	}
+
+	return c
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..8ba1725
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,10 @@
+module github.com/mr-linch/go-tg
+
+go 1.18
+
+require (
+	github.com/davecgh/go-spew v1.1.0 // indirect
+	github.com/pmezard/go-difflib v1.0.0 // indirect
+	github.com/stretchr/testify v1.7.2 // indirect
+	gopkg.in/yaml.v3 v3.0.1 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..d05fbd9
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,10 @@
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
+github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=


Test patch:
diff --git a/client_test.go b/client_test.go
new file mode 100644
index 0000000..b28dc08
--- /dev/null
+++ b/client_test.go
@@ -0,0 +1,20 @@
+package tg
+
+import (
+	"net/http"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestNewClient(t *testing.T) {
+	doer := &http.Client{}
+
+	client := New("token",
+		WithServer("http://example.com"),
+		WithDoer(doer),
+	)
+
+	assert.Equal(t, "http://example.com", client.server)
+
+}