mirror of
https://github.com/Threnklyn/zeit.git
synced 2026-05-18 21:03:30 +02:00
Deduplicate initDatabase, move to initConfig in Rootcmd
This commit is contained in:
@@ -73,11 +73,4 @@ func init() {
|
||||
entryCmd.Flags().StringVarP(¬es, "notes", "n", "", "Update activity notes")
|
||||
entryCmd.Flags().StringVarP(&task, "task", "t", "", "Update activity task")
|
||||
entryCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,4 @@ var eraseCmd = &cobra.Command{
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(eraseCmd)
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,11 +102,4 @@ func init() {
|
||||
exportCmd.Flags().StringVar(&until, "until", "", "Date/time to export until")
|
||||
exportCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be exported")
|
||||
exportCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be exported")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,11 +104,4 @@ func init() {
|
||||
finishCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be assigned")
|
||||
finishCmd.Flags().StringVarP(¬es, "notes", "n", "", "Activity notes")
|
||||
finishCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be assigned")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,11 +108,4 @@ var importCmd = &cobra.Command{
|
||||
func init() {
|
||||
rootCmd.AddCommand(importCmd)
|
||||
importCmd.Flags().StringVar(&format, "format", "zeit", "Format to import, possible values: zeit, tyme")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,11 +115,4 @@ func init() {
|
||||
listCmd.Flags().BoolVar(&listOnlyProjectsAndTasks, "only-projects-and-tasks", false, "Only list projects and their tasks, no entries")
|
||||
listCmd.Flags().BoolVar(&listOnlyTasks, "only-tasks", false, "Only list tasks, no projects nor entries")
|
||||
listCmd.Flags().BoolVar(&appendProjectIDToTask, "append-project-id-to-task", false, "Append project ID to tasks in the list")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,11 +45,4 @@ var projectCmd = &cobra.Command{
|
||||
func init() {
|
||||
rootCmd.AddCommand(projectCmd)
|
||||
projectCmd.Flags().StringVarP(&projectColor, "color", "c", "", "Set the color of the project (hex code, e.g. #121212)")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
+35
-29
@@ -51,13 +51,13 @@ func Execute() {
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_CONFIG_HOME/zeit.[yaml|toml")
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_CONFIG_HOME/zeit.[yaml|toml")
|
||||
|
||||
rootCmd.PersistentFlags().BoolVar(&noColors, FlagNoColors, false, "Do not use colors in output")
|
||||
viper.BindPFlag(FlagNoColors, rootCmd.PersistentFlags().Lookup(FlagNoColors))
|
||||
rootCmd.PersistentFlags().BoolVar(&noColors, FlagNoColors, false, "Do not use colors in output")
|
||||
viper.BindPFlag(FlagNoColors, rootCmd.PersistentFlags().Lookup(FlagNoColors))
|
||||
|
||||
rootCmd.PersistentFlags().BoolVarP(&debug, FlagDebug, "d", false, "Display debugging output in the console. (default: false)")
|
||||
viper.BindPFlag(FlagDebug, rootCmd.PersistentFlags().Lookup(FlagDebug))
|
||||
rootCmd.PersistentFlags().BoolVarP(&debug, FlagDebug, "d", false, "Display debugging output in the console. (default: false)")
|
||||
viper.BindPFlag(FlagDebug, rootCmd.PersistentFlags().Lookup(FlagDebug))
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
@@ -65,34 +65,40 @@ func initConfig() {
|
||||
color.Disable()
|
||||
}
|
||||
|
||||
viper.SetEnvPrefix("zeit")
|
||||
viper.BindEnv("db")
|
||||
viper.SetEnvPrefix("zeit")
|
||||
viper.BindEnv("db")
|
||||
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// TODO: Discuss if toml is also needed - Config Type can only be set one per block, supporting toml would need a complete new block
|
||||
viper.AddConfigPath("$XDG_CONFIG_HOME")
|
||||
viper.AddConfigPath("$XDG_CONFIG_HOME/zeit")
|
||||
viper.AddConfigPath(home + "/.config")
|
||||
viper.AddConfigPath(home + "/.config/zeit")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName("zeit")
|
||||
}
|
||||
viper.AddConfigPath("$XDG_CONFIG_HOME")
|
||||
viper.AddConfigPath("$XDG_CONFIG_HOME/zeit")
|
||||
viper.AddConfigPath(home + "/.config")
|
||||
viper.AddConfigPath(home + "/.config/zeit")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName("zeit")
|
||||
}
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
// Set default values for parameters
|
||||
viper.Set("debug", false)
|
||||
}
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
// Set default values for parameters
|
||||
viper.Set("debug", false)
|
||||
}
|
||||
|
||||
if viper.GetBool("debug") {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
fmt.Fprintln(os.Stderr, "Using Database file:", viper.GetString("db"))
|
||||
}
|
||||
}
|
||||
if viper.GetBool("debug") {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
fmt.Fprintln(os.Stderr, "Using Database file:", viper.GetString("db"))
|
||||
}
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,10 +52,4 @@ var statsCmd = &cobra.Command{
|
||||
func init() {
|
||||
rootCmd.AddCommand(statsCmd)
|
||||
statsCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,11 +45,4 @@ var taskCmd = &cobra.Command{
|
||||
func init() {
|
||||
rootCmd.AddCommand(taskCmd)
|
||||
taskCmd.Flags().StringVarP(&taskGitRepository, "git", "g", "-", "Set the task's Git repository to enable commit message importing into activity notes.\nSet to an empty string '' to remove a previously set repository and disable git log imports.")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,11 +55,4 @@ func init() {
|
||||
trackCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be assigned")
|
||||
trackCmd.Flags().StringVarP(¬es, "notes", "n", "", "Activity notes")
|
||||
trackCmd.Flags().BoolVarP(&force, "force", "f", false, "Force begin tracking of a new task \neven though another one is still running \n(ONLY IF YOU KNOW WHAT YOU'RE DOING!)")
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,11 +37,4 @@ var trackingCmd = &cobra.Command{
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(trackingCmd)
|
||||
|
||||
var err error
|
||||
database, err = InitDatabase()
|
||||
if err != nil {
|
||||
fmt.Printf("%s %+v\n", CharError, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user