aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-07-24 13:01:36 +0700
committerShulhan <ms@kilabit.info>2023-10-06 10:03:04 +0700
commit8a1a81a6428ae970569d9c2a506acf90deb2e0c4 (patch)
treed3e82e19718962181b51572a0395ed6b1cef2222 /config.go
parent479e33040d93e582aa10f19c299b2633f2270d24 (diff)
downloadgotp-8a1a81a6428ae970569d9c2a506acf90deb2e0c4.tar.xz
all: unfold private key path before reading
By unfolding, user can set the path to private key using "~" that points to their home. This make the key can works across home directory, in case user have multiple homes (Linux and macOS have different home path and maybe user name, but both can use "~" as substitution for $HOME)
Diffstat (limited to 'config.go')
-rw-r--r--config.go42
1 files changed, 29 insertions, 13 deletions
diff --git a/config.go b/config.go
index 024450c..c7150d6 100644
--- a/config.go
+++ b/config.go
@@ -13,6 +13,7 @@ import (
"strings"
"github.com/shuLhan/share/lib/ini"
+ libos "github.com/shuLhan/share/lib/os"
)
const (
@@ -42,18 +43,18 @@ func newConfig(file string) (cfg *config, err error) {
if !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf(`%s: Open %q: %w`, logp, file, err)
}
- isNotExist = true
- }
- if isNotExist {
var dir = filepath.Dir(file)
err = os.MkdirAll(dir, 0700)
if err != nil {
return nil, fmt.Errorf(`%s: MkdirAll %q: %w`, logp, dir, err)
}
+ isNotExist = true
}
- cfg, err = loadConfig(content)
+ cfg = &config{}
+
+ err = cfg.UnmarshalText(content)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
@@ -64,25 +65,40 @@ func newConfig(file string) (cfg *config, err error) {
return cfg, nil
}
-// loadConfig load configuration from raw bytes.
-func loadConfig(content []byte) (cfg *config, err error) {
- var logp = `loadConfig`
+// UnmarshalText load configuration from raw bytes.
+func (cfg *config) UnmarshalText(content []byte) (err error) {
+ var logp = `UnmarshalText`
+
+ cfg.Issuers = make(map[string]string)
- cfg = &config{
- Issuers: make(map[string]string),
+ if len(content) > 0 {
+ err = ini.Unmarshal(content, cfg)
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
}
- err = ini.Unmarshal(content, cfg)
- if err != nil {
- return nil, fmt.Errorf(`%s: %w`, logp, err)
+ if len(cfg.PrivateKey) != 0 {
+ cfg.PrivateKey, err = libos.PathUnfold(cfg.PrivateKey)
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
}
- return cfg, nil
+ return nil
}
// MarshalText convert the config object back to INI format.
func (cfg *config) MarshalText() (text []byte, err error) {
var logp = `MarshalText`
+
+ if len(cfg.PrivateKey) != 0 {
+ cfg.PrivateKey, err = libos.PathFold(cfg.PrivateKey)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+ }
+
text, err = ini.Marshal(cfg)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)