Skip to content

Commit

Permalink
Merge branch 'fwslash/download-command'
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Nov 27, 2014
2 parents fc03444 + 4d455a7 commit 4c5fd74
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
26 changes: 26 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ func Fetch(url string) ([]*Problem, error) {
return payload.Problems, nil
}

func Download(url string) (*Submission, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

payload := &PayloadSubmission{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(payload)
if err != nil {
return nil, fmt.Errorf("error parsing API response - %s", err)
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf(`unable to fetch Submission (HTTP: %d) - %s`, res.StatusCode, payload.Error)
}

return payload.Submission, err
}

// Demo fetches the first problem in each language track.
func Demo(c *config.Config) ([]*Problem, error) {
url := fmt.Sprintf("%s/problems/demo?key=%s", c.XAPI, c.APIKey)
Expand Down
13 changes: 8 additions & 5 deletions api/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package api

// Submission is an iteration that has been submitted to the API.
type Submission struct {
URL string `json:"url"`
TrackID string `json:"track_id"`
Language string `json:"language"`
Slug string `json:"slug"`
Name string `json:"name"`
URL string `json:"url"`
TrackID string `json:"track_id"`
Language string `json:"language"`
Slug string `json:"slug"`
Name string `json:"name"`
Username string `json:"username"`
ProblemFiles map[string]string `json:"problem_files"`
SolutionFiles map[string]string `json:"solution_files"`
}
4 changes: 2 additions & 2 deletions bin/build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

set -e

go build -o out/exercism exercism/main.go

set -e
59 changes: 59 additions & 0 deletions cmd/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/codegangsta/cli"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
)

// Download returns specified submissions and related problem.
func Download(ctx *cli.Context) {
c, err := config.Read(ctx.GlobalString("config"))
if err != nil {
log.Fatal(err)
}

args := ctx.Args()

if len(args) != 1 {
msg := "Usage: exercism download SUBMISSION_ID"
log.Fatal(msg)
}

var url string
url = fmt.Sprintf("%s/api/v1/submissions/%s", c.API, args[0])

submission, err := api.Download(url)
if err != nil {
log.Fatal(err)
}

var path string

path = filepath.Join(c.Dir, "solutions", submission.Username, submission.Language, submission.Slug, args[0])

if err := os.MkdirAll(path, 0755); err != nil {
log.Fatal(err)
}

for name, contents := range submission.ProblemFiles {
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s", path, name), []byte(contents), 0755); err != nil {
log.Fatal(err)
}
}

for name, contents := range submission.SolutionFiles {
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s", path, name), []byte(contents), 0755); err != nil {
log.Fatal(err)
}
}

fmt.Printf("Successfully downloaded submission.\n\nThe submission can be viewed at:\n %s\n\n", path)

}
7 changes: 7 additions & 0 deletions exercism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
descLogout = "DEPRECATED: Clear exercism.io api credentials"

descLongRestore = "Restore will pull the latest revisions of exercises that have already been submitted. It will *not* overwrite existing files. If you have made changes to a file and have not submitted it, and you're trying to restore the last submitted version, first move that file out of the way, then call restore."
descDownload = "Downloads and saves a specified submission into the local system"
)

func main() {
Expand Down Expand Up @@ -123,6 +124,12 @@ func main() {
Usage: descTracks,
Action: cmd.Tracks,
},
{
Name: "download",
ShortName: "dl",
Usage: descDownload,
Action: cmd.Download,
},
}
if err := app.Run(os.Args); err != nil {

Expand Down

0 comments on commit 4c5fd74

Please sign in to comment.