mukan-ignite/ignite/pkg/confile/confile.go
Mukan Erkin Törük 26b204bd04
Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
feat: fork Ignite CLI v29 as Mukan Ignite — remove cosmos-sdk restrictions
2026-05-11 03:31:37 +03:00

50 lines
1.3 KiB
Go

// Package confile is helper to load and overwrite configuration files.
package confile
import (
"os"
"path/filepath"
)
// ConfigFile represents a configuration file.
type ConfigFile struct {
creator EncodingCreator
path string
}
// New starts a new ConfigFile by using creator as underlying EncodingCreator to encode and
// decode config file that presents or will present on path.
func New(creator EncodingCreator, path string) *ConfigFile {
return &ConfigFile{
creator: creator,
path: path,
}
}
// Load loads content of config file into v if file exist on path.
// otherwise nothing loaded into v and no error is returned.
func (c *ConfigFile) Load(v interface{}) error {
file, err := os.Open(c.path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer file.Close()
return c.creator.Create(file).Decode(v)
}
// Save saves v into config file by overwriting the previous content it also creates the
// config file if it wasn't exist.
func (c *ConfigFile) Save(v interface{}) error {
if err := os.MkdirAll(filepath.Dir(c.path), 0o755); err != nil {
return err
}
file, err := os.OpenFile(c.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer file.Close()
return c.creator.Create(file).Encode(v)
}