github.com/schumacherfm/hugo@v0.47.1/hugofs/fs.go (about)

     1  // Copyright 2016 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package hugofs provides the file systems used by Hugo.
    15  package hugofs
    16  
    17  import (
    18  	"github.com/gohugoio/hugo/config"
    19  	"github.com/spf13/afero"
    20  )
    21  
    22  // Os points to an Os Afero file system.
    23  var Os = &afero.OsFs{}
    24  
    25  // Fs abstracts the file system to separate source and destination file systems
    26  // and allows both to be mocked for testing.
    27  type Fs struct {
    28  	// Source is Hugo's source file system.
    29  	Source afero.Fs
    30  
    31  	// Destination is Hugo's destination file system.
    32  	Destination afero.Fs
    33  
    34  	// Os is an OS file system.
    35  	// NOTE: Field is currently unused.
    36  	Os afero.Fs
    37  
    38  	// WorkingDir is a read-only file system
    39  	// restricted to the project working dir.
    40  	WorkingDir *afero.BasePathFs
    41  }
    42  
    43  // NewDefault creates a new Fs with the OS file system
    44  // as source and destination file systems.
    45  func NewDefault(cfg config.Provider) *Fs {
    46  	fs := &afero.OsFs{}
    47  	return newFs(fs, cfg)
    48  }
    49  
    50  // NewMem creates a new Fs with the MemMapFs
    51  // as source and destination file systems.
    52  // Useful for testing.
    53  func NewMem(cfg config.Provider) *Fs {
    54  	fs := &afero.MemMapFs{}
    55  	return newFs(fs, cfg)
    56  }
    57  
    58  // NewFrom creates a new Fs based on the provided Afero Fs
    59  // as source and destination file systems.
    60  // Useful for testing.
    61  func NewFrom(fs afero.Fs, cfg config.Provider) *Fs {
    62  	return newFs(fs, cfg)
    63  }
    64  
    65  func newFs(base afero.Fs, cfg config.Provider) *Fs {
    66  	return &Fs{
    67  		Source:      base,
    68  		Destination: base,
    69  		Os:          &afero.OsFs{},
    70  		WorkingDir:  getWorkingDirFs(base, cfg),
    71  	}
    72  }
    73  
    74  func getWorkingDirFs(base afero.Fs, cfg config.Provider) *afero.BasePathFs {
    75  	workingDir := cfg.GetString("workingDir")
    76  
    77  	if workingDir != "" {
    78  		return afero.NewBasePathFs(afero.NewReadOnlyFs(base), workingDir).(*afero.BasePathFs)
    79  	}
    80  
    81  	return nil
    82  }