-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: config flag sets config file location (#11)
- Loading branch information
1 parent
828deda
commit a8e895b
Showing
11 changed files
with
203 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ out/ | |
temp/ | ||
dist/ | ||
.task/ | ||
athenaeum | ||
/athenaeum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/CallumKerson/Athenaeum/internal/adapters/bolt" | ||
"github.com/CallumKerson/Athenaeum/internal/adapters/logrus" | ||
audiobooksService "github.com/CallumKerson/Athenaeum/internal/audiobooks/service" | ||
mediaService "github.com/CallumKerson/Athenaeum/internal/media/service" | ||
podcastService "github.com/CallumKerson/Athenaeum/internal/podcasts/service" | ||
transportHttp "github.com/CallumKerson/Athenaeum/internal/transport/http" | ||
) | ||
|
||
const ( | ||
shortHelp = "an audiobook server that provides a podcast feed" | ||
) | ||
|
||
func main() { | ||
cmd := NewRootCommand() | ||
if err := cmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// Build the cobra command that handles our command line tool. | ||
func NewRootCommand() *cobra.Command { | ||
pathToConfig := "" | ||
var cfg Config | ||
|
||
// Define our command | ||
rootCmd := &cobra.Command{ | ||
Use: "athenaeum", | ||
Short: shortHelp, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return InitConfig(&cfg, pathToConfig, cmd.OutOrStderr()) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
logger := logrus.NewLogger() | ||
setLogLevel(logger, cfg.GetLogLevel()) | ||
mediaSvc := mediaService.New(logger, cfg.GetMediaServiceOpts()...) | ||
boltAudiobookStore, err := bolt.NewAudiobookStore(logger, true, cfg.GetBoltDBOps()...) | ||
if err != nil { | ||
return err | ||
} | ||
audiobookSvc := audiobooksService.New(mediaSvc, boltAudiobookStore, logger) | ||
if errScan := audiobookSvc.UpdateAudiobooks(context.Background()); errScan != nil { | ||
return errScan | ||
} | ||
podcastSvc := podcastService.New(audiobookSvc, logger, cfg.GetPodcastServiceOpts()...) | ||
httpHandler := transportHttp.NewHandler(podcastSvc, audiobookSvc, logger, cfg.GetHTTPHandlerOpts()...) | ||
|
||
return transportHttp.Serve(httpHandler, cfg.Port, logger) | ||
}, | ||
} | ||
|
||
// Define cobra flags, the default value has the lowest (least significant) precedence | ||
rootCmd.PersistentFlags().StringVarP(&pathToConfig, "config", "c", pathToConfig, "path to config file") | ||
rootCmd.SilenceUsage = true | ||
|
||
rootCmd.Version = Version | ||
|
||
rootCmd.AddCommand(NewVersionCommand()) | ||
return rootCmd | ||
} | ||
|
||
func setLogLevel(logger *logrus.Logger, level string) { | ||
level = strings.ToLower(level) | ||
switch level { | ||
case "debug": | ||
logger.SetLevelDebug() | ||
case "warn": | ||
logger.SetLevelWarn() | ||
case "error": | ||
logger.SetLevelError() | ||
default: | ||
logger.SetLevelInfo() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/CallumKerson/Athenaeum/internal/adapters/bolt" | ||
mediaService "github.com/CallumKerson/Athenaeum/internal/media/service" | ||
podcastService "github.com/CallumKerson/Athenaeum/internal/podcasts/service" | ||
) | ||
|
||
func (c *Config) GetMediaHost() string { | ||
return fmt.Sprintf("%s/%s", c.Host, c.Media.HostPath) | ||
} | ||
|
||
func (c *Config) GetMediaServiceOpts() []mediaService.Option { | ||
return []mediaService.Option{mediaService.WithPathToMediaRoot(c.Media.Root)} | ||
} | ||
|
||
func (c *Config) GetBoltDBOps() []bolt.Option { | ||
return []bolt.Option{bolt.WithDBDefaults(), bolt.WithPathToDBDirectory(c.DB.Root)} | ||
} | ||
|
||
func (c *Config) GetPodcastServiceOpts() []podcastService.Option { | ||
return []podcastService.Option{podcastService.WithHost(c.Host), | ||
podcastService.WithMediaPath(c.Media.HostPath), | ||
podcastService.WithPodcastFeedInfo(c.Podcast.Explicit, c.Podcast.Language, c.Podcast.Author, c.Podcast.Email, c.Podcast.Copyright)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.