Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikegehard committed Aug 26, 2013
1 parent 856e603 commit 3b5d096
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
24 changes: 18 additions & 6 deletions src/exercism/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ type submitRequest struct {
func FetchAssignments(host string, path string, apiKey string) (as []Assignment, err error) {
url := fmt.Sprintf("%s%s?key=%s", host, path, apiKey)
req, err := http.NewRequest("GET", url, nil)
if err != nil { return }
if err != nil {
return
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -81,19 +83,27 @@ func SubmitAssignment(host, apiKey, filePath string) (r *submitResponse, err err
url := fmt.Sprintf("%s/%s", host, path)

workingDirectory, err := os.Getwd()
if err != nil { return }
if err != nil {
return
}

fullFilePath := filepath.Join(workingDirectory, filepath.Clean(filePath))
code, err := ioutil.ReadFile(fullFilePath)

if err != nil { return }
if err != nil {
return
}

submission := submitRequest{Key: apiKey, Code: string(code), Path: filePath}
submissionJson, err := json.Marshal(submission)
if err != nil { return }
if err != nil {
return
}

req, err := http.NewRequest("POST", url, bytes.NewReader(submissionJson))
if err != nil { return }
if err != nil {
return
}

req.Header.Set("User-Agent", fmt.Sprintf("github.com/kytrinyx/exercism CLI v%s", VERSION))

Expand All @@ -105,7 +115,9 @@ func SubmitAssignment(host, apiKey, filePath string) (r *submitResponse, err err

body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil { return }
if err != nil {
return
}

if resp.StatusCode != http.StatusCreated {
postError := submitError{}
Expand Down
8 changes: 6 additions & 2 deletions src/exercism/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ type Config struct {

func ConfigFromFile(dir string) (c Config, err error) {
bytes, err := ioutil.ReadFile(configFilename(dir))
if err != nil { return }
if err != nil {
return
}

err = json.Unmarshal(bytes, &c)
return
}

func ConfigToFile(dir string, c Config) (err error) {
bytes, err := json.Marshal(c)
if err != nil { return }
if err != nil {
return
}

err = ioutil.WriteFile(configFilename(dir), bytes, 0644)
return
Expand Down
19 changes: 5 additions & 14 deletions src/exercism/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@ package exercism
import "strings"

var testExtensions = map[string]string{
"ruby": "_test.rb",
"js": ".spec.js",
"elixir": "_test.exs",
"ruby": "_test.rb",
"js": ".spec.js",
"elixir": "_test.exs",
"clojure": "_test.clj",
"python": "_test.py",
"go": "_test.go",
"python": "_test.py",
"go": "_test.go",
}

/*
:ruby => '_test.rb',
:js => '.spec.js',
:elixir => '_test.exs',
:clojure => '_test.clj',
:python => '_test.py',
:go => '_test.go',
*/

func IsTest(filename string) bool {
for _, ext := range testExtensions {
if strings.LastIndex(filename, ext) > 0 {
Expand Down

0 comments on commit 3b5d096

Please sign in to comment.