initial commit

This commit is contained in:
surtur 2022-08-17 17:57:17 +02:00
commit 31310b108e
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
3 changed files with 81 additions and 0 deletions

5
go.mod Normal file

@ -0,0 +1,5 @@
module git.dotya.ml/wanderer/drone-sqlite-to-s3
go 1.19
require github.com/mattn/go-sqlite3 v1.14.15

2
go.sum Normal file

@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

74
main.go Normal file

@ -0,0 +1,74 @@
package main
import (
"database/sql"
"fmt"
"log"
"os"
"path"
"time"
_ "github.com/mattn/go-sqlite3"
)
const (
droneDir = "/var/lib/drone"
dbFile = "database.sqlite"
logDir = "dronelogs/logs"
)
func main() {
log.Println("starting...")
if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
log.Fatal(err)
}
filename := path.Join(droneDir, dbFile)
start := time.Now()
log.Printf("logs dir: %s\n", logDir)
log.Printf("opening db file: %s\n", filename)
db, err := sql.Open("sqlite3", "file:"+filename+"?mode:ro")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT * FROM logs")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var id int
var row string
for rows.Next() {
err = rows.Scan(&id, &row)
if err != nil {
log.Fatal(err)
}
log.Printf("handling row id: %d\n", id)
f, err := os.Create(path.Join(logDir, fmt.Sprint(id)))
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.Println("saving row to a file")
_, err = f.Write([]byte(row))
}
elapsed := time.Since(start)
log.Printf("took: %s", elapsed)
}