himitsu/config/config.go

56 lines
985 B
Go
Raw Normal View History

2024-04-28 21:35:37 +02:00
package config
2024-04-28 22:08:34 +02:00
import (
"himitsu/totp"
"os"
"path/filepath"
"github.com/kirsle/configdir"
"gopkg.in/yaml.v3"
)
2024-04-28 21:35:37 +02:00
const Version = "v0.0.1"
2024-04-28 22:08:34 +02:00
var DefaultConfig = []totp.TOTP{
2024-04-28 21:35:37 +02:00
{
2024-04-28 22:08:34 +02:00
Label: "Label",
Secret: "Secret",
Account: "Account",
2024-04-28 21:35:37 +02:00
},
}
2024-04-28 22:08:34 +02:00
var Config []totp.TOTP
func Init() {
configPath := configdir.LocalConfig("ontake", "himitsu")
err := configdir.MakePath(configPath) // Ensure it exists.
if err != nil {
panic(err)
}
configFile := filepath.Join(configPath, "totp.yml")
// Does the file not exist?
if _, err = os.Stat(configFile); os.IsNotExist(err) {
// Create the new config file.
fh, err := os.Create(configFile)
if err != nil {
panic(err)
}
defer fh.Close()
encoder := yaml.NewEncoder(fh)
encoder.Encode(&DefaultConfig)
Config = DefaultConfig
} else {
// Load the existing file.
fh, err := os.Open(configFile)
if err != nil {
panic(err)
}
defer fh.Close()
decoder := yaml.NewDecoder(fh)
decoder.Decode(&Config)
}
}