Browse Source

Initial commit

master
Trevor Slocum 2 months ago
commit
569018bd03
6 changed files with 139 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +21
    -0
      LICENSE
  3. +17
    -0
      README.md
  4. +89
    -0
      ez.go
  5. +5
    -0
      go.mod
  6. +4
    -0
      go.sum

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
.idea/
vendor/
*.sh

+ 21
- 0
LICENSE View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Trevor Slocum <trevor@rocketnine.space>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

+ 17
- 0
README.md View File

@ -0,0 +1,17 @@
# ez
[![GoDoc](https://code.rocketnine.space/tslocum/godoc-static/raw/branch/master/badge.svg)](https://docs.rocketnine.space/code.rocketnine.space/tslocum/ez)
[![Donate](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
Easily serialize and deserialize data in YAML format
## Add to your application
```go get code.rocketnine.space/tslocum/ez```
## Dependencies
* [gopkg.in/yaml](https://github.com/go-yaml/yaml)
## Documentation
See [godoc](https://docs.rocketnine.space/code.rocketnine.space/tslocum/ez).

+ 89
- 0
ez.go View File

@ -0,0 +1,89 @@
// Package ez provides data serialization and deseralization in YAML format
package ez
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
p "path"
"path/filepath"
"gopkg.in/yaml.v2"
)
// DefaultConfigPath returns the default path to a configuration file for an
// application with the specified name. If no name is provided, the executable
// name is used.
func DefaultConfigPath(appName string) (string, error) {
homedir, err := os.UserHomeDir()
if err != nil {
return "", err
} else if homedir == "" {
return "", errors.New("a blank path to user homedir was returned")
}
if appName == "" {
appName = filepath.Base(os.Args[0])
}
dir := path.Join(homedir, ".config", appName)
_, err = os.Stat(path.Join(dir, "config.yaml"))
if err == nil {
return path.Join(dir, "config.yaml"), nil
}
_, err = os.Stat(path.Join(dir, "config.yml"))
if err == nil {
return path.Join(dir, "config.yml"), nil
}
return path.Join(dir, "config.yaml"), nil
}
// Serialize stores data in YAML format at the specified path.
func Serialize(object interface{}, path string) error {
if path == "" {
return errors.New("failed to serialize: no path specified")
}
out, err := yaml.Marshal(object)
if err != nil {
return fmt.Errorf("failed to marshal configuration: %s", err)
}
os.MkdirAll(p.Dir(path), 0755)
err = ioutil.WriteFile(path, out, 0644)
if err != nil {
return fmt.Errorf("failed to write to %s: %s", path, err)
}
return nil
}
// Deserialize loads data from the specified path. If a file does not exist at
// the specified path, no error is returned.
func Deserialize(object interface{}, path string) error {
if path == "" {
return errors.New("failed to deserialize: no path specified")
}
_, err := os.Stat(path)
if os.IsNotExist(err) {
return nil
}
configData, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file: %s", err)
}
err = yaml.Unmarshal(configData, object)
if err != nil {
return fmt.Errorf("failed to parse file: %s", err)
}
return nil
}

+ 5
- 0
go.mod View File

@ -0,0 +1,5 @@
module code.rocketnine.space/tslocum/ez
go 1.16
require gopkg.in/yaml.v2 v2.4.0

+ 4
- 0
go.sum View File

@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

Loading…
Cancel
Save