Skip to content

Commit 2d413e6

Browse files
committed
Dump file metadata for "outputs"
1 parent 9022dbf commit 2d413e6

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

output.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package cwl
22

3+
import (
4+
"crypto/sha1"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/otiai10/jsonindent"
11+
)
12+
313
// Output represents and conbines "CommandOutputParameter" and "WorkflowOutputParameter"
414
// @see
515
// - http://www.commonwl.org/v1.0/CommandLineTool.html#CommandOutputParameter
@@ -89,3 +99,55 @@ func (o Outputs) Less(i, j int) bool {
8999
func (o Outputs) Swap(i, j int) {
90100
o[i], o[j] = o[j], o[i]
91101
}
102+
103+
// DumpFileMeta ...
104+
func (o Output) DumpFileMeta(dir string, w io.Writer) error {
105+
106+
dest := map[string]map[string]interface{}{}
107+
108+
switch o.Types[0].Type {
109+
case "File":
110+
for _, glob := range o.Binding.Glob {
111+
metadata, err := getFileMetaData(filepath.Join(dir, glob))
112+
if err != nil {
113+
return err
114+
}
115+
dest[o.ID] = metadata
116+
}
117+
case "stdout":
118+
metadata, err := getFileMetaData(filepath.Join(dir, o.ID))
119+
if err != nil {
120+
return err
121+
}
122+
dest[o.ID] = metadata
123+
default:
124+
return nil // do nothing
125+
}
126+
127+
return jsonindent.NewEncoder(w).Encode(dest)
128+
}
129+
130+
// getFileMetaData
131+
func getFileMetaData(targetfilepath string) (map[string]interface{}, error) {
132+
targetfile, err := os.Open(targetfilepath)
133+
if err != nil {
134+
return nil, err
135+
}
136+
defer targetfile.Close()
137+
info, err := os.Stat(targetfilepath)
138+
if err != nil {
139+
return nil, err
140+
}
141+
h := sha1.New()
142+
if _, err := io.Copy(h, targetfile); err != nil {
143+
return nil, err
144+
}
145+
return map[string]interface{}{
146+
"checksum": fmt.Sprintf("sha1$%x", string(h.Sum(nil))),
147+
"basename": filepath.Base(targetfilepath),
148+
"location": fmt.Sprintf("file://%s", targetfilepath),
149+
"path": targetfilepath,
150+
"class": "File",
151+
"size": info.Size(),
152+
}, nil
153+
}

0 commit comments

Comments
 (0)