tree

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2018 License: MIT Imports: 10 Imported by: 0

README

tree CircleCI Go Report Card MIT Licensed

Simple go project to tree structure. It supports only go files.

???

alt text

package main

import (
	"fmt"
	"go/ast"

	"github.com/andream16/tree"
)

func main() {

	out, err := tree.Get("examples/example", &tree.Node{})
	if err != nil {
		panic(err)
	}

	fmt.Println(out.Name)                   // example
	fmt.Println(out.Leafs[0].Name)          // somefile.go
	fmt.Println(out.Leafs[0].Path)          // examples/example/somefile.go
	fmt.Println(out.Nodes[0].Name)          // example/subexample
	fmt.Println(out.Nodes[0].Leafs[0].Name) // someotherfile.go
	fmt.Println(out.Nodes[0].Leafs[0].Path) // examples/example/subexample/someotherfile.go

	err = out.Leafs[0].Ast()
	if err != nil {
		panic(err)
	}

	ast.Inspect(out.Leafs[0].SyntaxTree, func(n ast.Node) bool {
		var s string
		switch x := n.(type) {
		case *ast.BasicLit:
			s = x.Value
		case *ast.Ident:
			s = x.Name
		}
		if s != "" {
			fmt.Printf("%s\n", s) // example
		}
		return true
	})

	// example
	// |       somefile.go
	// example/subexample
	// |       someotherfile.go
	out.Print()

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Leaf

type Leaf struct {
	Name       string
	Path       string
	SyntaxTree *ast.File
}

Leaf represents a go file.

func (*Leaf) Ast added in v1.0.1

func (l *Leaf) Ast() error

Ast sets leaf's SyntaxTree

type Node

type Node struct {
	Name  string
	Nodes []*Node
	Leafs []Leaf
}

Node represents a package (directory) that can contain other sub-packages.

func Get

func Get(path string, node *Node) (*Node, error)

Get returns all go files in the repository by directory name

func (*Node) Print added in v1.0.1

func (n *Node) Print()

Print pretty prints go project structure

type Treer added in v1.0.1

type Treer interface {
	Get(string, *Node) (*Node, error)
	Ast(*Leaf) error
}

Treer allows third party mocking

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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