Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkg/boilr/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"log"
"os/user"
"path/filepath"

"github.com/tmrts/boilr/pkg/util/exit"
Expand Down Expand Up @@ -62,7 +63,12 @@ func IsTemplateDirInitialized() (bool, error) {
}

func init() {
homeDir := os.Getenv("HOME")
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
homeDir := usr.HomeDir

if homeDir == "" {
// FIXME is this really necessary?
exit.Error(fmt.Errorf("environment variable ${HOME} should be set"))
Expand Down
135 changes: 135 additions & 0 deletions pkg/cmd/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package cmd

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)

// https://gist.github.com/r0l1/92462b38df26839a3ca324697c8cba04
/* MIT License
*
* Copyright (c) 2017 Roland Singer [[email protected]]
*
* 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.
*/

// CopyFile copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file. The file mode will be copied from the source and
// the copied data is synced/flushed to stable storage.
func CopyFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
if e := out.Close(); e != nil {
err = e
}
}()

_, err = io.Copy(out, in)
if err != nil {
return
}

err = out.Sync()
if err != nil {
return
}

si, err := os.Stat(src)
if err != nil {
return
}
err = os.Chmod(dst, si.Mode())
if err != nil {
return
}

return
}

// CopyDir recursively copies a directory tree, attempting to preserve permissions.
// Source directory must exist, destination directory must *not* exist.
// Symlinks are ignored and skipped.
func CopyDir(src string, dst string) (err error) {
src = filepath.Clean(src)
dst = filepath.Clean(dst)

si, err := os.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
}

_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return
}
if err == nil {
return fmt.Errorf("destination already exists")
}

err = os.MkdirAll(dst, si.Mode())
if err != nil {
return
}

entries, err := ioutil.ReadDir(src)
if err != nil {
return
}

for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())

if entry.IsDir() {
err = CopyDir(srcPath, dstPath)
if err != nil {
return
}
} else {
// Skip symlinks.
if entry.Mode()&os.ModeSymlink != 0 {
continue
}

err = CopyFile(srcPath, dstPath)
if err != nil {
return
}
}
}

return
}
2 changes: 1 addition & 1 deletion pkg/cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Download = &cli.Command{
// FIXME Half-Updates leave messy templates
Run: func(c *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"template-repo", validate.UnixPath},
{"template-repo", validate.Path},
{"template-tag", validate.AlphanumericExt},
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var Rename = &cli.Command{
Short: "Rename a project template",
Run: func(c *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"old-template-tag", validate.UnixPath},
{"new-template-tag", validate.UnixPath},
{"old-template-tag", validate.Path},
{"new-template-tag", validate.Path},
})

MustValidateTemplateDir()
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
cli "github.com/spf13/cobra"

"github.com/tmrts/boilr/pkg/boilr"
"github.com/tmrts/boilr/pkg/util/exec"
"github.com/tmrts/boilr/pkg/util/exit"
"github.com/tmrts/boilr/pkg/util/osutil"
"github.com/tmrts/boilr/pkg/util/validate"
Expand All @@ -20,7 +19,7 @@ var Save = &cli.Command{
Short: "Save a local project template to template registry",
Run: func(c *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"template-path", validate.UnixPath},
{"template-path", validate.Path},
{"template-tag", validate.AlphanumericExt},
})

Expand Down Expand Up @@ -51,7 +50,7 @@ var Save = &cli.Command{
}
}

if _, err := exec.Cmd("cp", "-r", tmplDir, targetDir); err != nil {
if err := CopyDir(tmplDir, targetDir); err != nil {
exit.Error(err)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var Use = &cli.Command{
Short: "Execute a project template in the given directory",
Run: func(cmd *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"template-tag", validate.UnixPath},
{"target-dir", validate.UnixPath},
{"template-tag", validate.Path},
{"target-dir", validate.Path},
})

MustValidateTemplateDir()
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var Validate = &cli.Command{
Short: "Validate a local project template",
Run: func(_ *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"template-path", validate.UnixPath},
{"template-path", validate.Path},
})

templatePath := args[0]
Expand Down
11 changes: 5 additions & 6 deletions pkg/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/tmrts/boilr/pkg/util/tlog"
)
Expand Down Expand Up @@ -110,13 +109,13 @@ func Func(defval interface{}) Interface {
}

func scanLine() (string, error) {
input := bufio.NewReader(os.Stdin)
line, err := input.ReadString('\n')
if err != nil {
return line, err
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
line := input.Text()
return line, nil
}

return strings.TrimSuffix(line, "\n"), nil
return "", nil
}

// New returns a prompt closure when executed asks for
Expand Down
6 changes: 1 addition & 5 deletions pkg/util/validate/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import "regexp"
const (
url = `^(https?:\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`

email = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x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F}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"

alpha = "^[a-zA-Z]+$"
alphanumeric = "^[a-zA-Z0-9]+$"
Expand Down Expand Up @@ -35,9 +34,6 @@ var (
// Numeric regular expression for numeric strings.
Numeric = regexp.MustCompile(numeric)

// UnixPath regular expression for unix path strings.
UnixPath = regexp.MustCompile(unixPath)

// URL regular expression for URL strings.
URL = regexp.MustCompile(url)
)
21 changes: 0 additions & 21 deletions pkg/util/validate/pattern/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@ import (
"github.com/tmrts/boilr/pkg/util/validate/pattern"
)

func TestUnixPathPattern(t *testing.T) {
tests := []struct {
String string
Valid bool
}{
{"", false},
{"/", true},
{"/root", true},
{"/tmp-dir", true},
{"/tmp-dir/new_dir", true},
{"/TMP/dir", true},
{"rel/dir", true},
}

for _, test := range tests {
if ok := pattern.UnixPath.MatchString(test.String); ok != test.Valid {
t.Errorf("pattern.UnixPath.MatchString(%q) expected to be %v", test.String, test.Valid)
}
}
}

func TestAlphanumericPattern(t *testing.T) {
tests := []struct {
String string
Expand Down
20 changes: 17 additions & 3 deletions pkg/util/validate/string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package validate

import (
"io/ioutil"
"os"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -28,9 +30,21 @@ func URL(url string) bool {
return pattern.URL.MatchString(url)
}

// UnixPath validates whether a string is an unix path string.
func UnixPath(path string) bool {
return pattern.UnixPath.MatchString(path)
// Path validates whether a string is an unix path string.
func Path(path string) bool {
// Check if file already exists
if _, err := os.Stat(path); err == nil {
return true
}

// Attempt to create it
var d []byte
if err := ioutil.WriteFile(path, d, 0644); err == nil {
os.Remove(path) // And delete it
return true
}

return false
}

// Alphanumeric validates whether a string is an alphanumeric string.
Expand Down