Categorygithub.com/magodo/ghwalk
modulepackage
0.0.0-20200930074045-b9a34d077a8b
Repository: https://github.com/magodo/ghwalk.git
Documentation: pkg.go.dev

# README

ghwalk is a Go library to walk a Github repository path similar to filepath.Walk().

Example

Basic

package main

import (
	"context"
	"fmt"

	"github.com/magodo/ghwalk"
)

func main() {
	ghwalk.Walk(context.TODO(), "magodo", "ghwalk", "testdata", nil,
		func(path string, info *ghwalk.FileInfo, err error) error {
			if err != nil {
				return err
			}

			// repo root will be called with nil info
			if info == nil {
				return nil
			}

			fmt.Printf("%s\n", path)
			return nil
		})
}

Output:

testdata
testdata/a
testdata/b
testdata/dir
testdata/dir/c
testdata/link_dir

Enable File Only Info

This will introduce extra API invocations on file blob, so the example below asks for a Github access token to avoid hitting ratelimit:

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/magodo/ghwalk"
)

var sep = strings.Repeat("=", 20)

func main() {
	token := os.Args[1]
	if err := ghwalk.Walk(context.TODO(), "magodo", "ghwalk", "testdata",
		&ghwalk.WalkOptions{
			Token:              token,
			EnableFileOnlyInfo: true,
		},
		func(path string, info *ghwalk.FileInfo, err error) error {
			if err != nil {
				return err
			}

			// repo root will be called with nil info
			if info == nil {
				return nil
			}

			switch info.Type {
			case ghwalk.FileTypeDir:
				fmt.Printf("%s\n%s (%s)\n%s\n", sep, info.Path, info.Type, sep)
			case ghwalk.FileTypeFile:
				content, _ := info.GetContent()
				if len(content) > 50 {
					content = content[:50] + "\n..."
				}
				fmt.Printf("%s\n%s (%s)\n%s\n%s\n", sep, info.Path, info.Type, sep, content)
			case ghwalk.FileTypeSymlink:
				fmt.Printf("%s\n%s -> %s (%s)\n%s\n", sep, info.Path, *info.FileOnlyInfo.Target, info.Type, sep)
			}
			return nil
		}); err != nil {
		log.Fatal(err)
	}
}

Output:

====================
testdata (dir)
====================
====================
testdata/a (file)
====================
content of a

====================
testdata/b (file)
====================
content of b

====================
testdata/dir (dir)
====================
====================
testdata/dir/c (file)
====================
content of c in dir

====================
testdata/link_dir -> dir (symlink)
====================

# Packages

No description provided by the author

# Functions

Walk walks the github repository tree, calling walkFn for each file or directory in the tree, including the path specified.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Variables

SkipDir is used as a return value from WalkFuncs to indicate that the directory named in the call is to be skipped.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
PathFilterFunc allows users to filter a file/directory before sending any Github API to retrieve its metadata, if it returns true.
WalkFunc is the type of the function called for each file or directory visited by Walk.