File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package api
22
3+ import (
4+ "fmt"
5+ "io/ioutil"
6+ "os"
7+ "path/filepath"
8+ )
9+
310type Problem struct {
411 ID string `json:"id"`
512 TrackID string `json:"track_id"`
@@ -10,6 +17,36 @@ type Problem struct {
1017 Files map [string ]string `json:"files"`
1118}
1219
20+ func (p * Problem ) String () {
21+ fmt .Sprintf ("%s - %s in %s" , p .ID , p .Name , p .Language )
22+ }
23+
24+ func (p * Problem ) ExistsIn (dir string ) bool {
25+ path := fmt .Sprintf ("%s/%s" , dir , p .ID )
26+
27+ if _ , err := os .Stat (path ); err != nil && os .IsNotExist (err ) {
28+ return false
29+ }
30+ return true
31+ }
32+
1333func (p * Problem ) Save (dir string ) error {
34+ if p .ExistsIn (dir ) {
35+ return nil
36+ }
37+
38+ for name , text := range p .Files {
39+ file := fmt .Sprintf ("%s/%s/%s" , dir , p .ID , name )
40+
41+ err := os .MkdirAll (filepath .Dir (file ), 0755 )
42+ if err != nil {
43+ return err
44+ }
45+
46+ err = ioutil .WriteFile (file , []byte (text ), 0644 )
47+ if err != nil {
48+ return err
49+ }
50+ }
1451 return nil
1552}
You can’t perform that action at this time.
0 commit comments