|
| 1 | +// Copyright 2022 The Hugoreleaser Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package archiveplugin |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/gohugoio/hugoreleaser-plugins-api/model" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + // Version gets incremented on incompatible changes to the archive plugin or its runtime, |
| 25 | + // think of this as a major version increment in semver terms. |
| 26 | + // This should almost never happen, but if it does, the old archive plugin will probably not work as expected. |
| 27 | + // This will be detected on Hugoreleaser startup and the build will fail. |
| 28 | + // The plugin server then needs to be updated and re-tested. |
| 29 | + Version = 0 |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + _ model.Initializer = (*ArchiveFile)(nil) |
| 34 | + _ model.Initializer = (*Request)(nil) |
| 35 | +) |
| 36 | + |
| 37 | +// Request is what is sent to an external archive tool. |
| 38 | +type Request struct { |
| 39 | + // Version is the archive plugin version. |
| 40 | + // This is just used for validation on startup. |
| 41 | + Version int `toml:"version"` |
| 42 | + |
| 43 | + // Heartbeat is a string that is echoed back to the caller, |
| 44 | + // used to test that plugin servers are up and running. |
| 45 | + Heartbeat string `toml:"heartbeat"` |
| 46 | + |
| 47 | + // BuildInfo holds the basic build information about the current build. |
| 48 | + BuildInfo model.BuildInfo `toml:"build_info"` |
| 49 | + |
| 50 | + // Settings for the archive. |
| 51 | + // This is the content of archive_settings.custom_settings. |
| 52 | + Settings map[string]any `toml:"settings"` |
| 53 | + |
| 54 | + Files []ArchiveFile `toml:"files"` |
| 55 | + |
| 56 | + // Filename with extension. |
| 57 | + OutFilename string `toml:"out_filename"` |
| 58 | +} |
| 59 | + |
| 60 | +// HeartbeatResponse returns a Response that, if the second return value is true, |
| 61 | +// will be returned to the caller. |
| 62 | +func (r Request) HeartbeatResponse() (Response, bool) { |
| 63 | + if r.Heartbeat == "" { |
| 64 | + return Response{}, false |
| 65 | + } |
| 66 | + var err *model.Error |
| 67 | + if r.Version != Version { |
| 68 | + err = &model.Error{Msg: fmt.Sprintf("archive plugin version mismatch: client sent %d, server is at %d", r.Version, Version)} |
| 69 | + } |
| 70 | + return Response{Heartbeat: r.Heartbeat, Error: err}, true |
| 71 | +} |
| 72 | + |
| 73 | +func (r *Request) Init() error { |
| 74 | + what := "archive_request" |
| 75 | + if r.OutFilename == "" { |
| 76 | + return fmt.Errorf("%s: archive request has no output filename", what) |
| 77 | + } |
| 78 | + for i := range r.Files { |
| 79 | + f := &r.Files[i] |
| 80 | + if err := f.Init(); err != nil { |
| 81 | + return fmt.Errorf("%s: %v", what, err) |
| 82 | + } |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// Response is what is sent back from an external archive tool. |
| 88 | +type Response struct { |
| 89 | + // Heartbeat is a string that is echoed back to the caller, |
| 90 | + // used to test that plugin servers are up and running. |
| 91 | + Heartbeat string `toml:"heartbeat"` |
| 92 | + |
| 93 | + Error *model.Error `toml:"err"` |
| 94 | +} |
| 95 | + |
| 96 | +func (r Response) Err() error { |
| 97 | + if r.Error == nil { |
| 98 | + // Make sure that resp.Err() == nil. |
| 99 | + return nil |
| 100 | + } |
| 101 | + return r.Error |
| 102 | +} |
| 103 | + |
| 104 | +type ArchiveFile struct { |
| 105 | + // The source filename. |
| 106 | + SourcePathAbs string `toml:"source_path_abs"` |
| 107 | + |
| 108 | + // Relative target path, including the name of the file. |
| 109 | + TargetPath string `toml:"target_path"` |
| 110 | +} |
| 111 | + |
| 112 | +func (a *ArchiveFile) Init() error { |
| 113 | + return nil |
| 114 | +} |
0 commit comments