giturl

package module
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: Apache-2.0 Imports: 10 Imported by: 19

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

Parser

Supported parsers
  • GitHub
  • GitLab
  • Azure
Parse a git URL
package main

import (
	"fmt"

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

func main() {

    fullURl := "https://github.com/kubescape/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()) // kubescape
	fmt.Printf(gitURL.GetRepoName())  // go-git-url
}

Git API support

Supported APIs
  • GitHub

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

  • GitLab

It is recommended to use a GitLab token. Set the GitLab token in the GITLAB_TOKEN env

  • Azure

It is recommended to use a Azure token. Set the Azure token in the AZURE_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/kubescape/go-git-url") 
 
nestedURL, err := giturl.NewGitURL("https://github.com/kubescape/go-git-url/tree/master/files")  

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

fileRawURL, err := giturl.NewGitURL("https://raw.githubusercontent.com/kubescape/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 IGitAPI

type IGitAPI interface {
	IGitURL

	GetToken() string
	SetToken(string)

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

	// 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)
}

func NewGitAPI

func NewGitAPI(fullURL string) (IGitAPI, error)

NewGitAPI get instance of git api

type IGitURL

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

	GetHostName() string
	GetProvider() string
	GetBranchName() string
	GetOwnerName() string
	GetPath() string
	GetRepoName() string
	GetHttpCloneURL() string

	// parse url
	Parse(fullURL string) error

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

IGitURL parse git urls

func NewGitURL

func NewGitURL(fullURL string) (IGitURL, error)

NewGitURL get instance of git parser

Directories

Path Synopsis
azureparser
v1
githubparser
v1
gitlabparser
v1

Jump to

Keyboard shortcuts

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