### Install go.yaml.in/yaml/v3 Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/go.yaml.in/yaml/v3/README.md Use 'go get' to install the YAML package for Go. This command fetches and installs the specified package and its dependencies. ```bash go get go.yaml.in/yaml/v3 ``` -------------------------------- ### Install Ginkgo Locally Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/CONTRIBUTING.md Installs the Ginkgo CLI locally using Go. Ensure Go is installed and configured. ```bash go install "./..." ``` -------------------------------- ### Create Root Logger Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-logr/logr/README.md Initialize the root logger early in your application's lifecycle. This example uses a hypothetical 'logimpl' implementation. ```go func main() { // ... other setup code ... // Create the "root" logger. We have chosen the "logimpl" implementation, // which takes some initial parameters and returns a logr.Logger. logger := logimpl.New(param1, param2) // ... other setup code ... } ``` -------------------------------- ### Ginkgo Spec Example Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/README.md An example of a Ginkgo spec demonstrating BeforeEach, When, Context, It, and SpecTimeout. It covers scenarios like checking out books from a library. ```go import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ... ) var _ = Describe("Checking books out of the library", Label("library"), func() { var library *libraries.Library var book *books.Book var valjean *users.User BeforeEach(func() { library = libraries.NewClient() book = &books.Book{ Title: "Les Miserables", Author: "Victor Hugo", } valjean = users.NewUser("Jean Valjean") }) When("the library has the book in question", func() { BeforeEach(func(ctx SpecContext) { Expect(library.Store(ctx, book)).To(Succeed()) }) Context("and the book is available", func() { It("lends it to the reader", func(ctx SpecContext) { Expect(valjean.Checkout(ctx, library, "Les Miserables")).To(Succeed()) Expect(valjean.Books()).To(ContainElement(book)) Expect(library.UserWithBook(ctx, book)).To(Equal(valjean)) }, SpecTimeout(time.Second * 5)) }) Context("but the book has already been checked out", func() { var javert *users.User BeforeEach(func(ctx SpecContext) { javert = users.NewUser("Javert") Expect(javert.Checkout(ctx, library, "Les Miserables")).To(Succeed()) }) It("tells the user", func(ctx SpecContext) { err := valjean.Checkout(ctx, library, "Les Miserables") Expect(err).To(MatchError("Les Miserables is currently checked out")) }, SpecTimeout(time.Second * 5)) It("lets the user place a hold and get notified later", func(ctx SpecContext) { Expect(valjean.Hold(ctx, library, "Les Miserables")).To(Succeed()) Expect(valjean.Holds(ctx)).To(ContainElement(book)) By("when Javert returns the book") Expect(javert.Return(ctx, library, book)).To(Succeed()) By("it eventually informs Valjean") notification := "Les Miserables is ready for pick up" Eventually(ctx, valjean.Notifications).Should(ContainElement(notification)) Expect(valjean.Checkout(ctx, library, "Les Miserables")).To(Succeed()) Expect(valjean.Books(ctx)).To(ContainElement(book)) Expect(valjean.Holds(ctx)).To(BeEmpty()) }, SpecTimeout(time.Second * 10)) }) }) When("the library does not have the book in question", func() { It("tells the reader the book is unavailable", func(ctx SpecContext) { err := valjean.Checkout(ctx, library, "Les Miserables") Expect(err).To(MatchError("Les Miserables is not in the library catalog")) }, SpecTimeout(time.Second * 5)) }) }) ``` -------------------------------- ### Convert klog.Infof to structured logger Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-logr/logr/README.md Example of converting a Kubernetes klog format string to a structured logger call with key-value pairs. Use this when migrating from klog to a structured logging system. ```go logger.Error(err, "client returned an error", "code", responseCode) ``` -------------------------------- ### Preview Documentation Changes Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/CONTRIBUTING.md Builds and serves the Ginkgo documentation locally using Jekyll. Navigate to the `docs` directory to run this command. ```bash bundle && bundle exec jekyll serve ``` -------------------------------- ### Build All Files (Old System) Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/golang.org/x/sys/unix/README.md Use this command to generate Go files for your current OS and architecture with the old build system. Ensure GOOS and GOARCH are set correctly. ```bash mkall.sh ``` -------------------------------- ### Show Build Commands (Old System) Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/golang.org/x/sys/unix/README.md Use this command with the '-n' flag to see the commands that will be run by mkall.sh without actually executing them. ```bash mkall.sh -n ``` -------------------------------- ### SecureJoin Implementation using chroot Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/cyphar/filepath-securejoin/README.md This Go code demonstrates a legacy implementation of SecureJoin using `chroot` and `readlink` on GNU/Linux systems. It is provided for historical context and legacy support, but is not recommended for new users due to potential TOCTOU vulnerabilities. ```go package securejoin import ( "os/exec" "path/filepath" ) func SecureJoin(root, unsafePath string) (string, error) { unsafePath = string(filepath.Separator) + unsafePath cmd := exec.Command("chroot", root, "readlink", "--canonicalize-missing", "--no-newline", unsafePath) output, err := cmd.CombinedOutput() if err != nil { return "", err } expanded := string(output) return filepath.Join(root, expanded), nil } ``` -------------------------------- ### System Call Interface Functions Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/golang.org/x/sys/unix/README.md These are the entry points for the hand-written assembly file that implements system call dispatch. They differ in the number of arguments they can pass to the kernel. ```go func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr) ``` ```go func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) ``` ```go func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr) ``` -------------------------------- ### Convert klog.Infof with multiple format specifiers Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-logr/logr/README.md Demonstrates converting a klog.Infof call with multiple format specifiers to a structured logger call. This pattern involves mapping format specifiers to key-value pairs. ```go logger.V(4).Info("got a retry-after response when requesting url", "attempt", retries, "after seconds", seconds, "url", url) ``` -------------------------------- ### Commit and publish a new release Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/RELEASING.md Commands to commit the version change, push to the repository, and create a GitHub release. ```bash git commit -m "vM.m.p" git push gh release create "vM.m.p" git fetch --tags origin master ``` -------------------------------- ### Check Version Against Constraints Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/Masterminds/semver/v3/README.md Use NewConstraint and NewVersion to parse inputs, then call Check to validate if a version satisfies the defined range. ```go c, err := semver.NewConstraint(">= 1.2.3") if err != nil { // Handle constraint not being parsable. } v, err := semver.NewVersion("1.3") if err != nil { // Handle version not being parsable. } // Check if the version meets the constraints. The variable a will be true. a := c.Check(v) ``` -------------------------------- ### OpenInRoot and related functions Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/cyphar/filepath-securejoin/README.md Functions for safely opening files within a root directory to prevent race conditions. ```go func OpenInRoot(root, unsafePath string) (*os.File, error) func OpenatInRoot(root *os.File, unsafePath string) (*os.File, error) func Reopen(handle *os.File, flags int) (*os.File, error) ``` ```go path, err := securejoin.SecureJoin(root, unsafePath) file, err := os.OpenFile(path, unix.O_PATH|unix.O_CLOEXEC) ``` -------------------------------- ### Vet Ginkgo Changes Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/CONTRIBUTING.md Performs static analysis on your Go code to detect suspicious constructs and potential errors. Run this before committing changes. ```bash go vet ./... ``` -------------------------------- ### Unmarshal and Marshal YAML Data in Go Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/go.yaml.in/yaml/v3/README.md Demonstrates unmarshaling YAML data into a Go struct and a map, and then marshaling them back into YAML format. Ensure struct fields are public for correct unmarshaling. ```go package main import ( "fmt" "log" "go.yaml.in/yaml/v3" ) var data = ` a: Easy! b: c: 2 d: [3, 4] ` // Note: struct fields must be public in order for unmarshal to // correctly populate the data. type T struct { A string B struct { RenamedC int `yaml:"c"` D []int `yaml:",flow"` } } func main() { t := T{} err := yaml.Unmarshal([]byte(data), &t) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- t:\n%v\n\n", t) d, err := yaml.Marshal(&t) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- t dump:\n%s\n\n", string(d)) m := make(map[interface{}]interface{}) err = yaml.Unmarshal([]byte(data), &m) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- m:\n%v\n\n", m) d, err = yaml.Marshal(&m) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- m dump:\n%s\n\n", string(d)) } ``` -------------------------------- ### MkdirAll and MkdirAllHandle functions Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/cyphar/filepath-securejoin/README.md Functions for safely creating directory structures within a root directory. ```go func MkdirAll(root, unsafePath string, mode int) error func MkdirAllHandle(root *os.File, unsafePath string, mode int) (*os.File, error) ``` ```go path, err := securejoin.SecureJoin(root, unsafePath) err = os.MkdirAll(path, mode) ``` -------------------------------- ### Run Ginkgo Specs in Parallel Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/README.md Execute Ginkgo test suites in parallel using the -p flag. This is useful for speeding up test execution on multi-core systems. ```bash ginkgo -p ``` -------------------------------- ### Run Ginkgo Tests Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/CONTRIBUTING.md Executes all tests recursively and in parallel for the Ginkgo project. This command verifies the integrity of your changes. ```bash ginkgo -r -p ``` -------------------------------- ### Load Slim-Sprig FuncMap in Go Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-task/slim-sprig/v3/README.md Load the Slim-Sprig FuncMap before parsing templates. This ensures all available template functions are registered with the Go template engine. ```go import ( "html/template" "github.com/go-task/slim-sprig" ) // This example illustrates that the FuncMap *must* be set before the // templates themselves are loaded. tpl := template.Must( template.New("base").Funcs(sprig.FuncMap()).ParseGlob("*.html") ) ``` -------------------------------- ### Directory Creation Operations Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/cyphar/filepath-securejoin/README.md Functions for safely creating directories within a restricted root directory. ```APIDOC ## MkdirAll ### Description Safely creates directories within a root path, protecting against race attacks. ### Parameters - **root** (string) - The root directory path. - **unsafePath** (string) - The path to create. - **mode** (int) - The directory permissions. ## MkdirAllHandle ### Description Similar to MkdirAll, but uses an *os.File for the root and returns an *os.File handle to the created directory. ### Parameters - **root** (*os.File) - The root directory handle. - **unsafePath** (string) - The path to create. - **mode** (int) - The directory permissions. ``` -------------------------------- ### Validate Version Against Constraint in Go Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/Masterminds/semver/v3/README.md Use the Validate method to check if a version meets a specific constraint and retrieve error messages if it fails. ```go c, err := semver.NewConstraint("<= 1.2.3, >= 1.4") if err != nil { // Handle constraint not being parseable. } v, err := semver.NewVersion("1.3") if err != nil { // Handle version not being parseable. } // Validate a version against a constraint. a, msgs := c.Validate(v) // a is false for _, m := range msgs { fmt.Println(m) // Loops over the errors which would read // "1.3 is greater than 1.2.3" // "1.3 is less than 1.4" } ``` -------------------------------- ### Sort semantic versions Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/Masterminds/semver/v3/README.md Uses the standard library sort package to order a collection of Version objects. ```go raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",} vs := make([]*semver.Version, len(raw)) for i, r := range raw { v, err := semver.NewVersion(r) if err != nil { t.Errorf("Error parsing version: %s", err) } vs[i] = v } sort.Sort(semver.Collection(vs)) ``` -------------------------------- ### Parse a semantic version Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/Masterminds/semver/v3/README.md Parses a version string into a Version object, returning an error if the format is invalid. ```go v, err := semver.NewVersion("1.2.3-beta.1+build345") ``` -------------------------------- ### Use fmt.Sprintf for complex values in structured logs Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-logr/logr/README.md Shows how to use fmt.Sprintf within a structured logger call when a format string is necessary for a specific value. This is generally discouraged but provided for cases where it's unavoidable. ```go logger.Info("unable to reflect over type", "type", fmt.Sprintf("%T")) ``` -------------------------------- ### Log Messages in Application Code Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-logr/logr/README.md Use the received logr.Logger object within your application code to emit log messages. The Info method is used here with a key-value pair. ```go func (app *appObject) Run() { app.logger.Info("starting up", "timestamp", time.Now()) // ... app code ... } ``` -------------------------------- ### Update CHANGELOG.md with recent commits Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/onsi/ginkgo/v2/RELEASING.md Generates a list of changes since the last git tag and prepends them to the CHANGELOG.md file. ```bash LAST_VERSION=$(git tag --sort=version:refname | tail -n1) CHANGES=$(git log --pretty=format:'- %s [%h]' HEAD...$LAST_VERSION) echo -e "## NEXT\n\n$CHANGES\n\n### Features\n\n### Fixes\n\n### Maintenance\n\n$(cat CHANGELOG.md)" > CHANGELOG.md ``` -------------------------------- ### File Opening Operations Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/cyphar/filepath-securejoin/README.md Functions for safely opening files within a restricted root directory to prevent security vulnerabilities. ```APIDOC ## OpenInRoot ### Description Opens a file safely within a root directory, protecting against race attacks. Returns an O_PATH file descriptor. ### Parameters - **root** (string) - The root directory path. - **unsafePath** (string) - The path to open relative to the root. ## OpenatInRoot ### Description Similar to OpenInRoot, but accepts an *os.File as the root to ensure operations occur within the same rootfs. ### Parameters - **root** (*os.File) - The root directory handle. - **unsafePath** (string) - The path to open relative to the root. ## Reopen ### Description Reopens an O_PATH file descriptor to obtain a more usable handle. ### Parameters - **handle** (*os.File) - The O_PATH file descriptor. - **flags** (int) - The flags for opening the file. ``` -------------------------------- ### Using Slim-Sprig Functions in Templates Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-task/slim-sprig/v3/README.md Call Slim-Sprig functions within Go templates using pipe syntax. Functions are lowercase by convention, following Go's idiom for template functions. ```go {{ "hello!" | upper | repeat 5 }} ``` -------------------------------- ### Pass Logger to Other Libraries Source: https://github.com/cloudfoundry/archiver/blob/main/vendor/github.com/go-logr/logr/README.md Pass the logr.Logger object to other libraries or store it in structs. No other packages need to know about the specific logging implementation. ```go app := createTheAppObject(logger) app.Run() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.