Skip to content

Commit

Permalink
Merge pull request #52 from fabiante/feat/refactor-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiante authored Sep 18, 2023
2 parents ce20735 + 8a7056a commit 3370c48
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 37 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ EXPOSE 8060
ENV GIN_MODE=release

ENTRYPOINT ["/persurl"]
CMD ["run"]
5 changes: 5 additions & 0 deletions cli/cmds/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cmds

import "github.com/spf13/cobra"

var Root = &cobra.Command{}
55 changes: 55 additions & 0 deletions cli/cmds/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmds

import (
"fmt"
"log"
"os"

"github.com/fabiante/persurl/api"
"github.com/fabiante/persurl/db"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
)

func init() {
cmd := &cobra.Command{
Use: "run",
Short: "Run the application",
}

cmd.Run = func(cmd *cobra.Command, args []string) {
dataDir := envDataDir()

dbFile := envDbFile(dataDir)

_, database, err := db.SetupAndMigrateDB(dbFile)
if err != nil {
log.Fatalf("setting up database failed: %s", err)
}

engine := gin.Default()
service := db.NewDatabase(database)
server := api.NewServer(service)
api.SetupRouting(engine, server)
if err := engine.Run(":8060"); err != nil {
log.Fatalf("running api failed: %s", err)
}
}

Root.AddCommand(cmd)
}

func envDataDir() string {
dataDir := os.Getenv("PERSURL_DATA_DIR")
if dataDir == "" {
dataDir = "."
}
log.Printf("using data dir: %s", dataDir)
return dataDir
}

func envDbFile(dataDir string) string {
dbFile := fmt.Sprintf("%s/prod.sqlite", dataDir)
log.Printf("using database file: %s", dbFile)
return dbFile
}
40 changes: 3 additions & 37 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,13 @@
package main

import (
"fmt"
"log"
"os"

"github.com/fabiante/persurl/api"
"github.com/fabiante/persurl/db"
"github.com/gin-gonic/gin"
"github.com/fabiante/persurl/cli/cmds"
)

// main is a crude CLI entrypoint for the application. Will be replaced
// with a proper CLI supporting multiple commands later.
func main() {
dataDir := envDataDir()

dbFile := envDbFile(dataDir)

_, database, err := db.SetupAndMigrateDB(dbFile)
if err != nil {
log.Fatalf("setting up database failed: %s", err)
if err := cmds.Root.Execute(); err != nil {
log.Fatal(err)
}

engine := gin.Default()
service := db.NewDatabase(database)
server := api.NewServer(service)
api.SetupRouting(engine, server)
if err := engine.Run(":8060"); err != nil {
log.Fatalf("running api failed: %s", err)
}
}

func envDataDir() string {
dataDir := os.Getenv("PERSURL_DATA_DIR")
if dataDir == "" {
dataDir = "."
}
log.Printf("using data dir: %s", dataDir)
return dataDir
}

func envDbFile(dataDir string) string {
dbFile := fmt.Sprintf("%s/prod.sqlite", dataDir)
log.Printf("using database file: %s", dbFile)
return dbFile
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/doug-martin/goqu/v9 v9.18.0
github.com/gin-gonic/gin v1.9.1
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.3
modernc.org/sqlite v1.25.0
)
Expand All @@ -21,6 +22,7 @@ require (
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
Expand All @@ -31,6 +33,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZX
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/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=
Expand Down Expand Up @@ -41,6 +42,8 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbu
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down Expand Up @@ -69,6 +72,11 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand Down

0 comments on commit 3370c48

Please sign in to comment.