-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (76 loc) · 1.46 KB
/
config.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package swagfs
import (
"encoding/json"
"fmt"
"io"
"github.com/a-kataev/swagfs/files"
"github.com/a-kataev/swagfs/internal/tmpl"
)
type Layout string
const (
BaseLayout = "BaseLayout"
StandaloneLayout = "StandaloneLayout"
)
type URL struct {
URL string `json:"url"`
Name string `json:"name,omitempty"`
}
type Config struct {
Urls []URL `json:"urls"`
Layout Layout `json:"layout"`
}
var _ tmpl.File = (*Config)(nil)
func defaultConfig() *Config {
return &Config{
Urls: []URL{
{
URL: "/api/v1/swagger.yaml",
Name: "v1",
},
},
Layout: BaseLayout,
}
}
func NewConfig() *Config {
return defaultConfig()
}
func (c *Config) Filename() string {
return files.ConfigName
}
func (c *Config) Template() string {
return string(files.ConfigData())
}
func (c *Config) TagFunc(w io.Writer, tag string) (int, error) {
switch tag {
case "layout":
return w.Write([]byte(c.Layout))
case "urls":
if c.Layout == BaseLayout {
return w.Write([]byte("null"))
}
b, err := json.Marshal(c.Urls)
if err != nil {
return 0, fmt.Errorf("urls: %v", err)
}
return w.Write(b)
case "url":
if c.Layout == BaseLayout {
if len(c.Urls) > 0 {
return w.Write([]byte(c.Urls[0].URL))
}
}
return 0, nil
}
return 0, nil
}
func (c *Config) AddURL(url, name string) *Config {
c.Urls = append(c.Urls, URL{
URL: url,
Name: name,
})
return c
}
func (c *Config) SetLayout(layout Layout) *Config {
c.Layout = layout
return c
}