Refactor init command and add errors util

This commit is contained in:
Mickaël Menu 2020-12-25 12:14:01 +01:00
parent bdda74362f
commit 9f4d2ac81b
No known key found for this signature in database
GPG Key ID: 53D73664CD359895
6 changed files with 126 additions and 52 deletions

2
.gitignore vendored
View File

@ -13,3 +13,5 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
.zk

View File

@ -1,61 +1,11 @@
package cmd package cmd
import ( import "github.com/mickael-menu/zk/core/zk"
"errors"
"fmt"
"os"
"path/filepath"
)
type Init struct { type Init struct {
Directory string `arg optional name:"directory" default:"."` Directory string `arg optional name:"directory" default:"."`
} }
func (cmd *Init) Run() error { func (cmd *Init) Run() error {
path, err := filepath.Abs(cmd.Directory) return zk.Create(cmd.Directory)
if err != nil {
return err
}
if existingPath, err := locateZk(path); err == nil {
return fmt.Errorf("a slip box already exists in %v", existingPath)
}
// Create .zk and .zk/templates directories.
err = os.MkdirAll(filepath.Join(path, ".zk/templates"), os.ModePerm)
if err != nil {
return err
}
return nil
}
var ErrNotFound = errors.New("slip box not found")
// locate finds the root of the slip box containing the given path.
func locateZk(path string) (string, error) {
if path == "/" || path == "." {
return "", ErrNotFound
}
if !filepath.IsAbs(path) {
panic("locateZk expects an absolute path")
}
if dotPath := filepath.Join(path, ".zk"); dirExists(dotPath) {
return path, nil
}
return locateZk(filepath.Dir(path))
}
func dirExists(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
switch mode := fi.Mode(); {
case mode.IsDir():
return true
default:
return false
}
} }

11
cmd/new.go Normal file
View File

@ -0,0 +1,11 @@
package cmd
import "github.com/mickael-menu/zk/core/zk"
type New struct {
Directory string `arg optional name:"directory" default:"."`
}
func (cmd *New) Run() error {
return zk.Open(cmd.Directory)
}

92
core/zk/zk.go Normal file
View File

@ -0,0 +1,92 @@
package zk
import (
"fmt"
"os"
"path/filepath"
"github.com/mickael-menu/zk/util/errors"
)
const defaultConfig = `
# Test
test = thing
`
// Open locates a slip box at the given path and parses its configuration.
func Open(path string) error {
wrap := errors.Wrapper("open failed")
path, err := filepath.Abs(path)
if err != nil {
return wrap(err)
}
_, err = locateRoot(path)
return wrap(err)
}
// Create initializes a new slip box at the given path.
func Create(path string) error {
wrap := errors.Wrapper("init failed")
path, err := filepath.Abs(path)
if err != nil {
return wrap(err)
}
if existingPath, err := locateRoot(path); err == nil {
return wrap(fmt.Errorf("a slip box already exists in %v", existingPath))
}
// Create .zk and .zk/templates directories.
err = os.MkdirAll(filepath.Join(path, ".zk/templates"), os.ModePerm)
if err != nil {
return wrap(err)
}
// Write default config.toml.
f, err := os.Create(filepath.Join(path, ".zk/config.toml"))
if err != nil {
return wrap(err)
}
_, err = f.WriteString(defaultConfig)
if err != nil {
return wrap(err)
}
return nil
}
// locate finds the root of the slip box containing the given path.
func locateRoot(path string) (string, error) {
if !filepath.IsAbs(path) {
panic("absolute path expected")
}
var locate func(string) (string, error)
locate = func(currentPath string) (string, error) {
if currentPath == "/" || currentPath == "." {
return "", fmt.Errorf("no slip box found in %v or a parent directory", path)
}
if dotPath := filepath.Join(currentPath, ".zk"); dirExists(dotPath) {
return currentPath, nil
}
return locate(filepath.Dir(currentPath))
}
return locate(path)
}
func dirExists(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
switch mode := fi.Mode(); {
case mode.IsDir():
return true
default:
return false
}
}

View File

@ -7,6 +7,7 @@ import (
var cli struct { var cli struct {
Init cmd.Init `cmd help:"Create a slip box in the given directory"` Init cmd.Init `cmd help:"Create a slip box in the given directory"`
New cmd.New `cmd help:"Add a new note to the slip box"`
} }
func main() { func main() {

18
util/errors/errors.go Normal file
View File

@ -0,0 +1,18 @@
package errors
import (
"fmt"
)
func Wrapper(msg string) func(error) error {
return func(err error) error {
return Wrap(err, msg)
}
}
func Wrap(err error, msg string) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", msg, err)
}