Skip to content

Commit

Permalink
When check a file is handler or not, should take extension account, (#…
Browse files Browse the repository at this point in the history
…433)

otherewise it might be a directory, not a file
  • Loading branch information
metrue committed Dec 26, 2019
2 parents a968999 + 0047e66 commit aefb449
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion fx.go
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/urfave/cli"
)

const version = "0.8.82"
const version = "0.8.83"

func init() {
go checkForUpdate()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -39,7 +39,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.6.1
github.com/stretchr/testify v1.4.0
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/ugorji/go v1.1.7 // indirect
github.com/urfave/cli v1.22.2
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 // indirect
Expand Down
4 changes: 2 additions & 2 deletions packer/packer.go
Expand Up @@ -70,7 +70,7 @@ func Pack(output string, input ...string) error {
return err
}

if isHandler(path) {
if isHandler(path, language) {
if err := copy.Copy(input[0], path); err != nil {
return err
}
Expand All @@ -84,7 +84,7 @@ func Pack(output string, input ...string) error {
return nil
}

if !hasFxHandleFile(input...) {
if !hasFxHandleFile(language, input...) {
msg := `it requires a fx handle file when input is not a single file function, e.g.
fx.go for Golang
Fx.java for Java
Expand Down
20 changes: 13 additions & 7 deletions packer/rules.go
Expand Up @@ -23,12 +23,18 @@ var ExtLangMapping = map[string]string{
".pl": "perl",
}

func isHandler(name string) bool {
func isHandler(name string, lang string) bool {
basename := filepath.Base(name)
nameWithoutExt := strings.TrimSuffix(basename, filepath.Ext(basename))
return nameWithoutExt == "fx" ||
nameWithoutExt == "Fx" || // Fx is for Java
nameWithoutExt == "mod" // mod.rs is for Rust
if ExtLangMapping[filepath.Ext(basename)] != lang {
return false
}

return (nameWithoutExt == "fx" ||
// Fx is for Java
nameWithoutExt == "Fx" ||
// mod.rs is for Rust)
nameWithoutExt == "mod")
}

func langFromFileName(fileName string) (string, error) {
Expand All @@ -44,10 +50,10 @@ func langFromFileName(fileName string) (string, error) {
return lang, nil
}

func hasFxHandleFile(input ...string) bool {
func hasFxHandleFile(lang string, input ...string) bool {
var handleFile string
for _, file := range input {
if utils.IsRegularFile(file) && isHandler(file) {
if utils.IsRegularFile(file) && isHandler(file, lang) {
handleFile = file
break
} else if utils.IsDir(file) {
Expand All @@ -56,7 +62,7 @@ func hasFxHandleFile(input ...string) bool {
return err
}

if utils.IsRegularFile(path) && isHandler(info.Name()) {
if utils.IsRegularFile(path) && isHandler(info.Name(), lang) {
handleFile = path
}
return nil
Expand Down

0 comments on commit aefb449

Please sign in to comment.