fs

package
v0.0.0-...-15bed1d Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsPath

func AbsPath(path string) string

func BaseDir

func BaseDir() fs.FS

func ChBaseDir

func ChBaseDir(path string) (err error)

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

	"github.com/asppj/appserver-template/pkg/utils/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