You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.4 KiB
87 lines
2.4 KiB
// staletea |
|
// Copyright (C) 2019 Jonas Franz |
|
// |
|
// 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 <http://www.gnu.org/licenses/>. |
|
|
|
package config |
|
|
|
import ( |
|
"gitea.com/jonasfranz/staletea/utils" |
|
"github.com/spf13/viper" |
|
"os" |
|
) |
|
|
|
// Key represents a config key |
|
type Key string |
|
|
|
// Get the value of the key from config |
|
func (k Key) Get() interface{} { |
|
return viper.Get(string(k)) |
|
} |
|
|
|
// Set the value of the key to config |
|
func (k Key) Set(value interface{}) { |
|
viper.Set(string(k), value) |
|
} |
|
|
|
func (k Key) setDefault(defaultValue interface{}) { |
|
viper.SetDefault(string(k), defaultValue) |
|
} |
|
|
|
// This block contains all config keys |
|
const ( |
|
GiteaURL Key = "gitea_url" |
|
BotAccessToken Key = "gitea_bot_access_token" |
|
|
|
SessionSecret Key = "session_secret" |
|
DatabaseDriver Key = "database_driver" |
|
DatabaseURI Key = "database_uri" |
|
|
|
RepositoryScanInterval Key = "repository_scan_interval" |
|
IssueScanInterval Key = "issue_scan_interval" |
|
|
|
RepositoryConfigFileName Key = "repository_config_file_name" |
|
) |
|
|
|
// SetupConfig fills all default values, reads the config and/or writes the default config. |
|
func SetupConfig() error { |
|
GiteaURL.setDefault("https://gitea.com") |
|
BotAccessToken.setDefault("YOUR_BOT_TOKEN") |
|
|
|
secret, err := utils.NewSecret(32) |
|
if err != nil { |
|
return err |
|
} |
|
SessionSecret.setDefault(secret) |
|
DatabaseDriver.setDefault("sqlite3") |
|
DatabaseURI.setDefault("staletea.db") |
|
|
|
RepositoryScanInterval.setDefault("@every 30s") |
|
IssueScanInterval.setDefault("@every 2m") |
|
|
|
RepositoryConfigFileName.setDefault(".stale.yml") |
|
|
|
viper.SetConfigName("config") |
|
viper.SetConfigType("yml") |
|
viper.AutomaticEnv() |
|
viper.AddConfigPath(".") |
|
if err := viper.SafeWriteConfigAs("config.yml"); err != nil { |
|
if os.IsNotExist(err) { |
|
if err := viper.WriteConfigAs("config.yml"); err != nil { |
|
return err |
|
} |
|
} |
|
} |
|
return viper.ReadInConfig() |
|
}
|
|
|