Skip to content

Commit

Permalink
undo *cwl.File vs cwl.File
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Buchanan committed Mar 21, 2018
1 parent 03e5e96 commit 8180749
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
1 change: 0 additions & 1 deletion cmd/cwl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ func (r *runner) runWorkflow(wf *cwl.Workflow, vals cwl.Values) (cwl.Values, err
stepvals[in.ID] = link.value()
}

// TODO failing because of the cwl.File vs *cwl.File mixup
debug("STEP VALS", stepvals)
outvals, err := r.runDoc(step.Run, stepvals)
if err != nil {
Expand Down
27 changes: 14 additions & 13 deletions process/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
var ErrFileNotFound = errors.New("file not found")

type Filesystem interface {
Create(path, contents string) (*cwl.File, error)
Info(loc string) (*cwl.File, error)
Create(path, contents string) (cwl.File, error)
Info(loc string) (cwl.File, error)
Contents(loc string) (string, error)
Glob(pattern string) ([]*cwl.File, error)
Glob(pattern string) ([]cwl.File, error)
}

const MaxContentsBytes = 64 * units.Kilobyte
Expand All @@ -25,7 +25,9 @@ const MaxContentsBytes = 64 * units.Kilobyte
// such as dirname, checksum, size, etc. If f.Contents is given, the
// file will be created via fs.Create(). if `loadContents` is true,
// the file contents will be loaded via fs.Contents().
func (process *Process) resolveFile(f cwl.File, loadContents bool) (*cwl.File, error) {
func (process *Process) resolveFile(f cwl.File, loadContents bool) (cwl.File, error) {
// TODO revisit pointer to File
var x cwl.File

// http://www.commonwl.org/v1.0/CommandLineTool.html#File
// "As a special case, if the path field is provided but the location field is not,
Expand All @@ -37,16 +39,15 @@ func (process *Process) resolveFile(f cwl.File, loadContents bool) (*cwl.File, e
}

if f.Location == "" && f.Contents == "" {
return nil, errf("location and contents are empty")
return x, errf("location and contents are empty")
}

// If both location and contents are set, one will get overwritten.
// Can't know which one the caller intended, so fail instead.
if f.Location != "" && f.Contents != "" {
return nil, errf("location and contents are both non-empty")
return x, errf("location and contents are both non-empty")
}

var x *cwl.File
var err error

if f.Contents != "" {
Expand All @@ -59,26 +60,26 @@ func (process *Process) resolveFile(f cwl.File, loadContents bool) (*cwl.File, e
if path == "" {
id, err := uuid.NewRandom()
if err != nil {
return nil, errf("generating a random name for a file literal: %s", err)
return x, errf("generating a random name for a file literal: %s", err)
}
path = id.String()
}

x, err = process.fs.Create(path, f.Contents)
if err != nil {
return nil, errf("creating file from inline content: %s", err)
return x, errf("creating file from inline content: %s", err)
}

} else {
x, err = process.fs.Info(f.Location)
if err != nil {
return nil, errf("getting file info for %q: %s", f.Location, err)
return x, errf("getting file info for %q: %s", f.Location, err)
}

if loadContents {
f.Contents, err = process.fs.Contents(f.Location)
if err != nil {
return nil, errf("loading file contents: %s", err)
return x, errf("loading file contents: %s", err)
}
}
}
Expand All @@ -103,10 +104,10 @@ func (process *Process) resolveFile(f cwl.File, loadContents bool) (*cwl.File, e
f.Nameroot, f.Nameext = splitname(f.Basename)
f.Dirname = filepath.Dir(f.Path)

return &f, nil
return f, nil
}

func (process *Process) resolveSecondaryFiles(file *cwl.File, x cwl.Expression) error {
func (process *Process) resolveSecondaryFiles(file cwl.File, x cwl.Expression) error {

// cwl spec:
// "If the value is an expression, the value of self in the expression
Expand Down
30 changes: 16 additions & 14 deletions process/fs/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewLocal(workdir string) *Local {
return &Local{workdir, false}
}

func (l *Local) Glob(pattern string) ([]*cwl.File, error) {
var out []*cwl.File
func (l *Local) Glob(pattern string) ([]cwl.File, error) {
var out []cwl.File

pattern = filepath.Join(l.workdir, pattern)

Expand All @@ -43,64 +43,66 @@ func (l *Local) Glob(pattern string) ([]*cwl.File, error) {
return out, nil
}

func (l *Local) Create(path, contents string) (*cwl.File, error) {
func (l *Local) Create(path, contents string) (cwl.File, error) {
var x cwl.File
if path == "" {
return nil, errf("can't create file with empty path")
return x, errf("can't create file with empty path")
}

b := []byte(contents)
size := int64(len(b))
if units.MetricBytes(size) > process.MaxContentsBytes {
return nil, errf("contents is max allowed size (%s)", process.MaxContentsBytes)
return x, errf("contents is max allowed size (%s)", process.MaxContentsBytes)
}

loc := filepath.Join(l.workdir, path)
abs, err := filepath.Abs(loc)
if err != nil {
return nil, errf("getting absolute path for %s: %s", loc, err)
return x, errf("getting absolute path for %s: %s", loc, err)
}

return &cwl.File{
return cwl.File{
Location: abs,
Path: path,
Checksum: "sha1$" + fmt.Sprintf("%x", sha1.Sum(b)),
Size: size,
}, nil
}

func (l *Local) Info(loc string) (*cwl.File, error) {
func (l *Local) Info(loc string) (cwl.File, error) {
var x cwl.File
if !filepath.IsAbs(loc) {
loc = filepath.Join(l.workdir, loc)
}

st, err := os.Stat(loc)
if os.IsNotExist(err) {
return nil, process.ErrFileNotFound
return x, process.ErrFileNotFound
}
if err != nil {
return nil, err
return x, err
}

// TODO make this work with directories
if st.IsDir() {
return nil, errf("can't call Info() on a directory: %s", loc)
return x, errf("can't call Info() on a directory: %s", loc)
}

abs, err := filepath.Abs(loc)
if err != nil {
return nil, errf("getting absolute path for %s: %s", loc, err)
return x, errf("getting absolute path for %s: %s", loc, err)
}

checksum := ""
if l.CalcChecksum {
b, err := ioutil.ReadFile(loc)
if err != nil {
return nil, errf("calculating checksum for %s: %s", loc, err)
return x, errf("calculating checksum for %s: %s", loc, err)
}
checksum = "sha1$" + fmt.Sprintf("%x", sha1.Sum(b))
}

return &cwl.File{
return cwl.File{
Location: abs,
Path: abs,
Checksum: checksum,
Expand Down
2 changes: 1 addition & 1 deletion process/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Loop:
}

return []*Binding{
{clb, z, *f, key, nil, name},
{clb, z, f, key, nil, name},
}, nil

case cwl.DirectoryType:
Expand Down
9 changes: 3 additions & 6 deletions process/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Loop:
}
case cwl.FileType:
switch y := val.(type) {
case []*cwl.File:
case []cwl.File:
if len(y) != 1 {
continue Loop
}
Expand All @@ -154,9 +154,6 @@ Loop:
}
return f, nil

// TODO returning both pointer and non-pointer
case *cwl.File:
return y, nil
case cwl.File:
return y, nil
default:
Expand Down Expand Up @@ -197,10 +194,10 @@ Loop:

// matchFiles executes the list of glob patterns, returning a list of matched files.
// matchFiles must return a non-nil list on success, even if no files are matched.
func (process *Process) matchFiles(fs Filesystem, globs []string, loadContents bool) ([]*cwl.File, error) {
func (process *Process) matchFiles(fs Filesystem, globs []string, loadContents bool) ([]cwl.File, error) {
// it's important this slice isn't nil, because the outputEval field
// expects it to be non-null during expression evaluation.
files := []*cwl.File{}
files := []cwl.File{}

// resolve all the globs into file objects.
for _, pattern := range globs {
Expand Down

0 comments on commit 8180749

Please sign in to comment.