Skip to content

Commit

Permalink
feat: implement config
Browse files Browse the repository at this point in the history
  • Loading branch information
jon4hz committed Jan 20, 2024
1 parent cc324b1 commit 4f61f06
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gmotd.yml
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ansible.python.interpreterPath": "/bin/python"
}
39 changes: 26 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package config

type Config struct {
Hostname Hostname
Smart Smart
}
import (
"github.com/spf13/viper"
)

func Load(cfg *Config) error {
viper.SetConfigName("gmotd")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/gmotd")
viper.AddConfigPath("$HOME/.gmotd")
viper.AddConfigPath(".")

setDefaults()

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return nil
}
return err
}

if err := viper.Unmarshal(cfg); err != nil {
return err
}

type Hostname struct {
Disabled bool
Color string
Figlet bool
FigletFont string
FigletFontDir string
return nil
}

type Smart struct {
Disabled bool
Disks []string
func setDefaults() {
viper.SetDefault("zpool.disabled", true)
}
41 changes: 41 additions & 0 deletions config/config_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

type Config struct {
Hostname *Hostname `mapstructure:"hostname"`
Uptime *Uptime `mapstructure:"uptime"`
SysInfo *SysInfo `mapstructure:"sysinfo"`
Zpool *Zpool `mapstructure:"zpool"`
Docker *Docker `mapstructure:"docker"`
Smart *Smart `mapstructure:"smart"`
}

type Hostname struct {
Disabled bool `mapstructure:"disabled"`
Color string `mapstructure:"color"`
Figlet bool `mapstructure:"figlet"`
FigletFont string `mapstructure:"figlet_font"`
FigletFontDir string `mapstructure:"figlet_font_dir"`
}

type Uptime struct {
Disabled bool `mapstructure:"disabled"`
Precision int `mapstructure:"precision"`
}

type SysInfo struct {
Disabled bool `mapstructure:"disabled"`
}

type Zpool struct {
Disabled bool `mapstructure:"disabled"`
}

type Docker struct {
Disabled bool `mapstructure:"disabled"`
IgnoreContainers []string `mapstructure:"ignore_containers"`
}

type Smart struct {
Disabled bool `mapstructure:"disabled"`
Disks []string `mapstructure:"disks"`
}
9 changes: 8 additions & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ func New() *Context {
return &Context{
Context: c,
cancel: cancel,
Config: &config.Config{},
Config: &config.Config{
Hostname: new(config.Hostname),
Uptime: new(config.Uptime),
SysInfo: new(config.SysInfo),
Zpool: new(config.Zpool),
Docker: new(config.Docker),
Smart: new(config.Smart),
},
Runtime: &Runtime{},
}
}
Expand Down
18 changes: 0 additions & 18 deletions defaults/defaults.go

This file was deleted.

27 changes: 20 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42
github.com/shirou/gopsutil/v3 v3.21.12
github.com/spf13/viper v1.18.2
golang.org/x/term v0.6.0
gopkg.in/ini.v1 v1.66.6
gopkg.in/ini.v1 v1.67.0
)

require (
Expand All @@ -27,14 +28,17 @@ require (
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/krystal/go-runner v0.1.2 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
Expand All @@ -43,17 +47,26 @@ require (
github.com/muesli/termenv v0.15.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tklauser/go-sysconf v0.3.9 // indirect
github.com/tklauser/numcpus v0.3.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/time v0.5.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
Loading

0 comments on commit 4f61f06

Please sign in to comment.