Repository navigation
Expand file tree
/
Copy pathsubmit.go
More file actions
128 lines (105 loc) · 2.83 KB
/
Copy pathsubmit.go
File metadata and controls
128 lines (105 loc) · 2.83 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cmd
import (
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"time"
"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.New(ctx.GlobalString("config"))
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("verbose") {
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)
}
dir, err := filepath.EvalSymlinks(c.Dir)
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("verbose") {
log.Printf("eval symlinks (dir): %s", dir)
}
files := []string{}
for _, filename := range ctx.Args() {
if ctx.GlobalBool("verbose") {
log.Printf("file name: %s", filename)
}
if isTest(filename) && !ctx.Bool("test") {
log.Fatal("You're trying to submit a test file. If this is really what " +
"you want, please pass the --test flag to exercism submit.")
}
file, err := filepath.Abs(filename)
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("verbose") {
log.Printf("absolute path: %s", file)
}
file, err = filepath.EvalSymlinks(file)
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("verbose") {
log.Printf("eval symlinks (file): %s", file)
}
files = append(files, file)
}
iteration, err := api.NewIteration(dir, files)
if err != nil {
log.Fatalf("Unable to submit - %s", err)
}
iteration.Key = c.APIKey
client := api.NewClient(c)
submission, err := client.Submit(iteration)
if err != nil {
log.Fatal(err)
}
msg := `
Submitted %s in %s.
Your submission can be found online at %s
`
if submission.Iteration == 1 {
msg += `
To get the next exercise, run "exercism fetch" again.
`
rand.Seed(time.Now().UTC().UnixNano())
phrases := []string{
"For bonus points",
"Don't stop now: The fun's just begun",
"Some tips to continue",
}
msg += fmt.Sprintf("\n## %s\n", phrases[rand.Intn(len(phrases))])
msg += tips
}
fmt.Printf(msg, submission.Name, submission.Language, submission.URL)
}
const tips = `
Did you get the tests passing and the code clean? If you want to, these are some
additional things you could try:
* Remove as much duplication as you possibly can.
* Optimize for readability, even if it means introducing duplication.
* If you've removed all the duplication, do you have a lot of conditionals? Try
finding ways to reduce or remove them. How does this affect your code's
readability? Its performance?
Then please share your thoughts in a comment on the submission. Did this
experiment make the code better? Worse? Did you learn anything from it?
`