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" "github.com/spf13/cobra"
) )
type Feed struct {
ID int
URL string
}
// addCmd represents the add command // addCmd represents the add command
var addCmd = &cobra.Command{ var addCmd = &cobra.Command{
Use: "add", Use: "add",
Short: "A brief description of your command", Args: cobra.MinimumNArgs(1),
Long: `A longer description that spans multiple lines and likely contains examples Short: "Adds a new feed to the application.",
and usage of using your command. For example: 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. Supported types currently are:
This application is a tool to generate the needed files - RSS
to quickly create a Cobra application.`, - Youtube Channel links`,
Run: func(cmd *cobra.Command, args []string) { 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 package cmd
import ( import (
"database/sql"
"fmt" "fmt"
"freed/internal/database"
"os" "os"
"path/filepath" "path/filepath"
@ -30,12 +32,18 @@ type DatabaseConfig struct {
} }
type Config 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", Use: "freed",
Short: "A f[r]eed aggregator with an intentionally reduced discovery mechanism.", Short: "A f[r]eed aggregator with an intentionally reduced discovery mechanism.",
Long: `A f[r]eed aggregator with an intentionally reduced discovery mechanism. Long: `A f[r]eed aggregator with an intentionally reduced discovery mechanism.
@ -46,7 +54,22 @@ 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. 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() { func Execute() {
err := rootCmd.Execute() err := rootCmd.Execute()
@ -58,11 +81,9 @@ func Execute() {
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
// rootCmd.SetHelpFunc(internal.HelpFunc)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)")
} }
// initConfig reads in config file and ENV variables if set.
func initConfig() { func initConfig() {
if cfgFile != "" { if cfgFile != "" {
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
@ -87,9 +108,9 @@ func initConfig() {
os.Exit(1) 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) fmt.Printf("Could not read config file: %s", err)
os.Exit(1) os.Exit(1)
} }