2020-05-25 15:07:40 -03:00
|
|
|
package extrafiles
|
|
|
|
|
|
|
|
import (
|
2020-09-21 14:47:51 -03:00
|
|
|
"fmt"
|
2020-05-25 15:07:40 -03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2020-11-10 11:20:55 -03:00
|
|
|
"github.com/goreleaser/fileglob"
|
2021-11-22 14:53:08 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2020-05-25 15:07:40 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
2021-11-22 14:53:08 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2020-05-25 15:07:40 -03:00
|
|
|
)
|
|
|
|
|
2020-05-25 15:18:24 -03:00
|
|
|
// Find resolves extra files globs et al into a map of names/paths or an error.
|
2021-11-22 14:53:08 -03:00
|
|
|
func Find(ctx *context.Context, files []config.ExtraFile) (map[string]string, error) {
|
|
|
|
t := tmpl.New(ctx)
|
2021-04-25 14:20:49 -03:00
|
|
|
result := map[string]string{}
|
2020-05-25 15:07:40 -03:00
|
|
|
for _, extra := range files {
|
2021-11-22 14:53:08 -03:00
|
|
|
glob, err := t.Apply(extra.Glob)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("failed to apply template to glob %q: %w", extra.Glob, err)
|
|
|
|
}
|
2021-11-23 22:18:47 -03:00
|
|
|
if glob == "" {
|
|
|
|
log.Warn("ignoring empty glob")
|
|
|
|
continue
|
|
|
|
}
|
2021-11-22 14:53:08 -03:00
|
|
|
files, err := fileglob.Glob(glob)
|
2020-11-09 19:16:06 -03:00
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("globbing failed for pattern %s: %w", extra.Glob, err)
|
|
|
|
}
|
2021-11-23 22:18:47 -03:00
|
|
|
if len(files) > 1 && extra.NameTemplate != "" {
|
|
|
|
return result, fmt.Errorf("failed to add extra_file: %q -> %q: glob matches multiple files", extra.Glob, extra.NameTemplate)
|
|
|
|
}
|
2020-11-09 19:16:06 -03:00
|
|
|
for _, file := range files {
|
|
|
|
info, err := os.Stat(file)
|
|
|
|
if err == nil && info.IsDir() {
|
|
|
|
log.Debugf("ignoring directory %s", file)
|
|
|
|
continue
|
2020-05-25 15:07:40 -03:00
|
|
|
}
|
2021-11-23 22:18:47 -03:00
|
|
|
n, err := t.Apply(extra.NameTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("failed to apply template to name %q: %w", extra.NameTemplate, err)
|
|
|
|
}
|
2021-04-25 14:20:49 -03:00
|
|
|
name := filepath.Base(file)
|
2021-11-23 22:18:47 -03:00
|
|
|
if n != "" {
|
|
|
|
name = n
|
|
|
|
}
|
2020-11-09 19:16:06 -03:00
|
|
|
if old, ok := result[name]; ok {
|
|
|
|
log.Warnf("overriding %s with %s for name %s", old, file, name)
|
2020-05-25 15:07:40 -03:00
|
|
|
}
|
2020-11-09 19:16:06 -03:00
|
|
|
result[name] = file
|
2020-05-25 15:07:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|