-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for including VCS metadata in the module manifest when publishing a module. For #3034. Signed-off-by: Roger Peppe <rogpeppe@gmail.com> Change-Id: Ia4066eff076988a4f1f4666238fd068443687233 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193705 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
- Loading branch information
Showing
3 changed files
with
134 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package modregistry | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Metadata holds extra information that can be associated with | ||
// a module. It is stored in the module's manifest inside | ||
// the annotations field. All fields must JSON-encode to | ||
// strings. | ||
type Metadata struct { | ||
VCSType string `json:"org.cuelang.vcs-type"` | ||
VCSCommit string `json:"org.cuelang.vcs-commit"` | ||
VCSCommitTime time.Time `json:"org.cuelang.vcs-commit-time"` | ||
} | ||
|
||
func newMetadataFromAnnotations(annotations map[string]string) (*Metadata, error) { | ||
// TODO if this ever turned out to be a bottleneck we could | ||
// improve performance by avoiding the round-trip through JSON. | ||
raw, err := json.Marshal(annotations) | ||
if err != nil { | ||
// Should never happen. | ||
return nil, err | ||
} | ||
var m Metadata | ||
if err := json.Unmarshal(raw, &m); err != nil { | ||
return nil, err | ||
} | ||
return &m, nil | ||
} | ||
|
||
func (m *Metadata) annotations() (map[string]string, error) { | ||
// The "is-empty" checks don't work for time.Time | ||
// so check explicitly. | ||
if m.VCSCommitTime.IsZero() { | ||
return nil, fmt.Errorf("no commit time in metadata") | ||
} | ||
// TODO if this ever turned out to be a bottleneck we could | ||
// improve performance by avoiding the round-trip through JSON. | ||
data, err := json.Marshal(m) | ||
if err != nil { | ||
// Should never happen. | ||
return nil, err | ||
} | ||
var annotations map[string]string | ||
if err := json.Unmarshal(data, &annotations); err != nil { | ||
// Should never happen. | ||
return nil, err | ||
} | ||
for field, val := range annotations { | ||
if val == "" { | ||
return nil, fmt.Errorf("empty metadata value for field %q", field) | ||
} | ||
} | ||
return annotations, nil | ||
} |