Skip to content

Commit

Permalink
Improve error when we cannot determine content directory in "hugo new"
Browse files Browse the repository at this point in the history
See #9166
  • Loading branch information
bep committed Nov 15, 2021
1 parent 08552a7 commit b815545
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 5 additions & 1 deletion create/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ func NewContent(h *hugolib.HugoSites, kind, targetPath string) error {
cf := hugolib.NewContentFactory(h)

if kind == "" {
kind = cf.SectionFromFilename(targetPath)
var err error
kind, err = cf.SectionFromFilename(targetPath)
if err != nil {
return err
}
}

b := &contentBuilder{
Expand Down
17 changes: 10 additions & 7 deletions hugolib/content_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,28 @@ func (f ContentFactory) AppplyArchetypeTemplate(w io.Writer, p page.Page, archet

}

func (f ContentFactory) SectionFromFilename(filename string) string {
func (f ContentFactory) SectionFromFilename(filename string) (string, error) {
filename = filepath.Clean(filename)
rel, _ := f.h.AbsProjectContentDir(filename)
if rel == "" {
return ""
rel, _, err := f.h.AbsProjectContentDir(filename)
if err != nil {
return "", err
}

parts := strings.Split(helpers.ToSlashTrimLeading(rel), "/")
if len(parts) < 2 {
return ""
return "", nil
}
return parts[0]
return parts[0], nil
}

// CreateContentPlaceHolder creates a content placeholder file inside the
// best matching content directory.
func (f ContentFactory) CreateContentPlaceHolder(filename string) (string, error) {
filename = filepath.Clean(filename)
_, abs := f.h.AbsProjectContentDir(filename)
_, abs, err := f.h.AbsProjectContentDir(filename)
if err != nil {
return "", err
}

// This will be overwritten later, just write a placholder to get
// the paths correct.
Expand Down
10 changes: 5 additions & 5 deletions hugolib/filesystems/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (b *BaseFs) RelContentDir(filename string) string {

// AbsProjectContentDir tries to construct a filename below the most
// relevant content directory.
func (b *BaseFs) AbsProjectContentDir(filename string) (string, string) {
func (b *BaseFs) AbsProjectContentDir(filename string) (string, string, error) {
isAbs := filepath.IsAbs(filename)
for _, dir := range b.SourceFilesystems.Content.Dirs {
meta := dir.Meta()
Expand All @@ -141,14 +141,14 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string) {
}
if isAbs {
if strings.HasPrefix(filename, meta.Filename) {
return strings.TrimPrefix(filename, meta.Filename), filename
return strings.TrimPrefix(filename, meta.Filename), filename, nil
}
} else {
contentDir := strings.TrimPrefix(strings.TrimPrefix(meta.Filename, meta.BaseDir), filePathSeparator)
if strings.HasPrefix(filename, contentDir) {
relFilename := strings.TrimPrefix(filename, contentDir)
absFilename := filepath.Join(meta.Filename, relFilename)
return relFilename, absFilename
return relFilename, absFilename, nil
}
}

Expand All @@ -162,12 +162,12 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string) {
for i := len(contentDirs) - 1; i >= 0; i-- {
meta := contentDirs[i].Meta()
if meta.Module == "project" {
return filename, filepath.Join(meta.Filename, filename)
return filename, filepath.Join(meta.Filename, filename), nil
}
}
}

return "", ""
return "", "", errors.Errorf("could not determine content directory for %q", filename)
}

// ResolveJSConfigFile resolves the JS-related config file to a absolute
Expand Down

0 comments on commit b815545

Please sign in to comment.