diff options
author | EuAndreh <eu@euandre.org> | 2024-09-02 11:03:10 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-09-12 12:06:36 -0300 |
commit | 72a43414fe6b6451d0587ad29d65129b4782a3b7 (patch) | |
tree | c795625415a0cc5b0f1a30a95b4bf465bd92b866 | |
parent | Initial empty commit (diff) | |
download | fiinha-72a43414fe6b6451d0587ad29d65129b4782a3b7.tar.gz fiinha-72a43414fe6b6451d0587ad29d65129b4782a3b7.tar.xz |
Init Go project skeleton with golite init
-rw-r--r-- | .gitignore | 7 | ||||
-rw-r--r-- | Makefile | 148 | ||||
-rw-r--r-- | deps.mk | 0 | ||||
-rwxr-xr-x | mkdeps.sh | 4 | ||||
-rw-r--r-- | src/liteq.go | 117 | ||||
-rw-r--r-- | src/main.go | 7 | ||||
-rwxr-xr-x | tests/cli-opts.sh | 4 | ||||
-rwxr-xr-x | tests/integration.sh | 4 | ||||
-rw-r--r-- | tests/liteq.go | 35 | ||||
-rw-r--r-- | tests/main.go | 7 |
10 files changed, 333 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c096254 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/src/version.go +/*.bin +/*.db +/src/*.a +/src/*.bin +/tests/*.a +/tests/*.bin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2145e48 --- /dev/null +++ b/Makefile @@ -0,0 +1,148 @@ +.POSIX: +DATE = 1970-01-01 +VERSION = 0.1.0 +NAME = liteq +NAME_UC = $(NAME) +LANGUAGES = en +## Installation prefix. Defaults to "/usr". +PREFIX = /usr +BINDIR = $(PREFIX)/bin +LIBDIR = $(PREFIX)/lib +GOLIBDIR = $(LIBDIR)/go +INCLUDEDIR = $(PREFIX)/include +SRCDIR = $(PREFIX)/src/$(NAME) +SHAREDIR = $(PREFIX)/share +LOCALEDIR = $(SHAREDIR)/locale +MANDIR = $(SHAREDIR)/man +EXEC = ./ +## Where to store the installation. Empty by default. +DESTDIR = +LDLIBS = -lsqlite3 +GOCFLAGS = -I $(GOLIBDIR) +GOLDFLAGS = -L $(GOLIBDIR) + + + +.SUFFIXES: +.SUFFIXES: .go .a .bin .bin-check + + + +all: +include deps.mk + + +objects = \ + src/$(NAME).a \ + src/main.a \ + tests/$(NAME).a \ + tests/main.a \ + +sources = \ + src/$(NAME).go \ + src/version.go \ + src/main.go \ + + +derived-assets = \ + src/version.go \ + $(objects) \ + src/main.bin \ + tests/main.bin \ + $(NAME).bin \ + +side-assets = \ + liteq.db \ + + + +## Default target. Builds all artifacts required for testing +## and installation. +all: $(derived-assets) + + +$(objects): Makefile + +src/$(NAME).a: src/$(NAME).go src/version.go + go tool compile $(GOCFLAGS) -o $@ -p $(*F) -I $(@D) $*.go src/version.go + +src/main.a: src/main.go src/$(NAME).a +tests/main.a: tests/main.go tests/$(NAME).a +src/main.a tests/main.a: + go tool compile $(GOCFLAGS) -o $@ -p $(*F) -I $(@D) $*.go + +tests/$(NAME).a: tests/$(NAME).go src/$(NAME).go src/version.go + go tool compile $(GOCFLAGS) -o $@ -p $(*F) $*.go src/$(*F).go src/version.go + +src/main.bin: src/main.a +tests/main.bin: tests/main.a +src/main.bin tests/main.bin: + go tool link $(GOLDFLAGS) -o $@ -L $(@D) --extldflags '$(LDLIBS)' $*.a + +$(NAME).bin: src/main.bin + ln -fs $? $@ + +src/version.go: Makefile + echo 'package $(NAME); var version = "$(VERSION)"' > $@ + + + +tests.bin-check = \ + tests/main.bin-check \ + +tests/main.bin-check: tests/main.bin +$(tests.bin-check): + $(EXEC)$*.bin + +check-unit: $(tests.bin-check) + + +integration-tests = \ + tests/cli-opts.sh \ + tests/integration.sh \ + +.PRECIOUS: $(integration-tests) +$(integration-tests): $(NAME).bin +$(integration-tests): ALWAYS + sh $@ + +check-integration: $(integration-tests) + + +## Run all tests. Each test suite is isolated, so that a parallel +## build can run tests at the same time. The required artifacts +## are created if missing. +check: check-unit check-integration + + + +## Remove *all* derived artifacts produced during the build. +## A dedicated test asserts that this is always true. +clean: + rm -rf $(derived-assets) $(side-assets) + + +## Installs into $(DESTDIR)$(PREFIX). Its dependency target +## ensures that all installable artifacts are crafted beforehand. +install: all + mkdir -p \ + '$(DESTDIR)$(BINDIR)' \ + '$(DESTDIR)$(GOLIBDIR)' \ + '$(DESTDIR)$(SRCDIR)' \ + + cp $(NAME).bin '$(DESTDIR)$(BINDIR)'/$(NAME) + cp src/$(NAME).a '$(DESTDIR)$(GOLIBDIR)' + cp $(sources) '$(DESTDIR)$(SRCDIR)' + +## Uninstalls from $(DESTDIR)$(PREFIX). This is a perfect mirror +## of the "install" target, and removes *all* that was installed. +## A dedicated test asserts that this is always true. +uninstall: + rm -rf \ + '$(DESTDIR)$(BINDIR)'/$(NAME) \ + '$(DESTDIR)$(GOLIBDIR)'/$(NAME).a \ + '$(DESTDIR)$(SRCDIR)' \ + + + +ALWAYS: diff --git a/mkdeps.sh b/mkdeps.sh new file mode 100755 index 0000000..e5606ff --- /dev/null +++ b/mkdeps.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu + +export LANG=POSIX.UTF-8 diff --git a/src/liteq.go b/src/liteq.go new file mode 100644 index 0000000..2eeff34 --- /dev/null +++ b/src/liteq.go @@ -0,0 +1,117 @@ +package liteq + +import ( + "database/sql" + "flag" + "io/ioutil" + "log/slog" + "os" + "sort" + + g "gobang" + "golite" +) + + + +func InitMigrations(db *sql.DB) { + _, err := db.Exec(` + CREATE TABLE IF NOT EXISTS migrations ( + filename TEXT PRIMARY KEY + ); + `) + g.FatalIf(err) +} + +const MIGRATIONS_DIR = "src/sql/migrations/" +func PendingMigrations(db *sql.DB) []string { + files, err := ioutil.ReadDir(MIGRATIONS_DIR) + g.FatalIf(err) + + set := make(map[string]bool) + for _, file := range files { + set[file.Name()] = true + } + + rows, err := db.Query(`SELECT filename FROM migrations;`) + g.FatalIf(err) + defer rows.Close() + + for rows.Next() { + var filename string + err := rows.Scan(&filename) + g.FatalIf(err) + delete(set, filename) + } + g.FatalIf(rows.Err()) + + difference := make([]string, 0) + for filename := range set { + difference = append(difference, filename) + } + + sort.Sort(sort.StringSlice(difference)) + return difference +} + +func RunMigrations(db *sql.DB) { + InitMigrations(db) + + stmt, err := db.Prepare(`INSERT INTO migrations (filename) VALUES (?);`) + g.FatalIf(err) + defer stmt.Close() + + for _, filename := range PendingMigrations(db) { + g.Info("Running migration file", "exec-migration-file", + "filename", filename, + ) + + tx, err := db.Begin() + g.FatalIf(err) + + sql, err := os.ReadFile(MIGRATIONS_DIR + filename) + g.FatalIf(err) + + _, err = tx.Exec(string(sql)) + g.FatalIf(err) + + _, err = tx.Stmt(stmt).Exec(filename) + g.FatalIf(err) + + err = tx.Commit() + g.FatalIf(err) + } +} + +func initDB(databasePath string) *sql.DB { + db, err := sql.Open("sqlite3", databasePath) + g.FatalIf(err) + RunMigrations(db) + return db +} + +func run(db *sql.DB) { +} + + + +var ( + databasePath = flag.String( + "f", + "q.db", + "The path to the database file", + ) +) + + +func Main() { + g.Init(slog.Group( + "versions", + "gobang", g.Version, + "golite", golite.Version, + "this", version, + )) + flag.Parse() + db := initDB(*databasePath) + run(db) +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..8d9a05e --- /dev/null +++ b/src/main.go @@ -0,0 +1,7 @@ +package main + +import "liteq" + +func main() { + liteq.Main() +} diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh new file mode 100755 index 0000000..fcb62ca --- /dev/null +++ b/tests/cli-opts.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu + +exit diff --git a/tests/integration.sh b/tests/integration.sh new file mode 100755 index 0000000..fcb62ca --- /dev/null +++ b/tests/integration.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu + +exit diff --git a/tests/liteq.go b/tests/liteq.go new file mode 100644 index 0000000..86d1bef --- /dev/null +++ b/tests/liteq.go @@ -0,0 +1,35 @@ +package q + +import ( + "os" + "testing" + "testing/internal/testdeps" + + g "gobang" +) + + + +func TestX(t *testing.T) { + g.AssertEqual(1, 1) +} + + + +func MainTest() { + tests := []testing.InternalTest { + { "TestX", TestX }, + } + + benchmarks := []testing.InternalBenchmark {} + fuzzTargets := []testing.InternalFuzzTarget {} + examples := []testing.InternalExample {} + m := testing.MainStart( + testdeps.TestDeps {}, + tests, + benchmarks, + fuzzTargets, + examples, + ) + os.Exit(m.Run()) +} diff --git a/tests/main.go b/tests/main.go new file mode 100644 index 0000000..893132e --- /dev/null +++ b/tests/main.go @@ -0,0 +1,7 @@ +package main + +import "q" + +func main() { + q.MainTest() +} |