Skip to content

Commit

Permalink
support fine-grained PAT
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoh86 committed Nov 13, 2023
1 parent 78507fe commit a88ff7b
Show file tree
Hide file tree
Showing 76 changed files with 1,498 additions and 5,285 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3
env:
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
- name: Test Go
run: go test -v --race ./...
test-release:
Expand All @@ -30,7 +30,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
- name: Try Bump-up Semantic Version
uses: kyoh86/git-vertag-action@v1
with:
Expand All @@ -48,11 +48,11 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
- name: Search diagnostics
uses: golangci/golangci-lint-action@v3
with:
version: v1.50.1
version: v1.55.2
test-covers:
name: Take test coverages
runs-on: ubuntu-latest
Expand All @@ -62,7 +62,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
- name: Take coverage
run: go test -coverprofile=coverage.txt -covermode=atomic ./...
- name: Send coverage
Expand Down
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
linters:
enable:
- bodyclose
- depguard
- dogsled
- gocritic
- godox
- gofmt
- goimports
- golint
- misspell
- nakedret
- prealloc
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test: gen
.PHONY: test

man: gen
rm -rf ./usage/**.md
go run -tags man -ldflags "-X=main.version=$(VERSION) -X=main.commit=$(COMMIT) -X=main.date=$(DATE)" ./cmd/gogh man
.PHONY: man

Expand Down
72 changes: 40 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,20 @@ $ mkdir -p gogh_build && \
$ makepkg -i
```

## Setup

`gogh` manages repositories in multiple servers that is pairs of an owner and a host name.
To login in new server or logout, you should use `auth login` with personal access tokens.

You should generate personal access tokens with repository permissions:

- ✔️ Read access to code and metadata
- ✔️ Read and Write access to administration

## Available commands

See manual for detail: [usage/gogh.md](./usage/gogh.md).

### Show projects

| Command | Description |
Expand All @@ -78,26 +90,28 @@ $ makepkg -i

### Manipulate projects

| Command | Description |
| -- | -- |
| `gogh create` | Create a new project with a remote repository |
| `gogh delete` | Delete a repository with a remote repository |
| `gogh fork` | Fork a repository |
| `gogh clone` | Clone a repository to local |
| Command | Description |
| -- | -- |
| `gogh create` | Create a new project with a remote repository |
| `gogh delete` | Delete a repository with a remote repository |
| `gogh fork` | Fork a repository |
| `gogh clone` | Clone a repository to local |

### Others

| Command | Description |
| -- | -- |
| `gogh roots` | Manage roots |
| `gogh servers` | Manage servers |
| `gogh bundle` | Manage bundle |
| `gogh help` | Help about any command |
| Command | Description |
| -- | -- |
| `gogh roots` | Manage roots |
| `gogh auth` | Manage Authentications |
| `gogh bundle` | Manage bundle |
| `gogh help` | Help about any command |

Use `gogh [command] --help` for more information about a command.
Or see the manual in [usage/gogh.md](./usage/gogh.md).

## Roots
## Configurations

### Roots

`gogh` manages projects under the `roots` directories.

Expand All @@ -109,32 +123,26 @@ local project under it. If you want to change the default, use `roots set-defaul

Default: `~/Projects`.

## Servers
### Default Host and Owner

`gogh` manages repositories in some servers that pairs of a user and a host name. To login in new
server or logout, you should use `servers login`. `gogh` uses the first server as the default one.
When you specify a repository with ambiguous user or host, it will be interpolated with a default
server.

I.E. when servers are:
value. You may set them with `set-default`.

```
- github.com:
- user: kyoh86
- example.com:
- user: foobar
```
If you set them like below:

Ambiguous repository names will be interpolated:
| key | value |
| - | - |
| `host` | `example.com` |
| `owner` | `kyoh86` |

| Ambiguous name | Interpolated name |
| -- | -- |
| `gogh` | github.com/kyoh86/gogh |
| `foobar/gogh` | github.com/foobar/gogh |
ambiguous repository names will be interpolated:

## Commands
| Ambiguous name | Interpolated name |
| -- | -- |
| `gogh` | example.com/kyoh86/gogh |
| `foobar/gogh` | example.com/foobar/gogh |

Manual: [usage/gogh.md](./usage/gogh.md).
NOTE: default host will be "github.com" if you don't set it.

## Directory structures

Expand Down
21 changes: 21 additions & 0 deletions cmd/gogh/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/kyoh86/gogh/v2"
"github.com/spf13/cobra"
)

var tokens gogh.TokenManager

var authCommand = &cobra.Command{
Use: "auth",
Short: "Manage tokens",
PersistentPostRunE: func(*cobra.Command, []string) error {
return saveTokens()
},
}

func init() {
configCommand.AddCommand(authCommand)
facadeCommand.AddCommand(authCommand)
}
32 changes: 32 additions & 0 deletions cmd/gogh/auth_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"fmt"

"github.com/apex/log"
"github.com/spf13/cobra"
)

var authListCommand = &cobra.Command{
Use: "list",
Short: "Listup authenticated host and owners",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
entries := tokens.Entries()
if len(entries) == 0 {
log.FromContext(ctx).Warn("No valid token found: you need to set token by `gogh auth login`")
return nil
}
for _, entry := range entries {
fmt.Println(entry)
}
return nil
},
}

func init() {
authCommand.AddCommand(authListCommand)
}
8 changes: 4 additions & 4 deletions cmd/gogh/servers_login.go → cmd/gogh/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var loginFlags struct {

var loginCommand = &cobra.Command{
Use: "login",
Short: "Login for a server",
Short: "Login for the host and owner",
Args: cobra.ExactArgs(0),
RunE: func(*cobra.Command, []string) error {
if err := survey.Ask([]*survey.Question{
Expand Down Expand Up @@ -45,7 +45,8 @@ var loginCommand = &cobra.Command{
}, &loginFlags); err != nil {
return err
}
return servers.Set(loginFlags.Host, loginFlags.User, loginFlags.Password)
tokens.Set(loginFlags.Host, loginFlags.User, gogh.Token(loginFlags.Password))
return nil
},
}

Expand All @@ -60,11 +61,10 @@ func stringValidator(v func(s string) error) survey.Validator {
}

func init() {
setup()
loginCommand.Flags().
StringVarP(&loginFlags.Host, "host", "", gogh.DefaultHost, "Host name to login")
loginCommand.Flags().StringVarP(&loginFlags.User, "user", "", "", "User name to login")
loginCommand.Flags().
StringVarP(&loginFlags.Password, "password", "", "", "Password or developer private token")
serversCommand.AddCommand(loginCommand)
authCommand.AddCommand(loginCommand)
}
49 changes: 49 additions & 0 deletions cmd/gogh/auth_logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"github.com/AlecAivazis/survey/v2"
"github.com/apex/log"
"github.com/kyoh86/gogh/v2"
"github.com/spf13/cobra"
)

var logoutCommand = &cobra.Command{
Use: "logout",
Short: "Logout from the host and owner",
RunE: func(cmd *cobra.Command, indices []string) error {
if len(indices) == 0 {
configured := tokens.Entries()
if len(configured) == 0 {
return nil
}
indices = make([]string, 0, len(configured))
for _, c := range configured {
indices = append(indices, c.String())
}

var selected []string
if err := survey.AskOne(&survey.MultiSelect{
Message: "Hosts to logout from",
Options: indices,
}, &selected); err != nil {
return err
}
indices = selected
}

for _, target := range indices {
log.FromContext(cmd.Context()).WithField("target", target).Info("logout from")
target, err := gogh.ParseTokenTarget(target)
if err != nil {
log.FromContext(cmd.Context()).WithField("target", target).Error("invalid target")
continue
}
tokens.Delete(target.Host, target.Owner)
}
return nil
},
}

func init() {
authCommand.AddCommand(logoutCommand)
}
4 changes: 0 additions & 4 deletions cmd/gogh/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import (
var bundleCommand = &cobra.Command{
Use: "bundle",
Short: "Manage bundle",
PersistentPostRunE: func(*cobra.Command, []string) error {
return saveServers()
},
}

func init() {
setup()
facadeCommand.AddCommand(bundleCommand)
}
1 change: 0 additions & 1 deletion cmd/gogh/bundle_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ var (
)

func init() {
setup()
bundleDumpFlags.File = defaultFlag.BundleDump.File
bundleDumpCommand.Flags().VarP(&bundleDumpFlags.File, "file", "", "A file to output")
bundleCommand.AddCommand(bundleDumpCommand)
Expand Down
1 change: 0 additions & 1 deletion cmd/gogh/bundle_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var (
)

func init() {
setup()
bundleRestoreFlags.File = defaultFlag.BundleRestore.File
bundleRestoreCommand.Flags().
BoolVarP(&bundleRestoreFlags.Dryrun, "dryrun", "", false, "Displays the operations that would be performed using the specified command without actually running them")
Expand Down
Loading

0 comments on commit a88ff7b

Please sign in to comment.