🔧 Add config
This commit is contained in:
parent
678b0d499f
commit
4d49d9d57c
4 changed files with 99 additions and 14 deletions
60
config/config.go
Normal file
60
config/config.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/kirsle/configdir"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Source string `toml:"source"`
|
||||
SavePath string `toml:"save_path"`
|
||||
MusicPath string `toml:"music_path"`
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
func newConfig() Config {
|
||||
return Config{
|
||||
Source: xdg.UserDirs.Download,
|
||||
SavePath: filepath.Join(xdg.UserDirs.Documents, "StripBeats"),
|
||||
MusicPath: xdg.UserDirs.Music,
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
config = newConfig()
|
||||
configPath := configdir.LocalConfig("strip-beats")
|
||||
if err := configdir.MakePath(configPath); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
configFile := filepath.Join(configPath, "profile.toml")
|
||||
if !exists(configFile) {
|
||||
file, err := os.Create(configFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = toml.NewEncoder(file).Encode(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
file.Close()
|
||||
} else {
|
||||
if _, err := toml.DecodeFile(configFile, &config); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetConfig() Config {
|
||||
return config
|
||||
}
|
||||
|
||||
func exists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !errors.Is(err, os.ErrNotExist)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue