Skip to content

Commit

Permalink
GetGitignore initial build
Browse files Browse the repository at this point in the history
Basic go program with readme
  • Loading branch information
joeds13 committed Jul 24, 2021
1 parent b9e940c commit 2dcd264
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
getgitignore
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# GetGitIgnore

Tool to get Git Ignore files from [GitHub GitIgnore](https://github.com/github/gitignore)

## Features

* Outputs to stdout by default
* list subcommand - lists all gitignores
* search subcommand - searches gitignores

## TODO

* arg to inteligently append to file
* don't duplicate if header comment found
* no get args == inteligent mode - finds types of files and gets appropriate ignores
* add in headers comment with commit hash above each ignore content
* Follows structure in github/gitignore
* Use Go Releaser
* Use Github Actions
* Add to Brew
* If finds a GITHUB_TOKEN env var, try use it
* Do a case insesitive search
* Move cli logic out of gitignore package
* If it's go:
* guess the compiled binary name and add to ignore
* remove comment against vendor/
* remove panics
* Case insensitive search
* rename to "gitignore"?
100 changes: 100 additions & 0 deletions gitignore/gitignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package gitignore

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)

type file struct {
Path string `json:"path"`
Mode string `json:"mode"`
Type string `json:"type"`
Sha string `json:"sha"`
Url string `json:"url"`
}

type tree struct {
Sha string `json:"sha"`
Url string `json:"url"`
Tree []file `json:"tree"`
}

var github = map[string]string{
"rawUrl": "https://raw.githubusercontent.com",
"reposApi": "https://api.github.com/repos",
"repo": "github/gitignore",
"branch": "master",
}

func Get(query string) ([]byte, error) {
url := fmt.Sprintf("%s/%s/%s/%s.gitignore", github["rawUrl"], github["repo"], github["branch"], query)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// TODO prepend header to bytes
var ignore []byte
if resp.StatusCode == http.StatusOK {
ignore, err = ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
return ignore, err
}

return nil, fmt.Errorf("could not find gitignore for %s", query)
}

func List() []string {
return getAllGitignores()
}

func Search(query string) ([]string, error) {
gitignores := getAllGitignores()

var ignores []string
for _, file := range gitignores {
if strings.Contains(file, query) {
ignores = append(ignores, file)
}

}
if len(ignores) == 0 {
return nil, fmt.Errorf("no gitignore found for: %s", query)
}
return ignores, nil
}

func getAllGitignores() []string {
url := fmt.Sprintf("%s/%s/git/trees/%s?recursive=1", github["reposApi"], github["repo"], github["branch"])
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()

repoTreeResponse, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}

var repoTree tree
err = json.Unmarshal([]byte(repoTreeResponse), &repoTree)
if err != nil {
panic(err)
}

var fileList []string
for _, v := range repoTree.Tree {
if strings.Contains(v.Path, ".gitignore") {
fileList = append(fileList, strings.ReplaceAll(v.Path, ".gitignore", ""))
}
}

return fileList
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/joeds13/getgitignore

go 1.16
100 changes: 100 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"os"

"github.com/joeds13/getgitignore/gitignore"
)

var (
version = "dev"
commit = "none"
date = "unknown"
)

const (
usage = `usage: %s
Get GitIgnore Files
`
)

func main() {
getCmd := flag.NewFlagSet("get", flag.ExitOnError)
getFile := getCmd.Bool("file", false, "TODO: file usage")
listCmd := flag.NewFlagSet("list", flag.ExitOnError)
searchCmd := flag.NewFlagSet("search", flag.ExitOnError)

flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), usage, os.Args[0])
getCmd.Usage()
// listCmd.Usage()
// searchCmd.Usage()
}

if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}

switch os.Args[1] {
case "get":
getCmd.Parse(os.Args[2:])
if len(os.Args[2:]) != 1 {
flag.Usage()
os.Exit(1)
}

ignore, err := gitignore.Get(os.Args[2])
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

if *getFile {
// TODO handle appending to gitignore
err := ioutil.WriteFile(".gitignore", ignore, 0644)
if err != nil {
panic(err)
}
} else {
fmt.Printf("%s\n", ignore)
}

case "list":
listCmd.Parse(os.Args[2:])
if len(listCmd.Args()) > 0 {
flag.Usage()
os.Exit(1)
}
ignores := gitignore.List()
for _, file := range ignores {
fmt.Printf("%s\n", file)
}

case "search":
searchCmd.Parse(os.Args[2:])
if len(searchCmd.Args()) > 1 {
flag.Usage()
os.Exit(1)
}
ignores, err := gitignore.Search(searchCmd.Args()[0])
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
for _, file := range ignores {
fmt.Printf("%s\n", file)
}

case "version":
fmt.Printf("Version: %s\nCommit: %s\nBuilt at: %s\n", version, commit, date)

default:
flag.Usage()
os.Exit(1)
}
}

0 comments on commit 2dcd264

Please sign in to comment.