fs

package
v2.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chroot

func Chroot(path string)

func DirExists

func DirExists(filePath string) (bool, error)

func FileExists

func FileExists(filePath string) (bool, error)

func IsDir

func IsDir(path string) (bool, error)

Determine whether the given path is a folder

func RelativeToCurrentPath

func RelativeToCurrentPath(path string) (string, error)

func Root

func Root() fs.FS

func SaveFile

func SaveFile(src io.ReadCloser, dst string) error

func SaveToDisk

func SaveToDisk(fileTree fs.FS, root string) (err error)

SaveToDisk saves the file tree to disk with path root. file tree is a tree of files from disk, memory or any other places which implement fs.FS.

func Sha1

func Sha1(root fs.FS, path string) (string, error)

func ShortenFileBase

func ShortenFileBase(baseDir, fullPath string) string

ShortenFileBase removes baseDir from fullPath (but keep the last element of baseDir). e.g. ShortenFileBase("a/b", "a/b/c.go") => "b/c.go"

Example
package main

import (
	"fmt"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"

	"github.com/koderover/zadig/v2/pkg/util/fs"
)

type testParams struct {
	base, path, expectedPath string
}

var _ = Describe("Testing file", func() {

	DescribeTable("Testing ShortenFileBase",
		func(p testParams) {
			path := fs.ShortenFileBase(p.base, p.path)
			Expect(path).To(Equal(p.expectedPath))
		},
		Entry("short path", testParams{
			base:         "a",
			path:         "a/b/c.go",
			expectedPath: "a/b/c.go",
		}),
		Entry("short path with '/'", testParams{
			base:         "a/",
			path:         "a/b/c.go",
			expectedPath: "a/b/c.go",
		}),
		Entry("long path", testParams{
			base:         "a/b",
			path:         "a/b/c.go",
			expectedPath: "b/c.go",
		}),
		Entry("long path with '/'", testParams{
			base:         "a/b/",
			path:         "a/b/c.go",
			expectedPath: "b/c.go",
		}),
		Entry("longer path", testParams{
			base:         "a/d/b",
			path:         "a/d/b/c.go",
			expectedPath: "b/c.go",
		}),
		Entry("empty path", testParams{
			base:         "",
			path:         "b/c.go",
			expectedPath: "b/c.go",
		}),
		Entry("current path", testParams{
			base:         ".",
			path:         "b/c.go",
			expectedPath: "b/c.go",
		}),
		Entry("root path", testParams{
			base:         "/",
			path:         "/b/c.go",
			expectedPath: "b/c.go",
		}),
	)
})

func main() {
	fmt.Println(fs.ShortenFileBase("a", "a/b/c.go"))
	fmt.Println(fs.ShortenFileBase("a/", "a/b/c.go"))
	fmt.Println(fs.ShortenFileBase("a/b", "a/b/c.go"))
	fmt.Println(fs.ShortenFileBase("a/b/", "a/b/c.go"))
	fmt.Println(fs.ShortenFileBase("a/d/b", "a/d/b/c.go"))
	fmt.Println(fs.ShortenFileBase("", "b/c.go"))
	fmt.Println(fs.ShortenFileBase(".", "b/c.go"))
	fmt.Println(fs.ShortenFileBase("/", "/b/c.go"))

}
Output:


a/b/c.go
a/b/c.go
b/c.go
b/c.go
b/c.go
b/c.go
b/c.go
b/c.go

func Tar

func Tar(src fs.FS, dst string) error

Tar archives the src file system and saves to disk with path dst. src file system is a tree of files from disk, memory or any other places which implement fs.FS.

func Untar

func Untar(src, dst string) (err error)

Untar extracts the src tarball and saves to disk with path dst.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL