better file organization
This commit is contained in:
58
configs/main.go
Normal file
58
configs/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package configs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"packets/internal/consts"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
|
||||
// DefaultConfigTOML returns configTOML struct with all default values and create all directorys
|
||||
func DefaultConfigTOML() (*ConfigTOML, error) {
|
||||
|
||||
var config ConfigTOML
|
||||
|
||||
_, err := os.Stat(consts.DefaultCache_d)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
err := os.MkdirAll(consts.DefaultCache_d, 0666)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = os.Stat(consts.DefaultCache_d)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
err := os.MkdirAll(consts.DefaultData_d, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config.Config.Cache_d = consts.DefaultCache_d
|
||||
config.Config.Data_d = consts.DefaultData_d
|
||||
config.Config.HttpPort = consts.DefaultHttpPort
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// GetConfigTOML return settings values
|
||||
func GetConfigTOML() (*ConfigTOML, error) {
|
||||
f, err := os.Open(filepath.Join(consts.DefaultLinux_d, "config.toml"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decoder := toml.NewDecoder(f)
|
||||
|
||||
var config ConfigTOML
|
||||
if err := decoder.Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
26
configs/structs.go
Normal file
26
configs/structs.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package configs
|
||||
|
||||
type Manifest struct {
|
||||
Info struct {
|
||||
Name string `toml:"name"`
|
||||
Version string `toml:"version"`
|
||||
Description string `toml:"description"`
|
||||
Dependencies []string `toml:"dependencies"`
|
||||
Author string `toml:"author"`
|
||||
Family string `toml:"family"`
|
||||
Serial uint `toml:"serial"`
|
||||
} `toml:"Info"`
|
||||
Hooks struct {
|
||||
Install string `toml:"install"`
|
||||
Remove string `toml:"remove"`
|
||||
} `toml:"Hooks"`
|
||||
}
|
||||
|
||||
type ConfigTOML struct {
|
||||
Config struct {
|
||||
HttpPort int `toml:"httpPort"`
|
||||
Cache_d string `toml:"cache_d"`
|
||||
Data_d string `toml:"data_d"`
|
||||
Bin_d string `toml:"bin_d"`
|
||||
} `toml:"Config"`
|
||||
}
|
||||
Reference in New Issue
Block a user