aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/github/filesystem.go
blob: 3f069b4b9ec0ba53f707a49e4dcfed976b16f3e5 (plain)
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
package github

import (
	"fmt"
	"net/url"
	"os"
	"path"
	"strings"
)

func listReleasesByTagName(dst *url.URL) ([]string, error) {
	entries, err := os.ReadDir(dst.Path)
	if err != nil {
		return nil, err
	}

	var tagNames []string
	for _, entry := range entries {
		tagNames = append(tagNames, entry.Name())
	}

	return tagNames, nil
}

// The path which project release assets are saved.
func localReleaseFilePath(dst *url.URL, tagName string) string {
	return path.Join(dst.Path, tagName)
}

func releaseName(tagName string) string {
	version := tagName
	if strings.HasPrefix(version, "v") {
		version = strings.TrimLeft(version, "v")
	}

	return version
}

// The source filename for a Github release.
//
// # The source code URL provided by Github's API only references the tag name
//
// for the release.  To make it useful for users, we rename to file to include
// the project name as their website does.
func releaseSourceFileName(project string, tagName string, ext string) string {
	return fmt.Sprintf("%s-%s.%s", project, releaseName(tagName), ext)
}

func removeRelease(dst *url.URL, tagName string) error {
	fp := localReleaseFilePath(dst, tagName)
	return os.RemoveAll(fp)
}

func isFileExist(fp string) (bool, error) {
	_, err := os.Stat(fp)
	if os.IsNotExist(err) {
		return false, nil
	} else if err != nil {
		return false, err
	}

	return true, nil
}