mirror of
https://github.com/goreleaser/nfpm
synced 2024-06-02 10:46:21 +02:00
fix: Avoid panic if target is blank/dir and no packager is specified. (#179)
* fix: Avoid panic if target is blank/dir and no packager is specified. * fix: Streamline insufficient parameter error.
This commit is contained in:
parent
3a60553d97
commit
46cd8d9dda
|
@ -2,6 +2,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -58,11 +59,26 @@ func initFile(config string) error {
|
|||
return ioutil.WriteFile(config, []byte(example), 0600)
|
||||
}
|
||||
|
||||
var errInsufficientParams = errors.New("a packager must be specified if target is a directory or blank")
|
||||
|
||||
// nolint:funlen
|
||||
func doPackage(configPath, target, packager string) error {
|
||||
if packager == "" {
|
||||
fmt.Printf("guessing packager from target file extension...")
|
||||
packager = filepath.Ext(target)[1:]
|
||||
targetIsADirectory := false
|
||||
stat, err := os.Stat(target)
|
||||
if err == nil && stat.IsDir() {
|
||||
targetIsADirectory = true
|
||||
}
|
||||
|
||||
if packager == "" {
|
||||
ext := filepath.Ext(target)
|
||||
if targetIsADirectory || ext == "" {
|
||||
return errInsufficientParams
|
||||
}
|
||||
|
||||
packager = ext[1:]
|
||||
fmt.Println("guessing packager from target file extension...")
|
||||
}
|
||||
|
||||
config, err := nfpm.ParseFile(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -89,14 +105,10 @@ func doPackage(configPath, target, packager string) error {
|
|||
// if no target was specified create a package in
|
||||
// current directory with a conventional file name
|
||||
target = pkg.ConventionalFileName(info)
|
||||
} else {
|
||||
} else if targetIsADirectory {
|
||||
// if a directory was specified as target, create
|
||||
// a package with conventional file name there
|
||||
var stat os.FileInfo
|
||||
stat, err = os.Stat(target)
|
||||
if err == nil && stat.IsDir() {
|
||||
target = path.Join(target, pkg.ConventionalFileName(info))
|
||||
}
|
||||
target = path.Join(target, pkg.ConventionalFileName(info))
|
||||
}
|
||||
|
||||
f, err := os.Create(target)
|
||||
|
|
Loading…
Reference in New Issue