|
| 1 | +package deploywp |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/jinzhu/copier" |
| 5 | + "github.com/newclarity/scribeHelpers/toolRuntime" |
| 6 | + "github.com/newclarity/scribeHelpers/toolTypes" |
| 7 | + "github.com/newclarity/scribeHelpers/ux" |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +const DefaultDestinationBasePath = "/tmp/deploywp" |
| 12 | + |
| 13 | +type Destination struct { |
| 14 | + Files Files `json:"files"` |
| 15 | + Paths Paths `json:"paths"` |
| 16 | + Providers Providers `json:"providers"` // mapstructure:",squash"` |
| 17 | + Targets Targets `json:"targets"` // mapstructure:",squash"` |
| 18 | + |
| 19 | + AbsPaths Paths |
| 20 | + AbsFiles Files |
| 21 | + |
| 22 | + selectedHost *Host // Should be set to the selected host_name from arg[0] |
| 23 | + selectedHostName string // Should be set to the selected host_name from arg[0] |
| 24 | + |
| 25 | + Valid bool |
| 26 | + runtime *toolRuntime.TypeRuntime |
| 27 | + state *ux.State |
| 28 | +} |
| 29 | + |
| 30 | +func (d *Destination) New(runtime *toolRuntime.TypeRuntime) *Destination { |
| 31 | + runtime = runtime.EnsureNotNil() |
| 32 | + return &Destination{ |
| 33 | + Files: *((*Files).New(&Files{}, runtime)), |
| 34 | + Paths: *((*Paths).New(&Paths{}, runtime)), |
| 35 | + Providers: *((*Providers).New(&Providers{}, runtime)), |
| 36 | + Targets: *((*Targets).New(&Targets{}, runtime)), |
| 37 | + AbsPaths: *((*Paths).New(&Paths{}, runtime)), |
| 38 | + AbsFiles: *((*Files).New(&Files{}, runtime)), |
| 39 | + |
| 40 | + Valid: true, |
| 41 | + runtime: runtime, |
| 42 | + state: ux.NewState(runtime.CmdName, runtime.Debug), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (d *Destination) IsNil() *ux.State { |
| 47 | + if state := ux.IfNilReturnError(d); state.IsError() { |
| 48 | + return state |
| 49 | + } |
| 50 | + d.state = d.state.EnsureNotNil() |
| 51 | + return d.state |
| 52 | +} |
| 53 | + |
| 54 | +func (d *Destination) IsValid() bool { |
| 55 | + if state := ux.IfNilReturnError(d); state.IsError() { |
| 56 | + return false |
| 57 | + } |
| 58 | + for range onlyOnce { |
| 59 | + if d.Files.IsNotValid() { |
| 60 | + d.state = d.Files.state |
| 61 | + d.Valid = false |
| 62 | + break |
| 63 | + } |
| 64 | + if d.Paths.IsNotValid() { |
| 65 | + d.state = d.Paths.state |
| 66 | + d.Valid = false |
| 67 | + break |
| 68 | + } |
| 69 | + if d.Providers.IsNotValid() { |
| 70 | + //d.state = d.Providers.state |
| 71 | + d.Valid = false |
| 72 | + break |
| 73 | + } |
| 74 | + if d.Targets.IsNotValid() { |
| 75 | + //d.state = d.Revisions.state |
| 76 | + d.Valid = false |
| 77 | + break |
| 78 | + } |
| 79 | + |
| 80 | + if d.AbsPaths.IsNotValid() { |
| 81 | + d.state = d.AbsPaths.state |
| 82 | + d.Valid = false |
| 83 | + break |
| 84 | + } |
| 85 | + if d.AbsFiles.IsNotValid() { |
| 86 | + d.state = d.AbsFiles.state |
| 87 | + d.Valid = false |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + d.Valid = true |
| 92 | + } |
| 93 | + return d.Valid |
| 94 | +} |
| 95 | +func (d *Destination) IsNotValid() bool { |
| 96 | + return !d.IsValid() |
| 97 | +} |
| 98 | + |
| 99 | +func (d *Destination) Process() *ux.State { |
| 100 | + if state := d.IsNil(); state.IsError() { |
| 101 | + return state |
| 102 | + } |
| 103 | + |
| 104 | + for range onlyOnce { |
| 105 | + err := copier.Copy(&d.AbsPaths, &d.Paths) |
| 106 | + d.state.SetError(err) |
| 107 | + if d.state.IsError() { |
| 108 | + break |
| 109 | + } |
| 110 | + |
| 111 | + d.state = d.Providers.Process(d.runtime) |
| 112 | + if d.state.IsError() { |
| 113 | + break |
| 114 | + } |
| 115 | + |
| 116 | + d.state = d.Targets.Process(d.runtime) |
| 117 | + if d.state.IsError() { |
| 118 | + break |
| 119 | + } |
| 120 | + |
| 121 | + if d.Paths.BasePath == "" { |
| 122 | + d.Paths.BasePath = DefaultDestinationBasePath |
| 123 | + } |
| 124 | + //d.state = d.Paths.GetBasePath(d.Repository.GetUrlAsDir()) |
| 125 | + selectedProvider := d.Providers.GetByName(d.selectedHost.Provider) |
| 126 | + d.state = d.Paths.AppendBasePath(d.selectedHost.HostName, selectedProvider.Meta.SiteName) |
| 127 | + if d.state.IsError() { |
| 128 | + break |
| 129 | + } |
| 130 | + |
| 131 | + d.AbsPaths.BasePath = d.Paths.BasePath |
| 132 | + d.state = d.AbsPaths.ExpandPaths() |
| 133 | + d.state.SetError(err) |
| 134 | + if d.state.IsError() { |
| 135 | + break |
| 136 | + } |
| 137 | + |
| 138 | + d.AbsFiles.Copy.Append(&d.Files.Copy) |
| 139 | + d.AbsFiles.Delete.Append(&d.Files.Delete) |
| 140 | + d.AbsFiles.Exclude.Append(&d.Files.Exclude) |
| 141 | + d.AbsFiles.Keep.Append(&d.Files.Keep) |
| 142 | + |
| 143 | + d.state = d.AbsFiles.Process(d.AbsPaths) |
| 144 | + if d.state.IsError() { |
| 145 | + break |
| 146 | + } |
| 147 | + |
| 148 | + d.state = d.Files.Process(d.Paths) |
| 149 | + if d.state.IsError() { |
| 150 | + break |
| 151 | + } |
| 152 | + |
| 153 | + d.Valid = true |
| 154 | + } |
| 155 | + |
| 156 | + return d.state |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +// //////////////////////////////////////////////////////////////////////////////// |
| 161 | +// Files |
| 162 | +func (d *Destination) GetFiles(ftype string) *FilesArray { |
| 163 | + var ret *FilesArray |
| 164 | + if state := d.IsNil(); state.IsError() { |
| 165 | + return &FilesArray{} |
| 166 | + } |
| 167 | + |
| 168 | + for range onlyOnce { |
| 169 | + ret = d.Files.GetFiles(ftype) |
| 170 | + } |
| 171 | + |
| 172 | + return ret |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +// //////////////////////////////////////////////////////////////////////////////// |
| 177 | +// Paths |
| 178 | +func (d *Destination) GetPaths(abs ...interface{}) *Paths { |
| 179 | + var ret *Paths |
| 180 | + if state := d.IsNil(); state.IsError() { |
| 181 | + return &Paths{} |
| 182 | + } |
| 183 | + |
| 184 | + for range onlyOnce { |
| 185 | + if toolTypes.ReflectBoolArg(abs) { |
| 186 | + ret = &d.AbsPaths |
| 187 | + break |
| 188 | + } |
| 189 | + |
| 190 | + ret = &d.Paths |
| 191 | + } |
| 192 | + |
| 193 | + return ret |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | +func (d *Destination) GetBasePath() string { |
| 198 | + if state := d.IsNil(); state.IsError() { |
| 199 | + return "" |
| 200 | + } |
| 201 | + return d.Paths.BasePath |
| 202 | +} |
| 203 | + |
| 204 | + |
| 205 | +// //////////////////////////////////////////////////////////////////////////////// |
| 206 | +// Providers |
| 207 | +func (d *Destination) GetProviderByName(provider string) *Provider { |
| 208 | + var ret *Provider |
| 209 | + if state := d.IsNil(); state.IsError() { |
| 210 | + return ret |
| 211 | + } |
| 212 | + return d.Providers.GetByName(provider) |
| 213 | +} |
| 214 | +func (d *Destination) GetProviderBySiteId(siteId string) *Provider { |
| 215 | + var ret *Provider |
| 216 | + if state := d.IsNil(); state.IsError() { |
| 217 | + return ret |
| 218 | + } |
| 219 | + ret = d.Providers.GetBySiteId(siteId) |
| 220 | + return ret |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | +// //////////////////////////////////////////////////////////////////////////////// |
| 225 | +// Revisions |
| 226 | +func (d *Destination) GetTargetByHost(host string) *Target { |
| 227 | + var ret *Target |
| 228 | + if state := d.IsNil(); state.IsError() { |
| 229 | + return &Target{state: state} |
| 230 | + } |
| 231 | + ret = d.Targets.GetByHost(host) |
| 232 | + return ret |
| 233 | +} |
| 234 | + |
| 235 | +func (d *Destination) GetTargetByName(ref string) *Target { |
| 236 | + var ret *Target |
| 237 | + if state := d.IsNil(); state.IsError() { |
| 238 | + return &Target{state: state} |
| 239 | + } |
| 240 | + ret = d.Targets.GetByRefName(ref) |
| 241 | + return ret |
| 242 | +} |
| 243 | + |
| 244 | +//func (d *Destination) SelectHost(host string) *ux.State { |
| 245 | +// if state := d.IsNil(); state.IsError() { |
| 246 | +// return state |
| 247 | +// } |
| 248 | +// |
| 249 | +// for range onlyOnce { |
| 250 | +// d.selectedHost = d.GetTargetByHost(host) |
| 251 | +// d.selectedHostName = host |
| 252 | +// } |
| 253 | +// |
| 254 | +// return d.state |
| 255 | +//} |
| 256 | + |
| 257 | + |
| 258 | +func (d *Destination) GetSelectedHost() *Host { |
| 259 | + if state := d.IsNil(); state.IsError() { |
| 260 | + return &Host{state: state} |
| 261 | + } |
| 262 | + return d.selectedHost |
| 263 | +} |
| 264 | + |
| 265 | + |
| 266 | +func (d *Destination) SetDestinationHost(host *Host) *ux.State { |
| 267 | + if state := d.IsNil(); state.IsError() { |
| 268 | + return state |
| 269 | + } |
| 270 | + |
| 271 | + for range onlyOnce { |
| 272 | + if host == nil { |
| 273 | + d.state.SetError("Selected host is nil.") |
| 274 | + break |
| 275 | + } |
| 276 | + |
| 277 | + d.selectedHost = host |
| 278 | + d.selectedHostName = host.HostName |
| 279 | + |
| 280 | + d.state.SetOk() |
| 281 | + } |
| 282 | + |
| 283 | + return d.state |
| 284 | +} |
0 commit comments