Skip to content

Commit 1b8e630

Browse files
committed
add auto db migrations & refactor
1 parent c617714 commit 1b8e630

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+933
-748
lines changed

.dockerignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ README.md
44
LICENSE
55
docker-compose.yml
66
Dockerfile
7-
gobin.json
8-
example.gobin.json
7+
gobin.toml
8+
example.gobin.toml

.github/workflows/cli.yml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@ on:
55
paths-ignore:
66
- "*.md"
77
- "gobin/**"
8-
- "assets/**"
8+
- "../../gobin/assets/**"
99
- "sql/**"
1010
- "styles/**"
11-
- "templates/**"
11+
- "../../gobin/templates/**"
1212

1313
jobs:
1414
cli-build:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
goos: [ linux, windows, darwin ]
19-
goarch: [ amd64, 386, arm, arm64 ]
18+
goos: [ "linux", "windows", "darwin" ]
19+
goarch: [ "amd64", "386", "arm", "arm64" ]
2020
exclude:
21-
- goos: darwin
22-
goarch: 386
23-
- goos: darwin
24-
goarch: arm
25-
- goos: windows
26-
goarch: arm
21+
- goos: "darwin"
22+
goarch: "386"
23+
- goos: "darwin"
24+
goarch: "arm"
25+
- goos: "windows"
26+
goarch: "arm"
2727
steps:
2828
- name: Checkout
29-
uses: actions/checkout@v3
29+
uses: actions/checkout@v4
3030
with:
3131
fetch-depth: 0
3232

3333
- name: Set up Go
34-
uses: actions/setup-go@v3
34+
uses: actions/setup-go@v5
3535
with:
36-
go-version: 1.22.x
36+
go-version: 1.23.x
3737
cache: true
3838

