-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathsubmit.go
More file actions
108 lines (86 loc) · 2.09 KB
/
submit.go
File metadata and controls
108 lines (86 loc) · 2.09 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/codegangsta/cli"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
)
// Submit posts an iteration to the api
func Submit(ctx *cli.Context) {
if len(ctx.Args()) == 0 {
log.Fatal("Please enter a file name")
}
c, err := config.Read(ctx.GlobalString("config"))
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("debug") {
log.Printf("Exercises dir: %s", c.Dir)
dir, err := os.Getwd()
if err != nil {
log.Printf("Unable to get current working directory - %s", err)
} else {
log.Printf("Current dir: %s", dir)
}
}
if !c.IsAuthenticated() {
log.Fatal(msgPleaseAuthenticate)
}
filename := ctx.Args()[0]
if ctx.GlobalBool("debug") {
log.Printf("file name: %s", filename)
}
if isTest(filename) {
log.Fatal("Please submit the solution, not the test file.")
}
file, err := filepath.Abs(filename)
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("debug") {
log.Printf("absolute path: %s", file)
}
file, err = filepath.EvalSymlinks(file)
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("debug") {
log.Printf("eval symlinks (file): %s", file)
}
dir, err := filepath.EvalSymlinks(c.Dir)
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("debug") {
log.Printf("eval symlinks (dir): %s", dir)
}
code, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("Cannot read the contents of %s - %s\n", filename, err)
}
url := fmt.Sprintf("%s/api/v1/user/assignments", c.API)
iteration := &api.Iteration{
Key: c.APIKey,
Code: string(code),
File: file,
Dir: dir,
}
if err := iteration.Identify(); err != nil {
msg := `Please leave the solution within the problem directory that was created by 'exercism fetch'`
log.Fatalf("Cannot submit - %s.\n\n%s", err, msg)
}
submission, err := api.Submit(url, iteration)
if err != nil {
log.Fatal(err)
}
msg := `
Submitted %s in %s.
Your submission can be found online at %s
To get the next exercise, run "exercism fetch" again.
`
fmt.Printf(msg, submission.Name, submission.Language, submission.URL)
}