feat(cmd/zip): add command to zip configured files

This commit is contained in:
fwiedmann
2019-07-09 01:45:16 +02:00
parent 8f876fa866
commit 3ad20b2d00
3 changed files with 67 additions and 4 deletions

View 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
},
}

View File

@@ -9,6 +9,7 @@ import (
"os"
"strings"
"github.com/Nightapes/go-semantic-release/pkg/config"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
@@ -38,19 +39,41 @@ func GetAccessToken(providerName string) (string, error) {
return token, nil
}
// ZipFile compress given file in zip format
func ZipFile(repository string, file string) (string, error) {
// PrepareAssets prepare all files before uploading
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)
if err != nil {
return "", err
}
log.Debugf("Created zipfile %s", zipFile.Name())
defer zipFile.Close()
fileToZip, err := os.Open(file)
fileToZip, err := os.Open(repository + "/" + file)
if err != nil {
return "", err
}
@@ -62,6 +85,7 @@ func ZipFile(repository string, file string) (string, error) {
}
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
fileToZipHeader, err := zip.FileInfoHeader(fileToZipInfo)
if err != nil {

View File

@@ -251,3 +251,5 @@ func (s *SemanticRelease) Release(force bool) error {
return nil
}
if file.Compress {