-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from SpatiumPortae/f/refactor-config
feat: refactor config for more extensibility
- Loading branch information
Showing
7 changed files
with
81 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/fatih/structs" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const CONFIGS_DIR_NAME = ".config" | ||
const PORTAL_CONFIG_DIR_NAME = "portal" | ||
const CONFIG_FILE_NAME = "config" | ||
const CONFIG_FILE_EXT = "yml" | ||
|
||
type Config struct { | ||
Relay string `mapstructure:"relay"` | ||
Verbose bool `mapstructure:"verbose"` | ||
} | ||
|
||
func GetDefault() Config { | ||
return Config{ | ||
Relay: "167.71.65.96:80", | ||
Verbose: false, | ||
} | ||
} | ||
|
||
func ToMap(config Config) map[string]any { | ||
p := map[string]any{} | ||
for _, field := range structs.Fields(config) { | ||
key := field.Tag("mapstructure") | ||
value := field.Value() | ||
p[key] = value | ||
} | ||
return p | ||
} | ||
|
||
func ToYaml(config Config) []byte { | ||
var builder strings.Builder | ||
for k, v := range ToMap(config) { | ||
builder.WriteString(fmt.Sprintf("%s: %v", k, v)) | ||
builder.WriteRune('\n') | ||
} | ||
return []byte(builder.String()) | ||
} | ||
|
||
func IsDefault(key string) bool { | ||
defaults := ToMap(GetDefault()) | ||
return viper.Get(key) == defaults[key] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters