|
1 | 1 | package cwl
|
2 | 2 |
|
| 3 | +import ( |
| 4 | + "crypto/sha1" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/otiai10/jsonindent" |
| 11 | +) |
| 12 | + |
3 | 13 | // Output represents and conbines "CommandOutputParameter" and "WorkflowOutputParameter"
|
4 | 14 | // @see
|
5 | 15 | // - http://www.commonwl.org/v1.0/CommandLineTool.html#CommandOutputParameter
|
@@ -89,3 +99,55 @@ func (o Outputs) Less(i, j int) bool {
|
89 | 99 | func (o Outputs) Swap(i, j int) {
|
90 | 100 | o[i], o[j] = o[j], o[i]
|
91 | 101 | }
|
| 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