/
models.go
161 lines (127 loc) · 3.88 KB
/
models.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 main
import (
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/streamingfast/cli"
"github.com/streamingfast/cli/sflags"
"go.uber.org/zap/zapcore"
)
type GlobalModel struct {
Owner string
Project string
Binary string
Language Language
License string
Variant Variant
Root string
ConfigRoot string
WorkingDirectory string
}
func (g *GlobalModel) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("owner", g.Owner)
encoder.AddString("project", g.Project)
encoder.AddString("binary", g.Binary)
encoder.AddString("language", g.Language.String())
encoder.AddString("license", g.License)
encoder.AddString("variant", g.Variant.String())
encoder.AddString("config_root", g.ConfigRoot)
encoder.AddString("working_directory", g.WorkingDirectory)
return nil
}
func mustGetGlobal(cmd *cobra.Command) *GlobalModel {
global := &GlobalModel{
Owner: sflags.MustGetString(cmd, "owner"),
Project: sflags.MustGetString(cmd, "project"),
Binary: sflags.MustGetString(cmd, "binary"),
Language: mustGetLanguage(cmd),
License: sflags.MustGetString(cmd, "license"),
Variant: mustGetVariant(cmd),
Root: sflags.MustGetString(cmd, "root"),
}
global.WorkingDirectory = cli.WorkingDirectory()
if global.Root != "" {
global.WorkingDirectory = cli.AbsolutePath(global.Root)
}
if global.Project == "" {
global.Project = filepath.Base(global.WorkingDirectory)
}
if global.Binary == "" {
global.Binary = global.Project
}
global.ConfigRoot = findSfreleaserDir(global.WorkingDirectory)
return global
}
func (g *GlobalModel) ResolveFile(in string) string {
if filepath.IsAbs(in) {
return in
}
return filepath.Join(g.ConfigRoot, in)
}
func (g *GlobalModel) ensureValidForBuild() {
g.ensureValidForRelease()
if g.Language != LanguageGolang {
cli.Quit(`'sfreleaser build' only works for Go projects at the moment, sorry!`)
}
}
func (g *GlobalModel) ensureValidForRelease() {
var errors []string
if g.Language == LanguageUnset {
errors = append(errors, `You must specify for which language you are building via flag ("--language"), config file ("global.language" in ".sfreleaser" file) or environment variable ("SFRELEASER_GLOBAL_LANGUAGE")`)
}
if g.Variant == VariantUnset {
errors = append(errors, `You must specify for which variant you are building for via flag ("--variant"), config file ("global.variant" in ".sfreleaser" file) or environment variable ("SFRELEASER_GLOBAL_VARIANT")`)
}
if len(errors) != 0 {
cli.Quit(strings.Join(errors, "\n"))
}
}
type BuildModel struct {
Version string
All bool
Platforms []string
}
func (m *BuildModel) populate(cmd *cobra.Command) {
m.All = sflags.MustGetBool(cmd, "all")
m.Platforms = sflags.MustGetStringArray(cmd, "platform")
for i, platform := range m.Platforms {
m.Platforms[i] = strings.Replace(strings.ToLower(platform), "/", "-", 1)
}
}
type ReleaseModel struct {
Version string
Brew *BrewReleaseModel
// Rust is populated only if config if of type Rust
Rust *RustReleaseModel
}
func (m *ReleaseModel) populate(cmd *cobra.Command, language Language) {
m.Brew = &BrewReleaseModel{
Disabled: sflags.MustGetBool(cmd, "brew-disabled"),
TapRepo: sflags.MustGetString(cmd, "brew-tap-repo"),
}
switch language {
case LanguageGolang:
// Nothing
case LanguageRust:
m.Rust = &RustReleaseModel{}
m.Rust.CargoPublishArgs = unquotedFlatten(sflags.MustGetString(cmd, "rust-cargo-publish-args"))
m.Rust.Crates = sflags.MustGetStringArray(cmd, "rust-crates")
default:
cli.Quit("unhandled language %q", language)
}
}
type RustReleaseModel struct {
CargoPublishArgs []string
Crates []string
}
type GitHubReleaseModel struct {
AllowDirty bool
EnvFilePath string
GoreleaseConfigPath string
GoreleaserImageID string
ReleaseNotesPath string
}
type BrewReleaseModel struct {
Disabled bool
TapRepo string
}