Implements database connection with config data

This commit is contained in:
Dennis Schoepf 2024-08-31 01:42:31 +02:00
parent 0f54c090bb
commit ad4bb08eb2
2 changed files with 44 additions and 18 deletions

View file

@ -22,18 +22,23 @@ import (
"github.com/spf13/cobra"
)
type Feed struct {
ID int
URL string
}
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Args: cobra.MinimumNArgs(1),
Short: "Adds a new feed to the application.",
Long: `Validates and stores a feed in the application's database. Depending on the feed type, articles, videos, or updates are fetched right away.
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Supported types currently are:
- RSS
- Youtube Channel links`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("add called")
fmt.Println("Not implemented. Feed will be added here")
},
}

View file

@ -17,7 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"database/sql"
"fmt"
"freed/internal/database"
"os"
"path/filepath"
@ -30,12 +32,18 @@ type DatabaseConfig struct {
}
type Config struct {
Db DatabaseConfig `mapstructure:"database"`
DB DatabaseConfig `mapstructure:"database"`
}
var cfgFile string
type AppContext struct {
Config Config
DB *sql.DB
}
var rootCmd = &cobra.Command{
var (
appContext = &AppContext{}
cfgFile string
rootCmd = &cobra.Command{
Use: "freed",
Short: "A f[r]eed aggregator with an intentionally reduced discovery mechanism.",
Long: `A f[r]eed aggregator with an intentionally reduced discovery mechanism.
@ -46,8 +54,23 @@ Where f[r]eed is different to other feed aggregators or bookmarking services is
The application also includes a set of recommended "Small Web" and topic-specific curated feeds. These are also available through the configuration.
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(appContext.Config.DB.Path)
if err != nil {
fmt.Printf("Could not connect to the database: %s", err)
}
appContext.DB = db
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if appContext.DB != nil {
appContext.DB.Close()
}
},
}
)
func Execute() {
err := rootCmd.Execute()
if err != nil {
@ -58,11 +81,9 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
// rootCmd.SetHelpFunc(internal.HelpFunc)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
@ -87,9 +108,9 @@ func initConfig() {
os.Exit(1)
}
var c Config
viper.SetDefault("database.path", "freed.sqlite3")
if err := viper.Unmarshal(&c); err != nil {
if err := viper.Unmarshal(&appContext.Config); err != nil {
fmt.Printf("Could not read config file: %s", err)
os.Exit(1)
}