pulumi/sdk/go/common/util/fsutil/copy.go

64 lines
1.9 KiB
Go
Raw Permalink Normal View History

2018-05-22 19:43:36 +00:00
// Copyright 2016-2018, Pulumi Corporation.
//
// 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 fsutil
import (
"fmt"
"os"
"path/filepath"
)
// CopyFile is a braindead simple function that copies a src file to a dst file. Note that it is not general purpose:
// it doesn't try to be efficient, it doesn't handle copies where src and dst overlap,
// and it makes no attempt to preserve file permissions. It is what we need for this utility package, no more, no less.
func CopyFile(dst string, src string, excl map[string]bool) error {
info, err := os.Stat(src)
Don't swallow file not found in fsutil.FileCopy (#14695) # Description `FileCopy` silently swallows file not found errors which leads to hard to debug issues. For instance, as described in #14694, tests with edit steps unexpectedly run twice against the first step. Fixes #14694 Fixes #5230 Merging this will break plenty of existing tests if they're not fixed first. [This query](https://github.com/search?q=org%3Apulumi+path%3A_test.go+%2F%5E%5Ct*Dir%3A+*%22%2F&type=code) is a rough indication. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. --> --------- Co-authored-by: Fraser Waters <fraser@pulumi.com>
2023-12-06 13:29:40 +00:00
if err != nil {
return err
} else if excl[info.Name()] {
return nil
}
if info.IsDir() {
// Recursively copy all files in a directory.
files, err := os.ReadDir(src)
if err != nil {
return fmt.Errorf("read dir: %w", err)
}
for _, file := range files {
name := file.Name()
copyerr := CopyFile(filepath.Join(dst, name), filepath.Join(src, name), excl)
if copyerr != nil {
return copyerr
}
}
} else if info.Mode().IsRegular() {
// Copy files by reading and rewriting their contents. Skip other special files.
data, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
dstdir := filepath.Dir(dst)
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
if err = os.MkdirAll(dstdir, 0o700); err != nil {
return err
}
if err = os.WriteFile(dst, data, info.Mode()); err != nil {
return err
}
}
return nil
}