Skip to content

Commit

Permalink
Merge branch 'v1'
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoh86 committed Mar 31, 2019
2 parents 33859b8 + 7cfcb9a commit 3399008
Show file tree
Hide file tree
Showing 63 changed files with 2,520 additions and 793 deletions.
112 changes: 76 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ You can also list projects (local repositories) (`gogh list`), find a project (`
## SYNOPSIS

```
gogh get [--update,u] [--ssh] [--shallow] [(<repository URL> | <user>/<project> | <project>)...]
gogh bulk-get [--update,u] [--ssh] [--shallow]
gogh pipe-get [--update,u] [--ssh] [--shallow] <command> <command-args>...
gogh fork [--update,u] [--ssh] [--shallow] [--no-remote] [--remote-name=<REMOTE>] [--org=<ORGANIZATION] (<repository URL> | <user>/<project> | <project>)
gogh new [--update,u] [--ssh] [--shallow] [--no-remote] [--remote-name=<REMOTE>] [--org=<ORGANIZATION] (<repository URL> | <user>/<project> | <project>)
gogh list [--format,f=short|full|relative|url] [--primary,p] [<query>]
gogh dump [--primary,p] [<query>]
gogh find (<project>)
gogh where [--primary,p] [<query>]
gogh repo [--user=<USER>] [--own] [--collaborate] [--member] [--visibility=<VISIBILITY>] [--sort=<SORT>] [--direction=<DIRECTION>]
gogh root [--all]
gogh [--config=<CONFIG>] get [--update,u] [--ssh] [--shallow] [(<repository URL> | <user>/<project> | <project>)...]
gogh [--config=<CONFIG>] bulk-get [--update,u] [--ssh] [--shallow]
gogh [--config=<CONFIG>] pipe-get [--update,u] [--ssh] [--shallow] <command> <command-args>...
gogh [--config=<CONFIG>] fork [--update,u] [--ssh] [--shallow] [--no-remote] [--remote-name=<REMOTE>] [--org=<ORGANIZATION] (<repository URL> | <user>/<project> | <project>)
gogh [--config=<CONFIG>] new [--update,u] [--ssh] [--shallow] [--no-remote] [--remote-name=<REMOTE>] [--org=<ORGANIZATION] (<repository URL> | <user>/<project> | <project>)
gogh [--config=<CONFIG>] list [--format,f=short|full|relative|url] [--primary,p] [<query>]
gogh [--config=<CONFIG>] dump [--primary,p] [<query>]
gogh [--config=<CONFIG>] find (<project>)
gogh [--config=<CONFIG>] where [--primary,p] [<query>]
gogh [--config=<CONFIG>] repo [--user=<USER>] [--own] [--collaborate] [--member] [--visibility=<VISIBILITY>] [--sort=<SORT>] [--direction=<DIRECTION>]
gogh [--config=<CONFIG>] root [--all]
```

## INSTALLATION
Expand All @@ -55,6 +55,56 @@ brew update
brew install gogh
```

## CONFIGURATIONS

It's possible to change targets by a preference **YAML file**.
If you don't set `--config` flag or `GOGH_CONFIG` environment variable,
`gogh` loads configurations from `${XDG_CONFIG_HOME:-$HOME/.config}/gogh/config.yaml`

Each of propoerties are able to be overwritten by environment variables.

### (REQUIRED) `github.user`

A name of your GitHub user (i.e. `kyoh86`).

If an environment variable `GOGH_GITHUB_USER` is set, its value is used instead.

### `root`

The paths to directory under which cloned repositories are placed.
See [DIRECTORY STRUCTURES](#DIRECTORY+STRUCTURES) below. Default: `~/go/src`.

This property can have multiple values.
If so, the first one becomes primary one i.e. new repository clones are always created under it.
You may want to specify `$GOPATH/src` as a secondary root.

If an environment variable `GOGH_ROOT` is set, its value is used instead.

### `log.level`

The level to output logs (debug, info, warn, error or panic). Default: `warn`.

If an environment variable `GOGH_LOG_LEVEL` is set, its value is used instead.

### `github.token`

The token to connect GitHub API.

If an environment variable `GOGH_GITHUB_TOKEN` is set, its value is used instead.

### `github.host`

The host name to connect to GitHub. Default: `github.com`.

If an environment variable `GOGH_GITHUB_HOST` is set, its value is used instead.

### `log.(date|time|microseconds|longfile|shortfile|utc)`

It can be customized that what log prints with message. Default: `log.time` is "yes", others are "no".
`"yes"` means that its optional property should be printed.

If an environment variable `GOGH_LOG_(DATE|TIME|MICROSECONDS|LONGFILE|SHORTFILE|UTC)` is set, its value is used instead.

## COMMANDS

```
Expand Down Expand Up @@ -130,46 +180,36 @@ Show a list of repositories for a user.

### `root`

Prints repositories' root (i.e. `gogh.root`). Without `--all` option, the primary one is shown.
Print repositories' root (i.e. `config get root`). Without `--all` option, the primary one is shown.

## ENVIRONMENT VARIABLES

### GOGH_ROOT

The paths to directory under which cloned repositories are placed.
See [DIRECTORY STRUCTURES](#DIRECTORY+STRUCTURES) below. Defaults to `~/go/src`.
### `config get-all`

This variable can have multiple values.
If so, the first one becomes primary one i.e. new repository clones are always created under it.
You may want to specify `$GOPATH/src` as a secondary root (environment variables should be expanded.)
Print all configuration options value.

### GOGH_GITHUB_USER
### `config get`

A name of your GitHub user (i.e. `kyoh86`).
If it is not set, gogh uses `GITHUB_USER` envar or OS user name from envar (`USERNAME` in windows, `USER` in others) instead.
Print one configuration option value.

### GOGH_LOG_LEVEL
### `config put`

The level to output logs (debug, info, warn, error or panic). Default: warn
Set or add one configuration option.

### GOGH_GHE_HOST
### `config unset`

Hostnames of your GitHub Enterprise installation.
This variable can have multiple values that separated with spaces.
Unset one configuration option.

### GOGH_GITHUB_TOKEN
## ENVIRONMENT VARIABLES

The token to connect GitHub API.
If it is not set, gogh uses `GITHUB_TOKEN` envar instead.
Some environment variables are used for flags.

### GOGH_GITHUB_HOST
### GOGH_CONFIG

The host to connect GitHub on default.
If it is not set, gogh uses `GITHUB_HOST` envar or `github.com` instead.
You can set it instead of `--config` flag (configuration file path).
Default: `${XDG_CONFIG_HOME:-$HOME/.config}/gogh/config.yaml`.

### GOGH_FLAG_ROOT_ALL

We can set it truely value and `gogh root` shows all of the roots like `gogh root --all`.
You can set it truely value and `gogh root` shows all of the roots like `gogh root --all`.
If we want show only primary root, call `gogh root --no-all`.

e.g.
Expand Down
3 changes: 1 addition & 2 deletions command/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package command
import (
"bufio"
"io"
"os"
"os/exec"

"github.com/kyoh86/gogh/gogh"
Expand Down Expand Up @@ -33,7 +32,7 @@ func Pipe(ctx gogh.Context, update, withSSH, shallow bool, command string, comma

// Bulk get repositories specified in stdin.
func Bulk(ctx gogh.Context, update, withSSH, shallow bool) error {
return bulkFromReader(ctx, os.Stdin, update, withSSH, shallow)
return bulkFromReader(ctx, ctx.Stdin(), update, withSSH, shallow)
}

// bulkFromReader bulk get repositories specified in reader.
Expand Down
42 changes: 42 additions & 0 deletions command/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package command

import (
"fmt"

"github.com/kyoh86/gogh/config"
)

func ConfigGetAll(cfg *config.Config) error {
for _, name := range config.OptionNames() {
opt, _ := config.Option(name) // ignore error: config.OptionNames covers all accessor
value := opt.Get(cfg)
fmt.Printf("%s = %s\n", name, value)
}
return nil
}

func ConfigGet(cfg *config.Config, optionName string) error {
opt, err := config.Option(optionName)
if err != nil {
return err
}
value := opt.Get(cfg)
fmt.Println(value)
return nil
}

func ConfigPut(cfg *config.Config, optionName, optionValue string) error {
opt, err := config.Option(optionName)
if err != nil {
return err
}
return opt.Put(cfg, optionValue)
}

func ConfigUnset(cfg *config.Config, optionName string) error {
opt, err := config.Option(optionName)
if err != nil {
return err
}
return opt.Unset(cfg)
}
40 changes: 40 additions & 0 deletions command/config_get_all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package command_test

import (
"github.com/kyoh86/gogh/command"
"github.com/kyoh86/gogh/config"
)

func ExampleConfigGetAll() {
if err := command.ConfigGetAll(&config.Config{
GitHub: config.GitHubConfig{
Token: "tokenx1",
Host: "hostx1",
User: "kyoh86",
},
Log: config.LogConfig{
Level: "trace",
Date: config.TrueOption,
Time: config.FalseOption,
MicroSeconds: config.TrueOption,
LongFile: config.TrueOption,
ShortFile: config.TrueOption,
UTC: config.TrueOption,
},
VRoot: []string{"/foo", "/bar"},
}); err != nil {
panic(err)
}
// Unordered output:
// root = /foo:/bar
// github.host = hostx1
// github.user = kyoh86
// github.token = tokenx1
// log.level = trace
// log.date = yes
// log.time = no
// log.microseconds = yes
// log.longfile = yes
// log.shortfile = yes
// log.utc = yes
}
23 changes: 23 additions & 0 deletions command/config_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package command_test

import (
"testing"

"github.com/kyoh86/gogh/command"
"github.com/kyoh86/gogh/config"
"github.com/stretchr/testify/assert"
)

func ExampleConfigGet() {
if err := command.ConfigGet(&config.Config{
VRoot: []string{"/foo", "/bar"},
}, "root"); err != nil {
panic(err)
}
// Output:
// /foo:/bar
}

func TestConfigGet(t *testing.T) {
assert.EqualError(t, command.ConfigGet(&config.Config{}, "invalid.name"), "invalid option name")
}
15 changes: 15 additions & 0 deletions command/config_put_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package command_test

import (
"testing"

"github.com/kyoh86/gogh/command"
"github.com/kyoh86/gogh/config"
"github.com/stretchr/testify/assert"
)

func TestConfigPut(t *testing.T) {
var cfg config.Config
assert.NoError(t, command.ConfigPut(&cfg, "github.host", "hostx1"))
assert.EqualError(t, command.ConfigPut(&cfg, "invalid.name", "hostx2"), "invalid option name")
}
20 changes: 20 additions & 0 deletions command/config_unset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package command_test

import (
"testing"

"github.com/kyoh86/gogh/command"
"github.com/kyoh86/gogh/config"
"github.com/stretchr/testify/assert"
)

func TestConfigUnset(t *testing.T) {
cfg := config.Config{
GitHub: config.GitHubConfig{
Host: "hostx1",
},
}
assert.NoError(t, command.ConfigUnset(&cfg, "github.host"))
assert.Empty(t, cfg.GitHub.Host)
assert.EqualError(t, command.ConfigUnset(&cfg, "invalid.name"), "invalid option name")
}
46 changes: 46 additions & 0 deletions command/empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package command

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

"github.com/kyoh86/gogh/gogh"
"github.com/kyoh86/gogh/internal/context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEmpty(t *testing.T) {
defaultGitClient = &mockGitClient{}
defaultHubClient = &mockHubClient{}
tmp, err := ioutil.TempDir(os.TempDir(), "gogh-test")
require.NoError(t, err)
defer os.RemoveAll(tmp)
ctx := &context.MockContext{
MRoot: []string{tmp},
MGitHubHost: "github.com",
}

assert.NoError(t, Pipe(ctx, false, false, false, "echo", []string{"kyoh86/gogh"}))
ctx.MStdin = strings.NewReader(`kyoh86/gogh`)
assert.NoError(t, Bulk(ctx, false, false, false))
mustRepo := func(name string) *gogh.Repo {
t.Helper()
repo, err := gogh.ParseRepo(name)
require.NoError(t, err)
return repo
}
assert.NoError(t, GetAll(ctx, false, false, false, gogh.Repos{
*mustRepo("kyoh86/gogh"),
*mustRepo("kyoh86/vim-gogh"),
}))
assert.NoError(t, Get(ctx, false, false, false, mustRepo("kyoh86/gogh")))
assert.NoError(t, Fork(ctx, false, false, false, false, "", "", mustRepo("kyoh86/gogh")))
assert.NoError(t, List(ctx, gogh.ProjectListFormatShort, false, ""))
proj1 := filepath.Join(tmp, "github.com", "kyoh86", "gogh", ".git")
require.NoError(t, os.MkdirAll(proj1, 0755))
assert.NoError(t, Root(ctx, false))
}
22 changes: 0 additions & 22 deletions command/find.go

This file was deleted.

6 changes: 2 additions & 4 deletions command/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ func Fork(ctx gogh.Context, update, withSSH, shallow, noRemote bool, remoteName
return err
}
log.Printf("info: Forking a repository")
if err := hubFork(ctx, project, repo, noRemote, remoteName, organization); err != nil {
if err := hub().Fork(ctx, project, repo, noRemote, remoteName, organization); err != nil {
return err
}

if _, err := fmt.Fprintln(ctx.Stdout(), project.RelPath); err != nil {
return err
}
fmt.Fprintln(ctx.Stdout(), project.RelPath)
return nil
}
Loading

0 comments on commit 3399008

Please sign in to comment.