You've already forked go-semantic-release
feat(cmd/zip): add command to zip configured files
This commit is contained in:
37
cmd/go-semantic-release/commands/zip.go
Normal file
37
cmd/go-semantic-release/commands/zip.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Nightapes/go-semantic-release/pkg/semanticrelease"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(zipCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var zipCmd = &cobra.Command{
|
||||||
|
Use: "zip",
|
||||||
|
Short: "Zip configured artifact from release config",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
config, err := cmd.Flags().GetString("config")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
repository, err := cmd.Flags().GetString("repository")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := semanticrelease.New(readConfig(config), repository)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = s.ZipFiles(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
@@ -38,19 +39,41 @@ func GetAccessToken(providerName string) (string, error) {
|
|||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ZipFile compress given file in zip format
|
// PrepareAssets prepare all files before uploading
|
||||||
func ZipFile(repository string, file string) (string, error) {
|
func PrepareAssets(repository string, assets []config.Asset) ([]*string, error) {
|
||||||
|
filesToUpload := []*string{}
|
||||||
|
for _, asset := range assets {
|
||||||
|
if asset.Compress {
|
||||||
|
log.Debugf("Asset %s will now be compressed", asset.Name)
|
||||||
|
log.Debugf("Repo url %s", repository)
|
||||||
|
zipNameWithPath, err := zipFile(repository, asset.Name)
|
||||||
|
if err != nil {
|
||||||
|
return filesToUpload, err
|
||||||
|
}
|
||||||
|
filesToUpload = append(filesToUpload, &zipNameWithPath)
|
||||||
|
} else {
|
||||||
|
tmpFileName := fmt.Sprintf("%s/%s", repository, asset.Name)
|
||||||
|
filesToUpload = append(filesToUpload, &tmpFileName)
|
||||||
|
}
|
||||||
|
log.Debugf("Add asset %s to files to upload", asset.Name)
|
||||||
|
}
|
||||||
|
return filesToUpload, nil
|
||||||
|
}
|
||||||
|
|
||||||
zipFileName := fmt.Sprintf("%s/%s", strings.TrimSuffix(repository, "/"), file)
|
// ZipFile compress given file in zip format
|
||||||
|
func zipFile(repository string, file string) (string, error) {
|
||||||
|
|
||||||
|
zipFileName := fmt.Sprintf("%s/%s.zip", strings.TrimSuffix(repository, "/"), file)
|
||||||
zipFile, err := os.Create(zipFileName)
|
zipFile, err := os.Create(zipFileName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
log.Debugf("Created zipfile %s", zipFile.Name())
|
||||||
|
|
||||||
defer zipFile.Close()
|
defer zipFile.Close()
|
||||||
|
|
||||||
fileToZip, err := os.Open(file)
|
fileToZip, err := os.Open(repository + "/" + file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -62,6 +85,7 @@ func ZipFile(repository string, file string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
zipWriter := zip.NewWriter(zipFile)
|
zipWriter := zip.NewWriter(zipFile)
|
||||||
|
defer zipWriter.Close()
|
||||||
|
|
||||||
fileToZipHeader, err := zip.FileInfoHeader(fileToZipInfo)
|
fileToZipHeader, err := zip.FileInfoHeader(fileToZipInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -251,3 +251,5 @@ func (s *SemanticRelease) Release(force bool) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if file.Compress {
|
||||||
|
|||||||
Reference in New Issue
Block a user