Compare commits

..

4 Commits

Author SHA1 Message Date
07968a3a46 Add ci/cd workflow to validate and test commits
All checks were successful
Validate the build / validate-build (push) Successful in 2m11s
2025-05-28 00:57:46 +02:00
f7c72626ee Use cobra to turn linkctl into a proper cli
Most commands are currently placeholders but version and db init work
2025-05-09 03:37:52 +02:00
e66d800881 Add version package and update the makefile
The makefile will grab the version info from git and pass it to the
linker so that version information based on tags, commits and commit
times is available in the code.
2025-05-09 03:37:49 +02:00
9acc9a03aa Add basic database interaction 2025-05-09 03:36:29 +02:00
7 changed files with 287 additions and 14 deletions

View File

@@ -0,0 +1,40 @@
name: Validate the build
run-name: ${{ gitea.actor }} is validating
on: [push]
jobs:
validate-build:
runs-on: ubuntu-latest
container:
image: node:current-alpine
steps:
- name: Install dependencies
run: |
echo "https://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
apk update
apk add --no-cache git make bash go
GOBIN=/usr/local/bin go install mvdan.cc/gofumpt@latest
export "PATH=$PATH:/root/go/bin"
echo "---------------------"
echo "Go version:"
go version
echo "---------------------"
- name: Check out repository code
uses: actions/checkout@v4
- name: Fetch dependencies
run: |
go mod download
- name: Validate the code and formatting
run: |
make validate
- name: Run tests
run: |
make test

27
cmd/linkctl/config.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"fmt"
"github.com/spf13/cobra"
)
func configPreRun(cmd *cobra.Command, args []string) error {
return setupDb()
}
func configPostRun(cmd *cobra.Command, args []string) error {
return cleanupDb()
}
func configSetHandler(cmd *cobra.Command, args []string) {
fmt.Println("Not implemented")
}
func configGetHandler(cmd *cobra.Command, args []string) {
fmt.Println("Not implemented")
}
func configListHandler(cmd *cobra.Command, args []string) {
fmt.Println("Not implemented")
}

48
cmd/linkctl/db.go Normal file
View File

@@ -0,0 +1,48 @@
package main
import (
"fmt"
"git.omicron.one/omicron/linkshare/internal/database"
"git.omicron.one/omicron/linkshare/internal/util"
"git.omicron.one/omicron/linkshare/internal/version"
"github.com/spf13/cobra"
)
func openDB() (*database.DB, error) {
paths, err := util.FindDirectories(dbPath)
if err != nil {
return nil, err
}
return database.Open(paths.DatabaseFile)
}
func dbPreRun(cmd *cobra.Command, args []string) error {
return setupDb()
}
func dbPostRun(cmd *cobra.Command, args []string) error {
return cleanupDb()
}
func dbInitHandler(cmd *cobra.Command, args []string) {
err := db.Initialize(paths.SchemaDir)
if err == database.ErrAlreadyInitialized {
fmt.Printf("Database %q is already initialized\n", dbPath)
return
}
if err == nil {
fmt.Printf("Initialized database %q with schema version %d\n", dbPath, version.SchemaVersion)
return
}
fmt.Printf("Failed to initialize database %q: %v\n", dbPath, err)
}
func dbBackupHandler(cmd *cobra.Command, args []string) {
fmt.Println("Not implemented")
}
func dbUpdateHandler(cmd *cobra.Command, args []string) {
fmt.Println("Not implemented")
}

View File

