fstree

package module
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 4 Imported by: 0

README

Go Reference Tests Codecov Go Report

FSTree

FSTree is a module that works with filesystem trees driven by yaml.

It can:

  • make new filetree that described by yaml
  • check existing filetree by the given yaml description.
Installation
go get github.com/backdround/go-fstree
Example make fstree
package main

import (
	"log"
	"strings"

	"github.com/backdround/go-fstree"
)

var fstreeYaml =`
configs:
  config1.ini:
    type: file
    data: "port = 143"
  config2.yaml:
    type: file
    data: "port: 343"
pkg:
  pkg1:
    type: link
    path: ../../pkg1
`

func main() {
	fstreeYaml = strings.ReplaceAll(fstreeYaml, "\t", "  ")
	
	// Creates filesystem tree in ./project
	err := fstree.MakeOverOSFS("./project", fstreeYaml)
	
	if err != nil {
		log.Fatal(err)
	}
}

It creates the following structure:

project
├── configs
│   ├── config1.ini
│   └── config2.yaml
└── pkg
    └── pkg1 -> ../../pkg1
Example check fstree
package main

import (
	"log"
	"strings"

	"github.com/backdround/go-fstree"
)

var fstreeYaml =`
configs:
  config1.ini:
    type: file
    data: "port = 143"
  config2.yaml:
    type: file
    data: "port: 343"
pkg:
  pkg1:
    type: link
    path: ../../pkg1
`

func main() {
	fstreeYaml = strings.ReplaceAll(fstreeYaml, "\t", "  ")
	
	// checks filesystem tree in ./project
	difference, err := fstree.CheckOverOSFS("./project", fstreeYaml)
	
	if err != nil {
		log.Fatal(err)
	}

	if difference != nil {
		log.Printf("Path %q isn't corresponds to the expected tree:\n",
			difference.Path)
		log.Println(difference.Real)
	} else {
		log.Println("./project is corresponds to the given yaml")
	}
}

It checks that ./project is corresponds to the following structure:

project
├── configs
│   ├── config1.ini
│   └── config2.yaml
└── pkg
    └── pkg1 -> ../../pkg1
Yaml entries
Directory
depth0:
  # No type field is setted
  depth1:
    # No type field is setted
    depth2:
      # No type field is setted

creates ROOTPATH/depth0/depth1/depath2 directory

File
file1.txt:
  # type is required
  type: file
  # data is optinal
  data: some file data

creates file ROOTPATH/file1.txt with data some file data

link1:
  # type is reqired
  type: link
  # path is required
  path: ./some/destination

creates link ROOTPATH/link1 with destination ./some/destination

Documentation

Overview

Package fstree creates or check filesystem tree structure driven by yaml.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(fs MakerFS, rootPath string, yamlData string) error

Make makes filesystem tree in rootPath from yamlData. For example:

configs:
  "config1.txt":
    type: file
    data: "format: txt"
pkg:
  pkg1:
    type: link
    path: "../../pkg1"

The function creates:

  • ./configs/config1.txt (file with data "format: txt")
  • ./pkg/pkg1 (link points to "../../pkg1")

func MakeOverOSFS

func MakeOverOSFS(rootPath string, yamlData string) error

MakeOverOSFS makes the same thing as Make, but uses the real filesystem

Types

type CheckFS added in v1.2.0

type CheckFS interface {
	IsExist(path string) bool
	IsFile(path string) bool
	IsLink(path string) bool
	IsDirectory(path string) bool

	ReadDir(path string) ([]string, error)
	ReadFile(path string) ([]byte, error)
	Readlink(path string) (string, error)
}

CheckFS describes required interface for checking filetree. In the most cases it copies os package signatures.

type Difference added in v1.2.0

type Difference struct {
	Path        string
	Expectation string
	Real        string
}

Difference type describes specific difference between filesystem and check expectation

func Check added in v1.2.0

func Check(fs CheckFS, rootPath string, yamlData string) (*Difference, error)

Check checks filesystem tree in rootPath by yamlData. For example:

configs:
  config1.txt:
    type: file
    data: some data
pkg:
  pkg1:
    type: link
    path: "../../pkg1"

The function checks:

  • that ./configs/config1.txt is a file with data "some data"
  • that ./pkg/pkg1 is a link that points to "../../pkg1"

func CheckOverOSFS added in v1.2.0

func CheckOverOSFS(rootPath string, yamlData string) (*Difference, error)

CheckOverOSFS makes the same thing as Check, but uses the real filesystem

type MakerFS added in v1.2.0

type MakerFS interface {
	IsExist(path string) bool
	IsFile(path string) bool
	IsLink(path string) bool
	IsDirectory(path string) bool

	ReadFile(path string) ([]byte, error)
	Readlink(path string) (string, error)
	WriteFile(path string, data []byte) error
	Symlink(oldPath, newPath string) error
	Mkdir(path string) error
}

MakerFS describes required interface for making filetree. In the most cases it copies os package signatures.

Directories

Path Synopsis
Package checker makes compliance check with filesystem tree structure.
Package checker makes compliance check with filesystem tree structure.
Package config parses user config to abstract fs node structure.
Package config parses user config to abstract fs node structure.
Package maker makes filesystem tree by passed filesystem entries
Package maker makes filesystem tree by passed filesystem entries
Package osfs describes OsFS type that implements work with filesystem by os package.
Package osfs describes OsFS type that implements work with filesystem by os package.

Jump to

Keyboard shortcuts

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