github.com/fighterlyt/hugo@v0.47.1/hugofs/language_composite_fs.go (about) 1 // Copyright 2018 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 15 16 import ( 17 "github.com/spf13/afero" 18 ) 19 20 var ( 21 _ afero.Fs = (*languageCompositeFs)(nil) 22 _ afero.Lstater = (*languageCompositeFs)(nil) 23 ) 24 25 type languageCompositeFs struct { 26 *afero.CopyOnWriteFs 27 } 28 29 // NewLanguageCompositeFs creates a composite and language aware filesystem. 30 // This is a hybrid filesystem. To get a specific file in Open, Stat etc., use the full filename 31 // to the target filesystem. This information is available in Readdir, Stat etc. via the 32 // special LanguageFileInfo FileInfo implementation. 33 func NewLanguageCompositeFs(base afero.Fs, overlay *LanguageFs) afero.Fs { 34 return afero.NewReadOnlyFs(&languageCompositeFs{afero.NewCopyOnWriteFs(base, overlay).(*afero.CopyOnWriteFs)}) 35 } 36 37 // Open takes the full path to the file in the target filesystem. If it is a directory, it gets merged 38 // using the language as a weight. 39 func (fs *languageCompositeFs) Open(name string) (afero.File, error) { 40 f, err := fs.CopyOnWriteFs.Open(name) 41 if err != nil { 42 return nil, err 43 } 44 45 fu, ok := f.(*afero.UnionFile) 46 if ok { 47 // This is a directory: Merge it. 48 fu.Merger = LanguageDirsMerger 49 } 50 return f, nil 51 }