2023-08-15 17:34:45 +02:00
|
|
|
/*
|
2023-08-19 20:35:29 +02:00
|
|
|
YetAnotherToDoList
|
2023-08-15 17:34:45 +02:00
|
|
|
Copyright © 2023 gilex-dev gilex-dev@proton.me
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2023-08-19 20:35:29 +02:00
|
|
|
the Free Software Foundation, version 3.
|
2023-08-15 17:34:45 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-01 23:42:19 +02:00
|
|
|
"log"
|
2023-08-15 17:34:45 +02:00
|
|
|
"os"
|
2023-09-01 23:42:19 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-08-15 17:34:45 +02:00
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-08-15 17:34:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2023-10-10 22:26:07 +02:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/database"
|
2023-09-15 23:45:28 +02:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals"
|
2023-08-15 17:34:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var cfgFile string
|
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
2023-09-15 23:45:28 +02:00
|
|
|
Use: "YetAnotherToDoList",
|
|
|
|
Version: fmt.Sprintf("%s %s", globals.Version, globals.CommitHash),
|
|
|
|
Short: "Simple To Do List Web Application with Vue.js frontend and GraphQL API",
|
2023-09-01 23:42:19 +02:00
|
|
|
Long: `YetAnotherToDoList 2023 by gilex-dev
|
|
|
|
A simple To Do List Web Application with Go backend, Vue.js frontend and a GraphQL API
|
|
|
|
`,
|
2023-08-15 17:34:45 +02:00
|
|
|
// Uncomment the following line if your bare application
|
|
|
|
// has an action associated with it:
|
|
|
|
// Run: func(cmd *cobra.Command, args []string) { },
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-09-15 23:45:28 +02:00
|
|
|
rootCmd.CompletionOptions.HiddenDefaultCmd = true
|
|
|
|
// check if first argument needs init (inspired by https://github.com/spf13/cobra/issues/823#issuecomment-617863653)
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
noSetupRequired := []string{"__complete", "__completeNoDesc", "completion", "help", licenseCmd.Name()}
|
|
|
|
for _, subcommand := range noSetupRequired {
|
|
|
|
if subcommand != os.Args[1] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-10-10 22:26:07 +02:00
|
|
|
cobra.OnInitialize(initConfig, initLog, initDB)
|
2023-08-15 17:34:45 +02:00
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports persistent flags, which, if defined here,
|
|
|
|
// will be global for your application.
|
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.YetAnotherToDoList.yaml)")
|
2023-11-05 17:42:14 +01:00
|
|
|
rootCmd.PersistentFlags().String("logFile", "", "Path to log file")
|
|
|
|
rootCmd.PersistentFlags().String("sqlite3File", "", "Path to SQLite3 database")
|
2023-08-15 17:34:45 +02:00
|
|
|
|
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
2023-09-01 23:42:19 +02:00
|
|
|
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
2023-08-15 17:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
// Find home directory.
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
2023-09-01 23:42:19 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
2023-08-15 17:34:45 +02:00
|
|
|
// Search config in home directory with name ".YetAnotherToDoList" (without extension).
|
|
|
|
viper.AddConfigPath(home)
|
2023-09-01 23:42:19 +02:00
|
|
|
// Search config in working directory with name ".YetAnotherToDoList" (without extension).
|
|
|
|
viper.AddConfigPath(wd)
|
2023-08-15 17:34:45 +02:00
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.SetConfigName(".YetAnotherToDoList")
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
2023-09-01 23:42:19 +02:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, "Unable to read in", viper.ConfigFileUsed(), err)
|
2023-08-15 17:34:45 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-01 23:42:19 +02:00
|
|
|
|
|
|
|
func initLog() {
|
|
|
|
var utc = 0
|
|
|
|
var time_zone_use, time_zone_alt string
|
|
|
|
|
|
|
|
time_zone_local, _ := time.Now().Zone()
|
|
|
|
time_zone_offset := strings.Split(time.Now().In(time.Local).String(), " ")[2]
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
if viper.GetBool("logging.logUTC") {
|
2023-09-01 23:42:19 +02:00
|
|
|
utc = log.LUTC
|
|
|
|
time_zone_use = "UTC"
|
|
|
|
time_zone_alt = time_zone_local
|
|
|
|
time_zone_offset_rune := []rune(time_zone_offset)
|
|
|
|
|
|
|
|
if time_zone_offset_rune[0] == '+' {
|
|
|
|
time_zone_offset_rune[0] = '-'
|
|
|
|
}
|
|
|
|
|
|
|
|
time_zone_offset = string(time_zone_offset_rune)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
time_zone_use = time_zone_local
|
|
|
|
time_zone_alt = "UTC"
|
|
|
|
}
|
|
|
|
|
|
|
|
logger_flags := log.Ldate | log.Ltime | utc
|
2023-10-10 22:26:07 +02:00
|
|
|
globals.Logger = log.New(os.Stdout, "", logger_flags)
|
2023-09-01 23:42:19 +02:00
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
if err := viper.BindPFlag("logging.logFile", rootCmd.Flags().Lookup("logFile")); err != nil {
|
2023-09-01 23:42:19 +02:00
|
|
|
fmt.Println("Unable to bind flag:", err)
|
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
if viper.GetString("logging.logFile") != "" {
|
|
|
|
log_path, err := filepath.Abs(viper.GetString("logging.logFile"))
|
2023-09-01 23:42:19 +02:00
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
globals.Logger.SetOutput(os.Stdout)
|
2023-09-01 23:42:19 +02:00
|
|
|
if err != nil {
|
2023-10-10 22:26:07 +02:00
|
|
|
globals.Logger.Println("Invalid path for log file", log_path)
|
2023-09-01 23:42:19 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
logFile, err := os.OpenFile(log_path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
2023-09-01 23:42:19 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-10 22:26:07 +02:00
|
|
|
globals.Logger.Println("Failed to write to log file:", err)
|
2023-09-01 23:42:19 +02:00
|
|
|
} else {
|
2023-10-10 22:26:07 +02:00
|
|
|
globals.Logger.Println("Switching to log file", log_path)
|
2023-11-05 17:42:14 +01:00
|
|
|
globals.Logger.SetOutput(logFile)
|
2023-09-01 23:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
globals.Logger.SetFlags(0)
|
|
|
|
globals.Logger.Println()
|
|
|
|
globals.Logger.SetFlags(logger_flags)
|
|
|
|
globals.Logger.Printf("Started YetAnotherToDoList with pid: %v\n", os.Getpid())
|
|
|
|
globals.Logger.Printf("Using %v (%v %v) in logs", time_zone_use, time_zone_alt, time_zone_offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDB() {
|
2023-11-05 17:42:14 +01:00
|
|
|
if err := viper.BindPFlag("database.sqlite3File", rootCmd.Flags().Lookup("sqlite3File")); err != nil {
|
2023-10-10 22:26:07 +02:00
|
|
|
fmt.Println("Unable to bind flag:", err)
|
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
if viper.GetString("database.sqlite3File") == "" {
|
2023-10-10 22:26:07 +02:00
|
|
|
globals.Logger.Fatalln("No SQLite3 file specified")
|
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
db_path, err := filepath.Abs(viper.GetString("database.sqlite3File"))
|
2023-10-10 22:26:07 +02:00
|
|
|
if err != nil {
|
|
|
|
globals.Logger.Fatalln("Invalid path for SQLite3 file", db_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
globals.Logger.Println("Connecting to SQLite3", db_path)
|
2023-11-05 17:42:14 +01:00
|
|
|
globals.DB = database.InitSQLite3(db_path, globals.DB_schema, globals.Logger, []byte(viper.GetString("database.secret")), viper.GetString("database.initialAdmin.userName"), viper.GetString("database.initialAdmin.password"))
|
2024-02-02 21:23:32 +01:00
|
|
|
globals.DB.CleanExpiredRefreshTokensTicker(time.Minute * 10) //TODO: add to viper
|
|
|
|
globals.DB.CleanRevokedAccessTokensTicker(time.Minute * 10) //TODO: add to viper
|
2023-09-01 23:42:19 +02:00
|
|
|
}
|