Skip to content

Commit

Permalink
feat: add CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent cef0dc0 commit 5b74bf6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 16 deletions.
22 changes: 22 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "spoty",
Short: "A simple HTTP REST API microservice for Spotify",
Long: "Spoty provides simple REST API endpoints to query the current playing track on Spotify.",
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
return rootCmd.Execute()
}
37 changes: 24 additions & 13 deletions bootstrap/bootstrap.go → cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bootstrap
package cmd

import (
"context"
Expand All @@ -10,22 +10,29 @@ import (
"github.com/JulesMike/spoty/http"
"github.com/JulesMike/spoty/logger"
"github.com/JulesMike/spoty/spoty"
"github.com/spf13/cobra"
"go.uber.org/fx"
)

// Module exported for initialising application.
var Module = fx.Options(
build.Module,
config.Module,
logger.Module,
cache.Module,
health.Module,
http.Module,
spoty.Module,
fx.Invoke(bootstrap),
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start the HTTP server",
Run: func(cmd *cobra.Command, args []string) {
fx.New(
build.Module,
config.Module,
logger.Module,
cache.Module,
health.Module,
http.Module,
spoty.Module,
fx.Invoke(serve),
).Run()
},
}

func bootstrap(lc fx.Lifecycle, s *http.Server) error {
func serve(lc fx.Lifecycle, s *http.Server) error {
s.RegisterRoutes()

lc.Append(fx.Hook{
Expand All @@ -41,3 +48,7 @@ func bootstrap(lc fx.Lifecycle, s *http.Server) error {

return nil
}

func init() {
rootCmd.AddCommand(serveCmd)
}
37 changes: 37 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"os"

"github.com/JulesMike/spoty/build"
"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/logger"
"github.com/spf13/cobra"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Return the build information",
Run: func(cmd *cobra.Command, args []string) {
logger, err := logger.New(&config.Config{})
if err != nil {
fmt.Printf("failed to start logger: %v", err)
os.Exit(1) //nolint:revive
}

info, err := build.New(logger)
if err != nil {
logger.Fatal(err)
}

fmt.Printf("Revision: %v\n", info.Revision)
fmt.Printf("Last Commit: %v\n", info.LastCommit)
fmt.Printf("Dirty Build: %v\n", info.DirtyBuild)
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/json-iterator/go v1.1.10
github.com/kelseyhightower/envconfig v1.4.0
github.com/magefile/mage v1.13.0
github.com/spf13/cobra v1.4.0
github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.8.0
github.com/zmb3/spotify v1.3.0
Expand All @@ -37,6 +38,7 @@ require (
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/protobuf v1.5.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -45,6 +47,7 @@ require (
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.2.4 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/dig v1.14.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ github.com/cenkalti/dominantcolor v0.0.0-20171020061837-df772e8dd39e h1:5KS7rBZB
github.com/cenkalti/dominantcolor v0.0.0-20171020061837-df772e8dd39e/go.mod h1:Bu/hFErJeI1/nnRDAnOiGytVqsnIgUd2s0b5ZKDPYws=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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=
Expand Down Expand Up @@ -80,6 +81,8 @@ github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHL
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -126,6 +129,11 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"github.com/JulesMike/spoty/bootstrap"
"go.uber.org/fx"
"fmt"
"os"

"github.com/JulesMike/spoty/cmd"
)

// @title Spoty API
Expand All @@ -15,5 +17,8 @@ import (
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

func main() {
fx.New(bootstrap.Module).Run()
if err := cmd.Execute(); err != nil {
fmt.Printf("failed to execute cmd: %v", err)
os.Exit(1)
}
}

0 comments on commit 5b74bf6

Please sign in to comment.