-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpackage.go
More file actions
64 lines (54 loc) · 1.68 KB
/
package.go
File metadata and controls
64 lines (54 loc) · 1.68 KB
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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package portal
import (
"fmt"
"strings"
"time"
)
type Builds struct {
Builds []Build `json:"builds"`
}
type Build struct {
Version string `json:"version"`
Date time.Time `json:"date"`
Hash string `json:"hash"`
Artifacts []Artifact `json:"artifacts"`
Internal bool `json:"internal"`
}
type Artifact struct {
Md5Sum string `json:"md5sum"`
Filename string `json:"filename"`
Name string `json:"name"`
}
func (b *Build) GetBuildForDownload(filename string) (Build, error) {
for _, a := range b.Artifacts {
if a.Filename != filename {
continue
}
// Generate identical build but with only the matching artifact
build := *b
build.Artifacts = []Artifact{
a,
}
return build, nil
}
return Build{}, fmt.Errorf("artifact not found: %s", filename)
}
// BuildPackageFilename generates the standard package filename for a given build
// Format: {version}-{shortHash}-{filename}
// Hash is truncated to 10 characters, version slashes are replaced with dashes.
func (b *Build) BuildPackageFilename(filename string) string {
return BuildPackageFilenameFromParts(b.Version, b.Hash, filename)
}
// BuildPackageFilenameFromParts generates the standard package filename from individual parts
// Format: {version}-{shortHash}-{filename}
// Hash is truncated to 10 characters, version slashes are replaced with dashes.
func BuildPackageFilenameFromParts(version, hash, filename string) string {
shortHash := hash
if len(shortHash) > 10 {
shortHash = shortHash[:10]
}
sanitizedVersion := strings.ReplaceAll(version, "/", "-")
return sanitizedVersion + "-" + shortHash + "-" + filename
}