fs

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2021 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 added in v1.4.0

func DirExists(filePath string) (bool, error)

func FileExists

func FileExists(filePath string) (bool, error)

func RelativeToCurrentPath added in v1.5.0

func RelativeToCurrentPath(path string) (string, error)

func Root

func Root() fs.FS

func SaveFile added in v1.3.0

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

func SaveToDisk added in v1.4.0

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 added in v1.4.0

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

func ShortenFileBase added in v1.4.0

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"
	. "github.com/onsi/ginkgo/extensions/table"
	. "github.com/onsi/gomega"

	"github.com/koderover/zadig/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 added in v1.4.0

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 added in v1.4.0

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