giturl

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

GIT Parser

The git-parser is a package meant for parsing git urls

This package also enables listing all files based on there extension

The package currently supports only github parser and API. Feel free to contribute any other

Parser

Parse a git URL
package main

import (
	"fmt"

	giturl "github.com/armosec/go-git-url"
)

func main() {

	fullURl := "https://github.com/armosec/go-git-url"
	gitURL, err := giturl.NewGitURL(fullURl) // initialize and parse the URL
	if err != nil {
		// do something
	}

	fmt.Printf(gitURL.GetHostName())  // github.com
	fmt.Printf(gitURL.GetOwnerName()) // armosec
	fmt.Printf(gitURL.GetRepoName())  // go-git-url
}

Git API support

It is recommended to use a github token. Set the github token in the GITHUB_TOKEN env

List files and directories

// List all files and directories names
all, err := gitURL.ListAllNames()

// List all files names
files, err := gitURL.ListFilesNames()

// List all directories names
dirs, err := gitURL.ListDirsNames()

// List files names with the listed extensions
extensions := []string{"yaml", "json"}
files, err := gitURL.ListFilesNamesWithExtension(extensions)

Different URL support ->

 
basicURL, err := giturl.NewGitURL("https://github.com/armosec/go-git-url") 
 
nestedURL, err := giturl.NewGitURL("https://github.com/armosec/go-git-url/tree/master/files")  

fileApiURL, err := giturl.NewGitURL("https://github.com/armosec/go-git-url/blob/master/files/file0.json")  

fileRawURL, err := giturl.NewGitURL("https://raw.githubusercontent.com/armosec/go-git-url/master/files/file0.json") 

Download files

// Download all files
all, err := gitURL.DownloadAllFiles()

// Download all files with the listed extensions
extensions := []string{"yaml", "json"}
files, err := gitURL.DownloadFilesWithExtension(extensions)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IGitURL

type IGitURL interface {
	SetBranchName(string)
	SetOwnerName(string)
	SetPath(string)
	SetToken(string)
	SetRepoName(string)

	GetProvider() string
	GetBranchName() string
	GetOwnerName() string
	GetPath() string
	GetToken() string
	GetRepoName() string

	// parse url
	Parse(fullURL string) error

	// set default branch name using the providers git API
	SetDefaultBranchName() error

	// GetURL git url
	GetURL() *url.URL

	// ListFilesNamesWithExtension list all files in path with the desired extension. if empty will list all (including directories)
	ListFilesNamesWithExtension(extensions []string) ([]string, error)

	// ListAll list all directories and files in url tree
	ListAllNames() ([]string, error)

	// ListFilesNames list all files in url tree
	ListFilesNames() ([]string, error)

	// ListDirsNames list all directories in url tree
	ListDirsNames() ([]string, error)

	// DownloadAllFiles download files from git repo tree
	// return map of (url:file, url:error)
	DownloadAllFiles() (map[string][]byte, map[string]error)

	// DownloadFilesWithExtension download files from git repo tree based on file extension
	// return map of (url:file, url:error)
	DownloadFilesWithExtension(extensions []string) (map[string][]byte, map[string]error)

	// GetLatestCommit get latest commit
	GetLatestCommit() (*apis.Commit, error)
}

IGitURL parse git urls

func NewGitURL

func NewGitURL(fullURL string) (IGitURL, error)

NewGitURL get instance of git parser

Directories

Path Synopsis
githubparser
v1

Jump to

Keyboard shortcuts

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