mirror

Mirror free and open-source projects you like with minimal effort
git clone git://git.server.ky/slackcoder/mirror
Log | Files | Refs | README

filesystem.go (1420B)


      1 package github
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 	"path"
      7 	"strings"
      8 
      9 	"git.server.ky/slackcoder/mirror/internal"
     10 )
     11 
     12 func listReleasesByTagName(dst *internal.URL) ([]string, error) {
     13 	entries, err := os.ReadDir(dst.Path)
     14 	if err != nil {
     15 		return nil, err
     16 	}
     17 
     18 	var tagNames []string
     19 	for _, entry := range entries {
     20 		tagNames = append(tagNames, entry.Name())
     21 	}
     22 
     23 	return tagNames, nil
     24 }
     25 
     26 // The path which project release assets are saved.
     27 func localReleaseFilePath(dst *internal.URL, tagName string) string {
     28 	return path.Join(dst.Path, tagName)
     29 }
     30 
     31 func releaseName(tagName string) string {
     32 	version := tagName
     33 	if strings.HasPrefix(version, "v") {
     34 		version = strings.TrimLeft(version, "v")
     35 	}
     36 
     37 	return version
     38 }
     39 
     40 // The source filename for a Github release.
     41 //
     42 // # The source code URL provided by Github's API only references the tag name
     43 //
     44 // for the release.  To make it useful for users, we rename to file to include
     45 // the project name as their website does.
     46 func releaseSourceFileName(project string, tagName string, ext string) string {
     47 	return fmt.Sprintf("%s-%s.%s", project, releaseName(tagName), ext)
     48 }
     49 
     50 func removeRelease(dst *internal.URL, tagName string) error {
     51 	fp := localReleaseFilePath(dst, tagName)
     52 	return os.RemoveAll(fp)
     53 }
     54 
     55 func isFileExist(fp string) (bool, error) {
     56 	_, err := os.Stat(fp)
     57 	if os.IsNotExist(err) {
     58 		return false, nil
     59 	} else if err != nil {
     60 		return false, err
     61 	}
     62 
     63 	return true, nil
     64 }