-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_format.go
80 lines (69 loc) · 1.82 KB
/
project_format.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
package view
import (
"context"
"encoding/json"
"errors"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/kyoh86/gogh/v2"
)
type ProjectFormat interface {
Format(p gogh.Project) (string, error)
}
type ProjectFormatFunc func(gogh.Project) (string, error)
func (f ProjectFormatFunc) Format(p gogh.Project) (string, error) {
return f(p)
}
var ProjectFormatFullFilePath = ProjectFormatFunc(func(p gogh.Project) (string, error) {
return p.FullFilePath(), nil
})
var ProjectFormatRelPath = ProjectFormatFunc(func(p gogh.Project) (string, error) {
return p.RelPath(), nil
})
var ProjectFormatRelFilePath = ProjectFormatFunc(func(p gogh.Project) (string, error) {
return p.RelFilePath(), nil
})
var ProjectFormatURL = ProjectFormatFunc(func(p gogh.Project) (string, error) {
utxt, err := gogh.GetDefaultRemoteURLFromLocalProject(context.Background(), p)
if err != nil {
if errors.Is(err, git.ErrRemoteNotFound) {
utxt = "https://" + p.RelPath()
} else {
return "", err
}
}
return utxt, nil
})
var ProjectFormatJSON = ProjectFormatFunc(func(p gogh.Project) (string, error) {
utxt, err := ProjectFormatURL(p)
if err != nil {
return "", err
}
buf, _ := json.Marshal(map[string]interface{}{
"fullFilePath": p.FullFilePath(),
"relFilePath": p.RelFilePath(),
"url": utxt,
"relPath": p.RelPath(),
"host": p.Host(),
"owner": p.Owner(),
"name": p.Name(),
})
return string(buf), nil
})
func ProjectFormatFields(s string) ProjectFormat {
return ProjectFormatFunc(func(p gogh.Project) (string, error) {
utxt, err := gogh.GetDefaultRemoteURLFromLocalProject(context.Background(), p)
if err != nil {
return "", err
}
return strings.Join([]string{
p.FullFilePath(),
p.RelFilePath(),
utxt,
p.RelPath(),
p.Host(),
p.Owner(),
p.Name(),
}, s), nil
})
}