Debian Codegraph @

tpl/os/os.go

raw
1 // Copyright 2017 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 os provides template functions for interacting with the operating
15 // system.
16 package os
17
18 import (
19 "errors"
20 "fmt"
21 _os "os"
22 "path/filepath"
23
24 "github.com/bep/overlayfs"
25 "github.com/gohugoio/hugo/common/herrors"
26 "github.com/gohugoio/hugo/common/hugio"
27 "github.com/gohugoio/hugo/deps"
28 "github.com/gohugoio/hugo/hugofs"
29 "github.com/spf13/afero"
30 "github.com/spf13/cast"
31 )
32
33 // New returns a new instance of the os-namespaced template functions.
34 func New(d *deps.Deps) *Namespace {
35 var readFileFs, workFs *hugofs.DropSymlinksFs
36
37 // The docshelper script does not have or need all the dependencies set up.
38 if d.PathSpec != nil {
39 readFileFs = hugofs.NewDropSymlinksFs(overlayfs.New(overlayfs.Options{
40 Fss: []afero.Fs{
41 d.PathSpec.BaseFs.Work,
42 d.PathSpec.BaseFs.Content.Fs,
43 },
44 }))
45 // See #9599
46 workFs = hugofs.NewDropSymlinksFs(d.PathSpec.BaseFs.WorkDir)
47 }
48
49 return &Namespace{
50 readFileFs: readFileFs,
51 workFs: workFs,
52 deps: d,
53 }
54 }
55
56 // Namespace provides template functions for the "os" namespace.
57 type Namespace struct {
58 readFileFs *hugofs.DropSymlinksFs
59 workFs *hugofs.DropSymlinksFs
60 deps *deps.Deps
61 }
62
63 // Getenv retrieves the value of the environment variable named by the key.
64 // It returns the value, which will be empty if the variable is not present.
65 func (ns *Namespace) Getenv(key any) (string, error) {
66 skey, err := cast.ToStringE(key)
67 if err != nil {
68 return "", nil
69 }
70
71 if err = ns.deps.ExecHelper.Sec().CheckAllowedGetEnv(skey); err != nil {
72 return "", err
73 }
74
75 return _os.Getenv(skey), nil
76 }
77
78 // readFile reads the file named by filename in the given filesystem
79 // and returns the contents as a string.
80 func readFile(fs afero.Fs, filename string) (string, error) {
81 filename = filepath.Clean(filename)
82 if filename == "" || filename == "." || filename == string(_os.PathSeparator) {
83 return "", errors.New("invalid filename")
84 }
85
86 b, err := hugio.ReadFile(fs, filename)
87 if err != nil {
88 return "", err
89 }
90
91 return string(b), nil
92 }
93
94 // ReadFile reads the file named by filename relative to the configured WorkingDir.
95 // It returns the contents as a string.
96 // There is an upper size limit set at 1 megabytes.
97 func (ns *Namespace) ReadFile(i any) (string, error) {
98 s, err := cast.ToStringE(i)
99 if err != nil {
100 return "", err
101 }
102
103 if ns.deps.PathSpec != nil {
104 s = ns.deps.PathSpec.RelPathify(s)
105 }
106
107 s, err = readFile(ns.readFileFs, s)
108 if err != nil && herrors.IsNotExist(err) {
109 return "", nil
110 }
111 return s, err
112 }
113
114 // ReadDir lists the directory contents relative to the configured WorkingDir.
115 func (ns *Namespace) ReadDir(i any) ([]_os.FileInfo, error) {
116 path, err := cast.ToStringE(i)
117 if err != nil {
118 return nil, err
119 }
120
121 list, err := afero.ReadDir(ns.workFs, path)
122 if err != nil {
123 if herrors.IsNotExist(err) {
124 return nil, nil
125 }
126 return nil, fmt.Errorf("failed to read directory %q: %s", path, err)
127 }
128
129 return list, nil
130 }
131
132 // FileExists checks whether a file exists under the given path.
133 func (ns *Namespace) FileExists(i any) (bool, error) {
134 path, err := cast.ToStringE(i)
135 if err != nil {
136 return false, err
137 }
138
139 if path == "" {
140 return false, nil
141 }
142
143 status, err := afero.Exists(ns.readFileFs, path)
144 if err != nil {
145 return false, err
146 }
147
148 return status, nil
149 }
150
151 // Stat returns the os.FileInfo structure describing file.
152 func (ns *Namespace) Stat(i any) (_os.FileInfo, error) {
153 path, err := cast.ToStringE(i)
154 if err != nil {
155 return nil, err
156 }
157
158 if path == "" {
159 return nil, nil
160 }
161
162 r, err := ns.readFileFs.Stat(path)
163 if err != nil {
164 if herrors.IsNotExist(err) {
165 return nil, nil
166 }
167 return nil, err
168 }
169
170 return r, nil
171 }