@@ -2,17 +2,156 @@ package main
import ( import (
"fmt" "fmt"
"os"
"git.omicron.one/omicron/linkshare/internal/database"
"git.omicron.one/omicron/linkshare/internal/util" "git.omicron.one/omicron/linkshare/internal/util"
"git.omicron.one/omicron/linkshare/internal/version"
"github.com/spf13/cobra"
) )
func main() { var (
paths, err := util.FindDirectories("") dbPath string
verbosity int
)
var (
paths *util.AppPaths
db *database.DB
)
func exitIfError(err error) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1)
}
}
func setupPaths() error {
if paths != nil {
return nil
}
paths_, err := util.FindDirectories(dbPath)
if err != nil {
return err
}
paths = paths_
return nil
}
func setupDb() error {
if db != nil {
return nil
}
err := setupPaths()
if err != nil {
return nil
} }
fmt.Println("Paths:") db_, err := database.Open(dbPath)
fmt.Println(" Schema:", paths.SchemaDir) if err != nil {
fmt.Println(" Database:", paths.DatabaseFile) return nil
}
db = db_
return nil
}
func cleanupDb() error {
if db != nil {
err := db.Close()
if err != nil {
return err
}
}
return nil
}
func main() {
rootCmd := &cobra.Command{
Use: "linkctl",
Short: "LinkShare CLI tool",
Long: `Command line tool to manage your self-hosted LinkShare service.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.PersistentFlags().StringVarP(&dbPath, "db", "d", "", "Database file path")
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbose", "v", "Increase verbosity level")
configCmd := &cobra.Command{
Use: "config",
Short: "Configuration commands",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
PersistentPreRunE: configPreRun,
PersistentPostRunE: configPostRun,
}
configSetCmd := &cobra.Command{
Use: "set",
Short: "Set a configuration value",
Run: configSetHandler,
}
configGetCmd := &cobra.Command{
Use: "get",
Short: "Get a configuration value",
Run: configGetHandler,
}
configListCmd := &cobra.Command{
Use: "list",
Short: "List all configuration values",
Run: configListHandler,
}
configCmd.AddCommand(configSetCmd, configGetCmd, configListCmd)
dbCmd := &cobra.Command{
Use: "db",
Short: "Database commands",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
PersistentPreRunE: dbPreRun,
PersistentPostRunE: dbPostRun,
}
dbInitCmd := &cobra.Command{
Use: "init",
Short: "Initialize the database",
Run: dbInitHandler,
}
dbBackupCmd := &cobra.Command{
Use: "backup",
Short: "Backup the database",
Run: dbBackupHandler,
}
dbUpdateCmd := &cobra.Command{
Use: "update",
Short: "Update the database schema",
Run: dbUpdateHandler,
}
dbCmd.AddCommand(dbInitCmd, dbBackupCmd, dbUpdateCmd)
versionCmd := &cobra.Command{
Use: "version",
Short: "Display version information",
Run: func(cmd *cobra.Command, args []string) {
version.Print()
},
}
rootCmd.AddCommand(configCmd, dbCmd, versionCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} }

10
go.mod
View File

@@ -2,4 +2,12 @@ module git.omicron.one/omicron/linkshare
go 1.24 go 1.24
require github.com/mattn/go-sqlite3 v1.14.28 require (
github.com/mattn/go-sqlite3 v1.14.28
github.com/spf13/cobra v1.9.1
)
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
)

10
go.sum
View File

@@ -1,2 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -24,10 +24,11 @@ type DB struct {
} }
var ( var (
ErrDatabaseNotInitialized = errors.New("database not initialized") ErrNotInitialized = errors.New("database not initialized")
ErrDatabaseSchemaOutdated = errors.New("database schema needs updating") ErrAlreadyInitialized = errors.New("database already initialized")
ErrDatabaseSchemaUnsupported = errors.New("database schema is too new for the server") ErrSchemaOutdated = errors.New("database schema needs updating")
ErrMigrationFailed = errors.New("migration failed") ErrSchemaUnsupported = errors.New("database schema is too new for the server")
ErrMigrationFailed = errors.New("migration failed")
) )
// Open opens a connection to the sqlite database at the given path // Open opens a connection to the sqlite database at the given path
@@ -65,7 +66,7 @@ func (db *DB) Close() error {
func (db *DB) Initialize(schemaPath string) error { func (db *DB) Initialize(schemaPath string) error {
err := db.CheckInitialized() err := db.CheckInitialized()
if err == nil { if err == nil {
return nil return ErrAlreadyInitialized
} }
currentSchema := filepath.Join(schemaPath, "current.sql") currentSchema := filepath.Join(schemaPath, "current.sql")
@@ -91,7 +92,7 @@ func (db *DB) CheckInitialized() error {
} }
if count == 0 { if count == 0 {
return ErrDatabaseNotInitialized return ErrNotInitialized
} }
return nil return nil
} }
@@ -127,9 +128,9 @@ func (db *DB) CheckSchemaVersion() error {
return err return err
} }
if version_ < version.SchemaVersion { if version_ < version.SchemaVersion {
return ErrDatabaseSchemaOutdated return ErrSchemaOutdated
} else if version_ > version.SchemaVersion { } else if version_ > version.SchemaVersion {
return ErrDatabaseSchemaUnsupported return ErrSchemaUnsupported
} }
return nil return nil
} }