-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctx.go
161 lines (133 loc) · 3.22 KB
/
ctx.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package pm
import (
"fmt"
"log"
"os"
"path"
"time"
"github.com/spf13/viper"
)
const (
//ManifestName ..
ManifestName = "pm"
//ManifestType ..
ManifestType = "yml"
)
const (
// ChangelogName ..
ChangelogName = "changelog"
//ChangelogType ..
ChangelogType = "yml"
)
const (
//VersionName ..
VersionName = "version"
//VersionType ..
VersionType = "yml"
)
// PMDir ... workdir
const PMDir = ".pm"
//Ctx ...
type Ctx struct {
WorkingDir, PMDir string
Out, Err *log.Logger
Manifest, Changelog, Version *viper.Viper
Author Manifest
Maintainer Manifest
}
//NewCtx ..
func NewCtx(workingDir string) *Ctx {
a := Manifest{
FileName: "author",
FileType: "yml",
}
m := Manifest{
FileName: "maintainer",
FileType: "yml",
}
return &Ctx{
Out: log.New(os.Stdout, "", 0),
Err: log.New(os.Stderr, "", 0),
PMDir: PMDir,
WorkingDir: workingDir,
Author: a,
Maintainer: m,
}
}
//PreLoad ...
func (ctx *Ctx) PreLoad() error {
if err := ctx.LoadManifest(); err != nil {
return err
}
if err := ctx.LoadVersion(); err != nil {
return err
}
if err := ctx.LoadChangelog(); err != nil {
return err
}
return nil
}
/// MANIFEST
// LoadManifest ...
func (ctx *Ctx) LoadManifest() error {
ctx.Manifest = viper.New()
ctx.Manifest.SetConfigName(ManifestName)
ctx.Manifest.SetConfigType(ManifestType)
ctx.Manifest.AddConfigPath(path.Join(ctx.WorkingDir, ctx.PMDir))
if err := ctx.Manifest.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return ctx.Manifest.SafeWriteConfig()
}
}
return nil
}
// GetManifestPath ..
func (ctx Ctx) GetManifestPath() string {
return path.Join(ctx.WorkingDir, ctx.PMDir, fmt.Sprintf("%s.%s", ManifestName, ManifestType))
}
// VERSION
//LoadVersion ..
func (ctx *Ctx) LoadVersion() error {
v := viper.New()
v.SetConfigName(VersionName)
v.SetConfigType(VersionType)
v.AddConfigPath(path.Join(ctx.WorkingDir, ctx.PMDir))
ctx.Version = v
if err := ctx.Version.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
ctx.Version.Set("version.major", 0)
ctx.Version.Set("version.minor", 1)
ctx.Version.Set("version.patch", 0)
return ctx.Version.SafeWriteConfig()
}
}
return nil
}
// GetVersionPath ..
func (ctx Ctx) GetVersionPath() string {
return path.Join(ctx.WorkingDir, ctx.PMDir, fmt.Sprintf("%s.%s", VersionName, VersionType))
}
// Changelog
//LoadChangelog ..
func (ctx *Ctx) LoadChangelog() error {
ctx.Changelog = viper.NewWithOptions(viper.KeyDelimiter("::"))
ctx.Changelog.SetConfigName(ChangelogName)
ctx.Changelog.SetConfigType(ChangelogType)
ctx.Changelog.AddConfigPath(path.Join(ctx.WorkingDir, ctx.PMDir))
if err := ctx.Changelog.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
v := [1]string{"Initialized pm (Project Metadata)"}
ctx.Changelog.Set(time.Now().Format("2006-01-02"), v)
return ctx.Changelog.SafeWriteConfig()
}
}
return nil
}
// GetChangelogPath ..
func (ctx Ctx) GetChangelogPath() string {
return path.Join(ctx.WorkingDir, ctx.PMDir, fmt.Sprintf("%s.%s", ChangelogName, ChangelogType))
}
// VERSION
// CHANGELOG
// AUTHOR
// MAINTAINER