Browse Source

rewrite config to be generic

Signed-off-by: gryffyn <me@neveris.one>
main
gryffyn 5 months ago
parent
commit
485140fc9e
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 2
      go.mod
  2. 4
      ui/createpost.go
  3. 73
      utils/config.go

2
go.mod

@ -3,7 +3,7 @@ module git.neveris.one/gryffyn/ghcms
go 1.15
require (
git.neveris.one/gryffyn/femto v0.0.0-20210112104532-ddd6b1243814
git.neveris.one/gryffyn/femto v0.0.0-20210112134309-bcfe1c2e75f9
github.com/andskur/argon2-hashing v0.1.3
github.com/gdamore/tcell/v2 v2.1.0
gitlab.com/tslocum/cview v1.5.2

4
ui/createpost.go

@ -49,8 +49,8 @@ func main() {
return event
})
var colorscheme femto.Colorscheme
if monokai := runtime.Files.FindFile(femto.RTColorscheme, "monokai"); monokai != nil {
if data, err := monokai.Data(); err == nil {
if bnwcode := runtime.Files.FindFile(femto.RTColorscheme, "bnw-code"); bnwcode != nil {
if data, err := bnwcode.Data(); err == nil {
colorscheme = femto.ParseColorscheme(string(data))
}
}

73
utils/config.go

@ -9,7 +9,16 @@ import (
)
type Config struct {
Test string `yaml:"test"`
Username string
AppName string
Config struct{}
}
type configPaths struct {
home string
configDir string
dataDir string
configFile string
}
func ifile(filename string) ([]byte, error) {
@ -23,41 +32,59 @@ func ofile(filename string, data []byte) error {
return err
}
var home, _ = os.UserHomeDir()
var cdir = home + "/.config/ghcms"
var ddir = home + "/.local/share/ghcms"
var cfile = cdir + "/config.yml"
func Setup() {
if _, err := os.Stat(home); os.IsExist(err) {
if err := os.Mkdir(cdir, 0775); err != nil {
func (c *Config) setup() {
if _, err := os.Stat(c.getPaths().configDir); os.IsExist(err) {
if err := os.Mkdir(c.getPaths().configDir, 0775); err != nil {
log.Fatal(err)
}
if err := os.Mkdir(ddir, 0775); err != nil {
}
if _, err := os.Stat(c.getPaths().dataDir); os.IsExist(err) {
if err := os.Mkdir(c.getPaths().dataDir, 0775); err != nil {
log.Fatal(err)
}
}
}
func FirstRun() bool {
if _, err := os.Stat(cdir); os.IsNotExist(err) {
func (c *Config) getPaths() configPaths {
if c.Username == "root" {
return configPaths{
home: "/root",
configDir: "/etc/" + c.AppName,
dataDir: "/usr/share/" + c.AppName,
configFile: "/etc/" + c.AppName + "/config.yml",
}
} else {
home, _ := os.UserHomeDir()
cdir, _ := os.UserConfigDir()
return configPaths{
home: home,
configDir: cdir + "/" + c.AppName,
dataDir: home + "/.local/share/" + c.AppName,
configFile: cdir + "/" + c.AppName + "/config.yml",
}
}
}
func (c *Config) firstRun() bool {
_, errC := os.Stat(c.getPaths().configDir)
_, errD := os.Stat(c.getPaths().dataDir)
if os.IsNotExist(errC) && os.IsNotExist(errD) {
return true
}
return false
}
func WriteConfig() {
conf := Config{}
confm, _ := yaml.Marshal(&conf)
if FirstRun() {
Setup()
func (c *Config) Write(i interface{}) {
confm, _ := yaml.Marshal(&i)
if c.firstRun() {
c.setup()
}
_ = ofile(cfile, confm)
_ = ofile(c.getPaths().configFile, confm)
}
func GetConfig() (Config, error) {
conf := Config{}
in, err := ifile(cfile)
yaml.Unmarshal(in, conf)
return conf, err
func (c *Config) Get() (struct{}, error) {
in, err := ifile(c.getPaths().configFile)
yaml.Unmarshal(in, c.Config)
return c.Config, err
}
Loading…
Cancel
Save