/
submit.go
103 lines (85 loc) · 2.37 KB
/
submit.go
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
package cmd
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
"github.com/exercism/cli/paths"
"github.com/urfave/cli"
)
// Submit posts an iteration to the API.
func Submit(ctx *cli.Context) error {
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.")
}
if isREADME(filename) {
log.Fatal("You cannot submit the README as a solution.")
}
if paths.IsDir(filename) {
log.Fatal("Please specify each file that should be submitted, e.g. `exercism submit file1 file2 file3`.")
}
file, err := filepath.Abs(filename)
if err != nil {
log.Fatal(err)
}
if ctx.GlobalBool("verbose") {
log.Printf("absolute path: %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
iteration.Comment = ctx.String("comment")
client := api.NewClient(c)
submission, err := client.Submit(iteration)
if err != nil {
log.Fatal(err)
}
if len(submission.WhatNextInstructions) > 0 {
fmt.Print(submission.WhatNextInstructions)
} else {
solutionURL, _ := url.Parse(c.API)
solutionURL.Path += fmt.Sprintf("tracks/%s/exercises/%s", iteration.TrackID, iteration.Problem)
fmt.Printf("Your %s solution for %s has been submitted. View it here:\n%s\n\n", submission.Language, submission.Name, submission.URL)
fmt.Printf("See related solutions and get involved here:\n%s\n\n", solutionURL)
}
return nil
}