From 840b84b1206826b5595d0a82dd4dccdd430a1f84 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Wed, 31 Jul 2024 21:34:41 +0000 Subject: [PATCH] Adding Cobra CLI to project (#2) I would like to have a composable cli tool with subcommands for management of the Flux Feed application. This PR restructures the cmd package according to conventions outlined in the Cobra user guide https://github.com/spf13/cobra/blob/main/site/content/user_guide.md At present only the database migrate command is implemented, and with minimal functionality. Future work will have to add additional flags and information on how to declare the location of the database to be migrated. Reviewed-on: https://git.runcible.io/androiddrew/flux-feed/pulls/2 --- .gitignore | 1 + Makefile | 4 ++-- README.md | 22 +++++++++++++++++++++- cmd/{migrate/main.go => migrate.go} | 18 +++++++++++++----- cmd/root.go | 26 ++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 9 +++++++++ main.go | 10 ++++++++++ 8 files changed, 85 insertions(+), 8 deletions(-) rename cmd/{migrate/main.go => migrate.go} (61%) create mode 100644 cmd/root.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore index 2ac73c2..b6f4702 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,6 @@ go.work # Flux Feed fluxfeed flux-feed +fluxctl .env flux-local.db \ No newline at end of file diff --git a/Makefile b/Makefile index 780ef80..4a7d8d1 100644 --- a/Makefile +++ b/Makefile @@ -15,11 +15,11 @@ vet: fmt .PHONY:vet build: vet - go build -o ./fluxfeed . + go build -o ./fluxctl . .PHONY:build clean: - rm -rf ./fluxfeed + rm -rf ./fluxctl .PHONY:clean docs: diff --git a/README.md b/README.md index 7c1fe09..b1646db 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,25 @@ # Flux Feed ![build-status](https://drone.runcible.io/api/badges/androiddrew/flux-feed/status.svg) +[![](https://img.shields.io/badge/License-AGPL-blue.svg)](https://opensource.org/licenses/AGPL "License: AGPL") -An Indie Reader for the modern day. \ No newline at end of file +An Indie Reader for the modern day. + + +## Building + +From the root of the source tree run: + +```shell +make build +``` + +## Using + +```shell +./fluxctl --help +``` + +## License + +is project is licensed under the MIT License. See the [LICENSE file](https://git.runcible.io/androiddrew/flux-feed/src/branch/main/LICENSE) for the full license text. \ No newline at end of file diff --git a/cmd/migrate/main.go b/cmd/migrate.go similarity index 61% rename from cmd/migrate/main.go rename to cmd/migrate.go index e26b0d6..8bb7193 100644 --- a/cmd/migrate/main.go +++ b/cmd/migrate.go @@ -1,5 +1,5 @@ -// Entry point for applying database migrations for flux-feed application -package main +// Package cmd migrate provides a database migration command to root cli +package cmd import ( "database/sql" @@ -8,11 +8,12 @@ import ( "git.runcible.io/androiddrew/flux-feed/config" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/sqlite3" - _ "github.com/golang-migrate/migrate/v4/source/file" - _ "github.com/mattn/go-sqlite3" + _ "github.com/golang-migrate/migrate/v4/source/file" //migrations + _ "github.com/mattn/go-sqlite3" //migrations + "github.com/spf13/cobra" ) -func main() { +func runMigrate(_ *cobra.Command, _ []string) { cfg := config.New() db, err := sql.Open(cfg.DatabaseDriver, cfg.DatabaseDSN) defer db.Close() @@ -38,3 +39,10 @@ func main() { log.Fatal(err) } } + +// MigrateCmd provides database migrations command for cli +var MigrateCmd = &cobra.Command{ + Use: "migrate", + Short: "Run database migrations", + Run: runMigrate, +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..9be1437 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,26 @@ +// Package cmd provides cli commands +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "fluxctl", + Short: "A flexible indie reader for the modern day", +} + +// Execute executes the root command. +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func init() { + rootCmd.AddCommand(MigrateCmd) +} diff --git a/go.mod b/go.mod index 9a74ba8..80cb52a 100644 --- a/go.mod +++ b/go.mod @@ -13,5 +13,8 @@ require ( require ( github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.8.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect go.uber.org/atomic v1.7.0 // indirect ) diff --git a/go.sum b/go.sum index 73dd23f..5153210 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -12,6 +13,8 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= @@ -24,11 +27,17 @@ github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= 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/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..9bf1287 --- /dev/null +++ b/main.go @@ -0,0 +1,10 @@ +// Flux Feed main entrypoint +package main + +import ( + "git.runcible.io/androiddrew/flux-feed/cmd" +) + +func main() { + cmd.Execute() +}