-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathtransmission.go
More file actions
59 lines (56 loc) · 1.29 KB
/
transmission.go
File metadata and controls
59 lines (56 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package workspace
import (
"errors"
"path/filepath"
)
// Transmission is the data necessary to submit a solution.
type Transmission struct {
Files []string
Dir string
ArgDirs []string
}
// NewTransmission processes the arguments to the submit command to prep a submission.
func NewTransmission(root string, args []string) (*Transmission, error) {
tx := &Transmission{}
for _, arg := range args {
pt, err := DetectPathType(arg)
if err != nil {
return nil, err
}
if pt == TypeFile {
arg, err = filepath.Abs(arg)
if err != nil {
return nil, err
}
tx.Files = append(tx.Files, arg)
continue
}
// For our purposes, if it's not a file then it's a directory.
tx.ArgDirs = append(tx.ArgDirs, arg)
}
if len(tx.ArgDirs) > 1 {
return nil, errors.New("more than one dir")
}
if len(tx.ArgDirs) > 0 && len(tx.Files) > 0 {
return nil, errors.New("mixing files and dirs")
}
if len(tx.Files) > 0 {
ws := New(root)
parents := map[string]bool{}
for _, file := range tx.Files {
dir, err := ws.SolutionDir(file)
if err != nil {
return nil, err
}
parents[dir] = true
tx.Dir = dir
}
if len(parents) > 1 {
return nil, errors.New("files are from more than one solution")
}
}
if len(tx.ArgDirs) == 1 {
tx.Dir = tx.ArgDirs[0]
}
return tx, nil
}