Skip to content

Commit

Permalink
initial implement
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed May 13, 2019
0 parents commit 4fa6fbe
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.*
!.gitignore
!.travis.yml
dist/
8 changes: 8 additions & 0 deletions .travis.yml
@@ -0,0 +1,8 @@
language: go
go:
- 1.x
script:
- make lint
- make test
after_script:
- make cover
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2019 Songmu

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 changes: 34 additions & 0 deletions Makefile
@@ -0,0 +1,34 @@
ifdef update
u=-u
endif

export GO111MODULE=on

.PHONY: deps
deps:
go get ${u} -d
go mod tidy

.PHONY: devel-deps
devel-deps: deps
GO111MODULE=off go get ${u} \
golang.org/x/lint/golint \
github.com/mattn/goveralls \
github.com/Songmu/godzil/cmd/godzil

.PHONY: test
test: deps
go test

.PHONY: lint
lint: devel-deps
go vet
golint -set_exit_status

.PHONY: cover
cover: devel-deps
goveralls

.PHONY: release
release: devel-deps
godzil release
32 changes: 32 additions & 0 deletions README.md
@@ -0,0 +1,32 @@
gitconfig
=======

[![Build Status](https://travis-ci.org/Songmu/gitconfig.svg?branch=master)][travis]
[![Coverage Status](https://coveralls.io/repos/Songmu/gitconfig/badge.svg?branch=master)][coveralls]
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license]
[![GoDoc](https://godoc.org/github.com/Songmu/gitconfig?status.svg)][godoc]

[travis]: https://travis-ci.org/Songmu/gitconfig
[coveralls]: https://coveralls.io/r/Songmu/gitconfig?branch=master
[license]: https://github.com/Songmu/gitconfig/blob/master/LICENSE
[godoc]: https://godoc.org/github.com/Songmu/gitconfig

gitconfig short description

## Synopsis

```go
// simple usage here
```

## Description

## Installation

```console
% go get github.com/Songmu/gitconfig
```

## Author

[Songmu](https://github.com/Songmu)
14 changes: 14 additions & 0 deletions appveyor.yml
@@ -0,0 +1,14 @@
version: build-{build}.{branch}
clone_folder: c:\gopath\src\github.com/Songmu/gitconfig
shallow_clone: true
environment:
GOPATH: c:\gopath
GO111MODULE: on
install:
- copy c:\MinGW\bin\mingw32-make.exe c:\MinGW\bin\make.exe
- set PATH=%GOROOT%\bin;%GOPATH%\bin;c:\MinGW\bin;%PATH%
build: false
test_script:
- make lint
- make test
deploy: off
87 changes: 87 additions & 0 deletions config.go
@@ -0,0 +1,87 @@
package gitconfig

import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)

type Config struct {
System, Global, Local bool
File string
}

func (c *Config) Do(args ...string) (string, error) {
gitArgs := append([]string{"config", "--null"})
if c.System {
gitArgs = append(gitArgs, "--system")
}
if c.Global {
gitArgs = append(gitArgs, "--global")
}
if c.Local {
gitArgs = append(gitArgs, "--local")
}
if c.File != "" {
gitArgs = append(gitArgs, "--file", c.File)
}

gitArgs = append(gitArgs, args...)
cmd := exec.Command("git", gitArgs...)
cmd.Stderr = os.Stderr

buf, err := cmd.Output()
if exitError, ok := err.(*exec.ExitError); ok {
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
if waitStatus.ExitStatus() == 1 {
return "", notFound(
fmt.Sprintf("config not found. args: %q", strings.Join(gitArgs, " ")))
}
}
return "", err
}
return strings.TrimRight(string(buf), "\000"), nil
}

func (c *Config) Get(args ...string) (string, error) {
return c.Do(append([]string{"--get"}, args...)...)
}

func (c *Config) GetAll(args ...string) ([]string, error) {
val, err := c.Do(append([]string{"--get-all"}, args...)...)
if err != nil {
return nil, err
}
// No results found, return an empty slice
if val == "" {
return nil, nil
}
return strings.Split(val, "\000"), nil
}

func (c *Config) Bool(key string) (bool, error) {
val, err := c.Get("--type=bool", key)
if err != nil {
return false, err
}
return val == "true", nil
}

func (c *Config) Path(key string) (string, error) {
return c.Get("--type=path", key)
}

func (c *Config) PathAll(key string) ([]string, error) {
return c.GetAll("--type=path", key)
}

func (c *Config) Int(key string) (int, error) {
val, err := c.Get("--type=int", key)
if err != nil {
return 0, err
}
return strconv.Atoi(val)
}
48 changes: 48 additions & 0 deletions config_test.go
@@ -0,0 +1,48 @@
package gitconfig

import "testing"

func TestConfigAll(t *testing.T) {
dummyKey := "ghq.non.existent.key"
confs, err := GetAll(dummyKey)
if !IsNotFound(err) {
t.Errorf("error should be notFounder, but: %s", err)
}
if len(confs) > 0 {
t.Errorf("ConfigAll(%q) = %v; want %v", dummyKey, confs, nil)
}
}

func TestConfigURL(t *testing.T) {
defer WithConfig(t, `[ghq "https://ghe.example.com/"]
vcs = github
[ghq "https://ghe.example.com/hg/"]
vcs = hg
`)()

testCases := []struct {
name string
config []string
expect string
}{{
name: "github",
config: []string{"--get-urlmatch", "ghq.vcs", "https://ghe.example.com/foo/bar"},
expect: "github",
}, {
name: "hg",
config: []string{"--get-urlmatch", "ghq.vcs", "https://ghe.example.com/hg/repo"},
expect: "hg",
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
value, err := Do(tc.config...)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
if value != tc.expect {
t.Errorf("got: %s, expect: %s", value, tc.expect)
}
})
}
}
29 changes: 29 additions & 0 deletions error.go
@@ -0,0 +1,29 @@
package gitconfig

type errNotFound struct {
msg string
}

func (e *errNotFound) Error() string {
return e.msg
}

func (e *errNotFound) NotFound() bool {
return true
}

func notFound(msg string) error {
return &errNotFound{msg: msg}
}

type notFounder interface {
Error() string
NotFound() bool
}

func IsNotFound(err error) bool {
if nerr, ok := err.(notFounder); ok {
return nerr.NotFound()
}
return false
}
31 changes: 31 additions & 0 deletions gitconfig.go
@@ -0,0 +1,31 @@
package gitconfig

var defaultConfig = &Config{}

func Do(args ...string) (string, error) {
return defaultConfig.Do(args...)
}

func Get(args ...string) (string, error) {
return defaultConfig.Get(args...)
}

func GetAll(args ...string) ([]string, error) {
return defaultConfig.GetAll(args...)
}

func Bool(key string) (bool, error) {
return defaultConfig.Bool(key)
}

func Path(key string) (string, error) {
return defaultConfig.Path(key)
}

func PathAll(key string) ([]string, error) {
return defaultConfig.PathAll(key)
}

func Int(key string) (int, error) {
return defaultConfig.Int(key)
}
3 changes: 3 additions & 0 deletions go.mod
@@ -0,0 +1,3 @@
module github.com/Songmu/gitconfig

go 1.12
27 changes: 27 additions & 0 deletions helper.go
@@ -0,0 +1,27 @@
package gitconfig

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

// WithConfig is test helper to replace gitconfig temporarily
func WithConfig(t *testing.T, configContent string) func() {
tmpdir, err := ioutil.TempDir("", "gitconfig-test")
if err != nil {
t.Fatal(err)
}

tmpGitConfig := filepath.Join(tmpdir, "gitconfig")
ioutil.WriteFile(tmpGitConfig, []byte(configContent), 0644)

prevGitconfigEnv := os.Getenv("GIT_CONFIG")
os.Setenv("GIT_CONFIG", tmpGitConfig)

return func() {
os.Setenv("GIT_CONFIG", prevGitconfigEnv)
os.RemoveAll(tmpdir)
}
}
3 changes: 3 additions & 0 deletions version.go
@@ -0,0 +1,3 @@
package gitconfig

const version = "0.0.0"

0 comments on commit 4fa6fbe

Please sign in to comment.