mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.0 KiB
75 lines
2.0 KiB
/*
|
|
Copyright The Helm Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package action
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const defaultDirectoryPermission = 0755
|
|
|
|
// WriteManifestToFile writes the manifest data to a file in the output directory.
|
|
// If appendData is true, the data will be appended to an existing file; otherwise, a new file is created.
|
|
func WriteManifestToFile(outputDir string, name string, data string, appendData bool) error {
|
|
outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator))
|
|
|
|
err := ensureDirectoryForFile(outfileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
f, err := createOrOpenFile(outfileName, appendData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = fmt.Fprintf(f, "---\n# Source: %s\n%s\n", name, data)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("wrote %s\n", outfileName)
|
|
return nil
|
|
}
|
|
|
|
// createOrOpenFile creates a new file or opens an existing file for appending.
|
|
func createOrOpenFile(filename string, appendData bool) (*os.File, error) {
|
|
if appendData {
|
|
return os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
|
|
}
|
|
return os.Create(filename)
|
|
}
|
|
|
|
// ensureDirectoryForFile checks if the directory exists to create file. Creates it if it doesn't exist.
|
|
func ensureDirectoryForFile(file string) error {
|
|
baseDir := filepath.Dir(file)
|
|
_, err := os.Stat(baseDir)
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
return err
|
|
}
|
|
|
|
return os.MkdirAll(baseDir, defaultDirectoryPermission)
|
|
}
|