Search⌘ K
AI Features

Updating the Application to Use the SQLite Repository

Explore how to update a Go command-line Pomodoro application to use an SQLite repository for storing interval data persistently. Learn to modify configuration flags, integrate the SQLite repository, and verify database operations using command-line tools and queries.

We'll cover the following...

Once the SQLite repository is available, we need to update the Pomodoro application to use it.

Updating the root.go file

We update the file root.go to add a new command-line parameter that lets the user specify the database file to use. We bind that flag with viper so the user can set it in the configuration file as well:

Go (1.6.2)
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
"config file (default is $HOME/.pomo.yaml)")
rootCmd.Flags().StringP("db", "d", "pomo.db", "Database file")
rootCmd.Flags().DurationP("pomo", "p", 10*time.Minute,
"Pomodoro duration")
rootCmd.Flags().DurationP("short", "s", 5*time.Minute,
"Short break duration")
rootCmd.Flags().DurationP("long", "l", 5*time.Minute,
"Long break duration")
viper.BindPFlag("db", rootCmd.Flags().Lookup("db"))
viper.BindPFlag("pomo", rootCmd.Flags().Lookup("pomo"))
viper.BindPFlag("short", rootCmd.Flags().Lookup("short"))
viper.BindPFlag("long", rootCmd.Flags().Lookup("long"))
}
Creating the init() function

We create a new file called reposqlite.go, which will ...