3939
- name: File Extension Windows
@@ -58,7 +58,7 @@ jobs:
5858
go build -ldflags="-X 'main.Version=${{ env.VERSION }}' -X 'main.Commit=${{ env.COMMIT }}' -X 'main.BuildTime=${{ env.BUILD_TIME }}'" -o dist/gobin-${{ matrix.goos }}-${{ matrix.goarch }}${{ env.file_name }} github.com/topi314/gobin/v2/cli
5959
6060
- name: Upload
61-
uses: actions/upload-artifact@v3
61+
uses: actions/upload-artifact@v4
6262
with:
6363
name: gobin-${{ matrix.goos }}-${{ matrix.goarch }}
6464
path: dist/*
@@ -69,3 +69,7 @@ jobs:
6969
with:
7070
artifacts: dist/*
7171
allowUpdates: true
72+
omitBodyDuringUpdate: true
73+
omitDraftDuringUpdate: true
74+
omitNameDuringUpdate: true
75+
omitPrereleaseDuringUpdate: true

.github/workflows/server.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ on:
44
push:
55
paths-ignore:
66
- "*.md"
7-
- "cmd/**"
7+
- "../../cli/cmd/**"
88
- "cli/**"
99

1010
jobs:
1111
build:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Checkout
15-
uses: actions/checkout@v3
15+
uses: actions/checkout@v4
1616
with:
1717
fetch-depth: 0
1818

1919
- name: Docker meta
2020
id: meta
21-
uses: docker/metadata-action@v4
21+
uses: docker/metadata-action@v5
2222
with:
2323
images: ghcr.io/${{ github.repository }}
2424
tags: |
@@ -28,13 +28,13 @@ jobs:
2828
type=sha,prefix=
2929
3030
- name: Set up QEMU
31-
uses: docker/setup-qemu-action@v2
31+
uses: docker/setup-qemu-action@v3
3232

3333
- name: Set up Docker Buildx
34-
uses: docker/setup-buildx-action@v2
34+
uses: docker/setup-buildx-action@v3
3535

3636
- name: Login to GitHub Container Registry
37-
uses: docker/login-action@v2
37+
uses: docker/login-action@v3
3838
with:
3939
registry: ghcr.io
4040
username: ${{ github.repository_owner }}
@@ -47,7 +47,7 @@ jobs:
4747
echo "BUILD_TIME=$(date --rfc-3339=seconds)" >> $GITHUB_ENV
4848
4949
- name: Build and push
50-
uses: docker/build-push-action@v3
50+
uses: docker/build-push-action@v6
5151
with:
5252
context: .
5353
platforms: linux/amd64,linux/arm/v7,linux/arm64/v8

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.idea/
22
custom_styles/
3-
gobin.json
3+
gobin.toml
44
.gobin
55
gobin.db
66
go.work

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS build
1+
FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS build
22

33
WORKDIR /build
44

@@ -33,4 +33,4 @@ EXPOSE 80
3333

3434
ENTRYPOINT ["/bin/gobin"]
3535

36-
CMD ["-config", "/var/lib/gobin/gobin.json"]
36+
CMD ["-config", "/var/lib/gobin/gobin.toml"]

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ on [Packages](https://github.com/topi314/gobin/pkgs/container/gobin).
8787
Create a new `docker-compose.yml` file with the following content:
8888

8989
> [!Note]
90-
> You should change the password in the `docker-compose.yml` and `gobin.json` file.
90+
> You should change the password in the `docker-compose.yml` and `gobin.toml` file.
9191
9292
```yaml
9393
version: "3.8"
@@ -98,7 +98,7 @@ services:
9898
container_name: gobin
9999
restart: unless-stopped
100100
volumes:
101-
- ./gobin.json:/var/lib/gobin/gobin.json
101+
- ./gobin.toml:/var/lib/gobin/gobin.toml
102102
# use this for sqlite
103103
- ./gobin.db:/var/lib/gobin/gobin.db
104104
ports:
@@ -117,7 +117,7 @@ services:
117117
POSTGRES_PASSWORD: password
118118
```
119119
120-
For `gobin.json`/environment variables and database schema see [Configuration](#configuration).
120+
For `gobin.toml`/database schema see [Configuration](#configuration).
121121

122122
```bash
123123
docker-compose up -d
@@ -148,7 +148,7 @@ go install github.com/topi314/gobin/v2@latest
148148
##### Run
149149

150150
```bash
151-
gobin --config=gobin.json
151+
gobin --config=gobin.toml
152152
```
153153

154154
---
@@ -195,9 +195,9 @@ gobin help
195195

196196
## Configuration
197197

198-
The database schema is automatically created when you start gobin and there is no `documents` table in the database.
198+
The database schema is automatically created or migrated when you start gobin.
199199

200-
Create a new `gobin.json` file with the following content:
200+
Create a new `gobin.toml` file with the following content:
201201

202202
> [!Note]
203203
> Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
@@ -546,9 +546,9 @@ second `file-1` and so on.
546546
| style? | style name | Which style to use for the formatter |
547547
| expires? | Timestamp | When the document file should expire in RFC 3339 format |
548548

549-
| Header | Type | Description |
550-
|----------------------|-----------|---------------------------------------------------------|
551-
| Expires? | Timestamp | When the document file should expire in RFC 3339 format |
549+
| Header | Type | Description |
550+
|----------|-----------|---------------------------------------------------------|
551+
| Expires? | Timestamp | When the document file should expire in RFC 3339 format |
552552

553553
| Part Header | Type | Description |
554554
|---------------------|-----------|----------------------------------------------------------------------------------------------|
@@ -845,6 +845,7 @@ update the document.
845845

846846
> [!Note]
847847
> The update token will not change after updating the document. You can use the same token to update the document again.
848+
848849
```json5
849850
{
850851
"key": "hocwr6i6",

assets/icons/dark/github.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

assets/icons/light/github.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.
File renamed without changes.

cmd/env.go renamed to cli/cmd/env.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/spf13/cobra"
1111
"github.com/spf13/viper"
12+
1213
"github.com/topi314/gobin/v2/internal/cfg"
1314
)
1415

cmd/get.go renamed to cli/cmd/get.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
"github.com/spf13/cobra"
1313
"github.com/spf13/viper"
1414
"github.com/topi314/chroma/v2/lexers"
15-
"github.com/topi314/gobin/v2/gobin"
15+
1616
"github.com/topi314/gobin/v2/internal/ezhttp"
17+
"github.com/topi314/gobin/v2/server"
1718
)
1819

1920
func NewGetCmd(parent *cobra.Command) {
@@ -68,9 +69,11 @@ Will return the document with the id of jis74978.`,
6869
if err != nil {
6970
return fmt.Errorf("failed to get document versions: %w", err)
7071
}
71-
defer rs.Body.Close()
72+
defer func() {
73+
_ = rs.Body.Close()
74+
}()
7275

73-
var documentVersionsRs []gobin.DocumentResponse
76+
var documentVersionsRs []server.DocumentResponse
7477
if err = ezhttp.ProcessBody("get document versions", rs, &documentVersionsRs); err != nil {
7578
return err
7679
}
@@ -109,10 +112,12 @@ Will return the document with the id of jis74978.`,
109112
if err != nil {
110113
return fmt.Errorf("failed to get document: %w", err)
111114
}
112-
defer rs.Body.Close()
115+
defer func() {
116+
_ = rs.Body.Close()
117+
}()
113118

114119
if file != "" {
115-
var fileRs gobin.ResponseFile
120+
var fileRs server.ResponseFile
116121
if err = ezhttp.ProcessBody("get document file", rs, &fileRs); err != nil {
117122
return err
118123
}
@@ -131,17 +136,18 @@ Will return the document with the id of jis74978.`,
131136
if err != nil {
132137
return fmt.Errorf("failed to create file to write document: %w", err)
133138
}
134-
defer documentFile.Close()
139+
defer func() {
140+
_ = documentFile.Close()
141+
}()
135142

136-
_, err = documentFile.WriteString(content)
137-
if err != nil {
143+
if _, err = documentFile.WriteString(content); err != nil {
138144
return fmt.Errorf("failed to write document to file: %w", err)
139145
}
140146
cmd.Println("Document file saved to:", filePath)
141147
return nil
142148
}
143149

144-
var documentRs gobin.DocumentResponse
150+
var documentRs server.DocumentResponse
145151
if err = ezhttp.ProcessBody("get document", rs, &documentRs); err != nil {
146152
return err
147153
}
@@ -166,7 +172,9 @@ Will return the document with the id of jis74978.`,
166172
if err != nil {
167173
return fmt.Errorf("failed to create file to write document: %w", err)
168174
}
169-
defer documentFile.Close()
175+
defer func() {
176+
_ = documentFile.Close()
177+
}()
170178

171179
_, err = documentFile.WriteString(content)
172180
if err != nil {

cmd/import.go renamed to cli/cmd/import.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/spf13/cobra"
1010
"github.com/spf13/viper"
11+
1112
"github.com/topi314/gobin/v2/internal/cfg"
1213
)
1314

@@ -31,20 +32,20 @@ Will import the token for the document jis74978 and server https://xgob.in`,
3132
return fmt.Errorf("document link/token is required")
3233
}
3334
var (
34-
server string
35-
documentID string
36-
token string
35+
gobinServer string
36+
documentID string
37+
token string
3738
)
3839
if uri, err := url.Parse(args[0]); err == nil {
39-
server = uri.Scheme + "://" + uri.Host
40+
gobinServer = uri.Scheme + "://" + uri.Host
4041
documentID = strings.SplitN(uri.Path, "/", 2)[0]
4142
token = uri.Query().Get("token")
4243
} else {
4344
token = args[0]
4445
documentID = viper.GetString("document")
45-
server = viper.GetString("server")
46+
gobinServer = viper.GetString("server")
4647
}
47-
if server == "" {
48+
if gobinServer == "" {
4849
return fmt.Errorf("server is required")
4950
}
5051
if documentID == "" {

cmd/post.go renamed to cli/cmd/post.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
"github.com/spf13/cobra"
1616
"github.com/spf13/viper"
1717
"github.com/topi314/chroma/v2/lexers"
18-
"github.com/topi314/gobin/v2/gobin"
18+
1919
"github.com/topi314/gobin/v2/internal/cfg"
2020
"github.com/topi314/gobin/v2/internal/ezhttp"
21+
"github.com/topi314/gobin/v2/server"
2122
)
2223

2324
func NewPostCmd(parent *cobra.Command) {
@@ -172,7 +173,7 @@ Will post "hello world!" to the server`,
172173
}
173174
defer rs.Body.Close()
174175

175-
var documentRs gobin.DocumentResponse
176+
var documentRs server.DocumentResponse
176177
if err = ezhttp.ProcessBody("post document", rs, &documentRs); err != nil {
177178
return fmt.Errorf("failed to process response: %w", err)
178179
}

cmd/rm.go renamed to cli/cmd/rm.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
"github.com/spf13/cobra"
88
"github.com/spf13/viper"
9-
"github.com/topi314/gobin/v2/gobin"
9+
1010
"github.com/topi314/gobin/v2/internal/cfg"
1111
"github.com/topi314/gobin/v2/internal/ezhttp"
12+
"github.com/topi314/gobin/v2/server"
1213
)
1314

1415
func NewRmCmd(parent *cobra.Command) {
@@ -57,14 +58,14 @@ Will delete the jis74978 from the server.`,
5758
defer rs.Body.Close()
5859

5960
if rs.StatusCode != 200 && rs.StatusCode != 204 {
60-
var errRs gobin.ErrorResponse
61+
var errRs server.ErrorResponse
6162
if err = json.NewDecoder(rs.Body).Decode(&errRs); err != nil {
6263
return fmt.Errorf("failed to decode error response: %w", err)
6364
}
6465
return fmt.Errorf("failed to remove document: %s", errRs.Message)
6566
}
6667

67-
var deleteRs gobin.DeleteResponse
68+
var deleteRs server.DeleteResponse
6869
if version != "" {
6970
if err = json.NewDecoder(rs.Body).Decode(&deleteRs); err != nil {
7071
return fmt.Errorf("failed to decode delete response: %w", err)

0 commit comments

Comments
 (0)