Skip to content

Commit

Permalink
Merge pull request #190 from smacker/fix_workdirs_cmd
Browse files Browse the repository at this point in the history
Fix workdirs sub-command without active workdir
  • Loading branch information
smacker authored Aug 6, 2019
2 parents defe990 + 4ecd39b commit a644447
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
25 changes: 23 additions & 2 deletions cmd/sourced/cmd/workdirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"

"github.com/src-d/sourced-ce/cmd/sourced/compose/workdir"
)
Expand All @@ -21,13 +22,17 @@ func (c *workdirsCmd) Execute(args []string) error {
return err
}

// active directory not necessary exists
var activePath = ""
active, err := workdirHandler.Active()
if err != nil {
if err == nil {
activePath = active.Path
} else if !isNotExist(err) {
return err
}

for _, wd := range wds {
if wd.Path == active.Path {
if wd.Path == activePath {
fmt.Printf("* %s\n", wd.Name)
} else {
fmt.Printf(" %s\n", wd.Name)
Expand All @@ -40,3 +45,19 @@ func (c *workdirsCmd) Execute(args []string) error {
func init() {
rootCmd.AddCommand(&workdirsCmd{})
}

func isNotExist(err error) bool {
if os.IsNotExist(err) {
return true
}

if cause, ok := err.(causer); ok {
return isNotExist(cause.Cause())
}

return false
}

type causer interface {
Cause() error
}
10 changes: 5 additions & 5 deletions cmd/sourced/compose/workdir/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (h *Handler) Active() (*Workdir, error) {

resolvedPath, err := filepath.EvalSymlinks(path)
if os.IsNotExist(err) {
return nil, ErrMalformed.New("active", err)
return nil, ErrMalformed.Wrap(err, "active")
}

return h.builder.build(resolvedPath)
Expand All @@ -105,7 +105,7 @@ func (h *Handler) List() ([]*Workdir, error) {
})

if os.IsNotExist(err) {
return nil, ErrMalformed.New(h.workdirsPath, err)
return nil, ErrMalformed.Wrap(err, h.workdirsPath)
}

if err != nil {
Expand All @@ -131,16 +131,16 @@ func (h *Handler) List() ([]*Workdir, error) {
func (h *Handler) Validate(w *Workdir) error {
pointedDir, err := filepath.EvalSymlinks(w.Path)
if err != nil {
return ErrMalformed.New(w.Path, "is not a directory")
return ErrMalformed.Wrap(fmt.Errorf("is not a directory"), w.Path)
}

if info, err := os.Lstat(pointedDir); err != nil || !info.IsDir() {
return ErrMalformed.New(pointedDir, "is not a directory")
return ErrMalformed.Wrap(fmt.Errorf("is not a directory"), pointedDir)
}

for _, f := range RequiredFiles {
if !hasContent(pointedDir, f) {
return ErrMalformed.New(pointedDir, fmt.Sprintf("%s not found", f))
return ErrMalformed.Wrap(fmt.Errorf("%s not found", f), pointedDir)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sourced/compose/workdir/workdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
OptionalFiles = []string{"docker-compose.override.yml"}

// ErrMalformed is the returned error when the workdir is wrong
ErrMalformed = goerrors.NewKind("workdir %s is not valid: %s")
ErrMalformed = goerrors.NewKind("workdir %s is not valid")
)

// Type defines the type of the workdir
Expand Down

0 comments on commit a644447

Please sign in to comment.