fix: config creation

This commit is contained in:
2025-11-24 20:27:51 +03:00
parent 0160eec423
commit fe262d687a
4 changed files with 60 additions and 9 deletions

View File

@@ -32,7 +32,7 @@ func main() {
logger.NewLogger,
),
fx.Invoke(func(log *logger.Logger) {
log.Info("Я футбольный мячик :w")
log.Info("Starting application...");
}),
)

View File

@@ -0,0 +1,20 @@
// Copyright (c) 2025 Nikolai Papin
//
// This file is part of the Auto Attendance app that looks for
// self-attend QR-codes during lectures and opens their URLs in your
// browser.
//
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 <https://www.gnu.org/licenses/>.
package browserlauncher

View File

@@ -0,0 +1,20 @@
// Copyright (c) 2025 Nikolai Papin
//
// This file is part of the Auto Attendance app that looks for
// self-attend QR-codes during lectures and opens their URLs in your
// browser.
//
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 <https://www.gnu.org/licenses/>.
package browserlauncher

View File

@@ -36,6 +36,7 @@ type Config struct {
}
type AppConfig struct {
SettingsReviewed bool `mapstructure:"settings_reviewed"`
EnableAlarm bool `mapstructure:"enable_alarm"`
EnableLinkOpening bool `mapstructure:"enable_link_opening"`
UseAttendanceJounralApi bool `mapstructure:"use_attendance_journal_api"`
@@ -62,6 +63,7 @@ type LoggingConfig struct {
func getDefaultConfig() Config {
return Config{
App: AppConfig{
SettingsReviewed: false,
EnableAlarm: false,
EnableLinkOpening: true,
UseAttendanceJounralApi: false,
@@ -108,6 +110,7 @@ func initializeViper(appName string) (*viper.Viper, string, error) {
defaults := getDefaultConfig()
v.SetDefault("app.settings_reviewed", defaults.App.SettingsReviewed)
v.SetDefault("app.enable_alarm", defaults.App.EnableAlarm)
v.SetDefault("app.enable_link_opening", defaults.App.EnableLinkOpening)
v.SetDefault("app.use_attendance_journal_api", defaults.App.UseAttendanceJounralApi)
@@ -129,15 +132,23 @@ func NewConfig() (*Config, error) {
return nil, fmt.Errorf("failed to initialize viper: %w", err)
}
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
if err := v.SafeWriteConfig(); err != nil {
return nil, fmt.Errorf("failed to create config file: %w", err)
}
fmt.Printf("Created new config file at: %s\n", configFile)
} else {
return nil, fmt.Errorf("failed to read config file: %w", err)
if _, err := os.Stat(configFile); os.IsNotExist(err) {
file, err := os.Create(configFile)
if err != nil {
return nil, fmt.Errorf("failed to create config file: %w", err)
}
file.Close()
if err := v.WriteConfig(); err != nil {
return nil, fmt.Errorf("failed to write default config: %w", err)
}
fmt.Printf("Created new config file at: %s\n", configFile)
} else if err != nil {
return nil, fmt.Errorf("failed to check config file: %w", err)
}
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var config Config