fs.go (1436B)
1 package hub 2 3 import ( 4 "io/fs" 5 "path" 6 "strconv" 7 "strings" 8 ) 9 10 type FS struct { 11 Client *Client 12 Owner string 13 Repo string 14 } 15 16 func (fsys *FS) Open(name string) (fs.File, error) { 17 if !fs.ValidPath(name) { 18 return nil, &fs.PathError{"open", name, fs.ErrInvalid} 19 } 20 name = path.Clean(name) 21 if strings.Contains(name, "\\") { 22 return nil, fs.ErrNotExist 23 } 24 25 if name == "." { 26 return &fakeFile{ 27 Client: fsys.Client, 28 typ: ftypeRoot, 29 owner: fsys.Owner, 30 repo: fsys.Repo, 31 }, nil 32 } 33 34 elems := strings.Split(name, "/") 35 issueNum, err := strconv.Atoi(elems[0]) 36 if err != nil { 37 return nil, &fs.PathError{"open", name, fs.ErrNotExist} 38 } 39 f := &fakeFile{ 40 Client: fsys.Client, 41 owner: fsys.Owner, 42 repo: fsys.Repo, 43 number: issueNum, 44 } 45 46 switch len(elems) { 47 case 1: 48 // /123 49 _, err = fsys.Client.CheckIssue(fsys.Owner, fsys.Repo, issueNum) 50 if err != nil { 51 return nil, err 52 } 53 f.typ = ftypeIssueDir 54 case 2: 55 // /123/issue 56 // /123/comments 57 switch elems[1] { 58 case "issue": 59 f.typ = ftypeIssue 60 case "comments": 61 f.typ = ftypeCommentDir 62 default: 63 return nil, &fs.PathError{"open", name, fs.ErrNotExist} 64 } 65 case 3: 66 // /123/comments/69 67 num, err := strconv.Atoi(elems[2]) 68 if err != nil { 69 return nil, &fs.PathError{"open", name, fs.ErrNotExist} 70 } 71 f.typ = ftypeComment 72 f.number = num 73 default: 74 return nil, &fs.PathError{"open", name, fs.ErrNotExist} 75 } 76 return f, nil 77 }