go-meta/internal/pmd/pmd.go

62 lines
1.7 KiB
Go

// Copyright 2023 Michael Amann and contributors
// SPDX-License-Identifier: AGPL-3.0-only
package pmd
import (
"strings"
"go.kle.li/tools/go-meta/internal/utils"
"go.kle.li/tools/go-meta/shared"
)
type packageMetadata struct {
imd *shared.PackageMetadataInput
meta *shared.PackageMetadata
}
func NewPackageMetadata(imd *shared.PackageMetadataInput) *shared.PackageMetadata {
imdc := &shared.PackageMetadataInput{
IsTag: imd.IsTag,
Branch: strings.TrimSpace(imd.Branch),
DefaultBranch: strings.TrimSpace(imd.DefaultBranch),
Version: strings.TrimSpace(imd.Version),
CommitSha: strings.TrimSpace(imd.CommitSha),
CommitTime: strings.TrimSpace(imd.CommitTime),
}
md := packageMetadata{
imd: imdc,
meta: &shared.PackageMetadata{},
}
md.compute()
return md.meta
}
func (pmd *packageMetadata) compute() {
m := shared.PackageMetadata{
IsTag: utils.StringToBool(pmd.imd.IsTag),
Branch: utils.ValOrDefault(pmd.imd.Branch, shared.NoBranch),
DefaultBranch: utils.ValOrDefault(pmd.imd.DefaultBranch, shared.NoDefaultBranch),
GoCommitVersion: pmd.getGoDummyVersion(),
Version: utils.ValOrDefault(pmd.imd.Version, pmd.getGoDummyVersion()),
CommitSha: utils.ValOrDefault(pmd.imd.CommitSha, shared.NoCommit),
CommitTime: utils.ValOrDefault(pmd.imd.CommitTime, shared.NoTime),
}
pmd.meta = &m
}
func (p *packageMetadata) getGoDummyVersion() string {
if utils.StringIsSet(p.imd.CommitSha) && utils.StringIsSet(p.imd.CommitTime) {
ok, slug := utils.StringSlug(p.imd.CommitSha, 12)
if ok {
return "v0.0.0-" + p.imd.CommitTime + "-" + slug
}
return shared.NoVersion
}
return shared.NoVersion
}