Skip to content

Commit a88ff7b

Browse files
committed
support fine-grained PAT
1 parent 78507fe commit a88ff7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1498
-5285
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
- name: Setup Go
4646
uses: actions/setup-go@v3
4747
with:
48-
go-version: 1.19
48+
go-version: 1.21
4949
- name: Run GoReleaser
5050
uses: goreleaser/goreleaser-action@v3
5151
env:

.github/workflows/test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup Go
1919
uses: actions/setup-go@v3
2020
with:
21-
go-version: 1.19
21+
go-version: 1.21
2222
- name: Test Go
2323
run: go test -v --race ./...
2424
test-release:
@@ -30,7 +30,7 @@ jobs:
3030
- name: Setup Go
3131
uses: actions/setup-go@v3
3232
with:
33-
go-version: 1.19
33+
go-version: 1.21
3434
- name: Try Bump-up Semantic Version
3535
uses: kyoh86/git-vertag-action@v1
3636
with:
@@ -48,11 +48,11 @@ jobs:
4848
- name: Setup Go
4949
uses: actions/setup-go@v3
5050
with:
51-
go-version: 1.19
51+
go-version: 1.21
5252
- name: Search diagnostics
5353
uses: golangci/golangci-lint-action@v3
5454
with:
55-
version: v1.50.1
55+
version: v1.55.2
5656
test-covers:
5757
name: Take test coverages
5858
runs-on: ubuntu-latest
@@ -62,7 +62,7 @@ jobs:
6262
- name: Setup Go
6363
uses: actions/setup-go@v3
6464
with:
65-
go-version: 1.19
65+
go-version: 1.21
6666
- name: Take coverage
6767
run: go test -coverprofile=coverage.txt -covermode=atomic ./...
6868
- name: Send coverage

.golangci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
linters:
22
enable:
33
- bodyclose
4-
- depguard
54
- dogsled
65
- gocritic
76
- godox
87
- gofmt
98
- goimports
10-
- golint
119
- misspell
1210
- nakedret
1311
- prealloc

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ test: gen
2525
.PHONY: test
2626

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

README.md

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,20 @@ $ mkdir -p gogh_build && \
6767
$ makepkg -i
6868
```
6969

70+
## Setup
71+
72+
`gogh` manages repositories in multiple servers that is pairs of an owner and a host name.
73+
To login in new server or logout, you should use `auth login` with personal access tokens.
74+
75+
You should generate personal access tokens with repository permissions:
76+
77+
- ✔️ Read access to code and metadata
78+
- ✔️ Read and Write access to administration
79+
7080
## Available commands
7181

82+
See manual for detail: [usage/gogh.md](./usage/gogh.md).
83+
7284
### Show projects
7385

7486
| Command | Description |
@@ -78,26 +90,28 @@ $ makepkg -i
7890

7991
### Manipulate projects
8092

81-
| Command | Description |
82-
| -- | -- |
83-
| `gogh create` | Create a new project with a remote repository |
84-
| `gogh delete` | Delete a repository with a remote repository |
85-
| `gogh fork` | Fork a repository |
86-
| `gogh clone` | Clone a repository to local |
93+
| Command | Description |
94+
| -- | -- |
95+
| `gogh create` | Create a new project with a remote repository |
96+
| `gogh delete` | Delete a repository with a remote repository |
97+
| `gogh fork` | Fork a repository |
98+
| `gogh clone` | Clone a repository to local |
8799

88100
### Others
89101

90-
| Command | Description |
91-
| -- | -- |
92-
| `gogh roots` | Manage roots |
93-
| `gogh servers` | Manage servers |
94-
| `gogh bundle` | Manage bundle |
95-
| `gogh help` | Help about any command |
102+
| Command | Description |
103+
| -- | -- |
104+
| `gogh roots` | Manage roots |
105+
| `gogh auth` | Manage Authentications |
106+
| `gogh bundle` | Manage bundle |
107+
| `gogh help` | Help about any command |
96108

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

100-
## Roots
112+
## Configurations
113+
114+
### Roots
101115

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

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

110124
Default: `~/Projects`.
111125

112-
## Servers
126+
### Default Host and Owner
113127

114-
`gogh` manages repositories in some servers that pairs of a user and a host name. To login in new
115-
server or logout, you should use `servers login`. `gogh` uses the first server as the default one.
116128
When you specify a repository with ambiguous user or host, it will be interpolated with a default
117-
server.
118-
119-
I.E. when servers are:
129+
value. You may set them with `set-default`.
120130

121-
```
122-
- github.com:
123-
- user: kyoh86
124-
- example.com:
125-
- user: foobar
126-
```
131+
If you set them like below:
127132

128-
Ambiguous repository names will be interpolated:
133+
| key | value |
134+
| - | - |
135+
| `host` | `example.com` |
136+
| `owner` | `kyoh86` |
129137

130-
| Ambiguous name | Interpolated name |
131-
| -- | -- |
132-
| `gogh` | github.com/kyoh86/gogh |
133-
| `foobar/gogh` | github.com/foobar/gogh |
138+
ambiguous repository names will be interpolated:
134139

135-
## Commands
140+
| Ambiguous name | Interpolated name |
141+
| -- | -- |
142+
| `gogh` | example.com/kyoh86/gogh |
143+
| `foobar/gogh` | example.com/foobar/gogh |
136144

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

139147
## Directory structures
140148

cmd/gogh/auth.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"github.com/kyoh86/gogh/v2"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var tokens gogh.TokenManager
9+
10+
var authCommand = &cobra.Command{
11+
Use: "auth",
12+
Short: "Manage tokens",
13+
PersistentPostRunE: func(*cobra.Command, []string) error {
14+
return saveTokens()
15+
},
16+
}
17+
18+
func init() {
19+
configCommand.AddCommand(authCommand)
20+
facadeCommand.AddCommand(authCommand)
21+
}

cmd/gogh/auth_list.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/apex/log"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var authListCommand = &cobra.Command{
12+
Use: "list",
13+
Short: "Listup authenticated host and owners",
14+
Args: cobra.ExactArgs(0),
15+
RunE: func(cmd *cobra.Command, _ []string) error {
16+
ctx, cancel := context.WithCancel(cmd.Context())
17+
defer cancel()
18+
entries := tokens.Entries()
19+
if len(entries) == 0 {
20+
log.FromContext(ctx).Warn("No valid token found: you need to set token by `gogh auth login`")
21+
return nil
22+
}
23+
for _, entry := range entries {
24+
fmt.Println(entry)
25+
}
26+
return nil
27+
},
28+
}
29+
30+
func init() {
31+
authCommand.AddCommand(authListCommand)
32+
}

cmd/gogh/servers_login.go renamed to cmd/gogh/auth_login.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var loginFlags struct {
1616

1717
var loginCommand = &cobra.Command{
1818
Use: "login",
19-
Short: "Login for a server",
19+
Short: "Login for the host and owner",
2020
Args: cobra.ExactArgs(0),
2121
RunE: func(*cobra.Command, []string) error {
2222
if err := survey.Ask([]*survey.Question{
@@ -45,7 +45,8 @@ var loginCommand = &cobra.Command{
4545
}, &loginFlags); err != nil {
4646
return err
4747
}
48-
return servers.Set(loginFlags.Host, loginFlags.User, loginFlags.Password)
48+
tokens.Set(loginFlags.Host, loginFlags.User, gogh.Token(loginFlags.Password))
49+
return nil
4950
},
5051
}
5152

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

6263
func init() {
63-
setup()
6464
loginCommand.Flags().
6565
StringVarP(&loginFlags.Host, "host", "", gogh.DefaultHost, "Host name to login")
6666
loginCommand.Flags().StringVarP(&loginFlags.User, "user", "", "", "User name to login")
6767
loginCommand.Flags().
6868
StringVarP(&loginFlags.Password, "password", "", "", "Password or developer private token")
69-
serversCommand.AddCommand(loginCommand)
69+
authCommand.AddCommand(loginCommand)
7070
}

cmd/gogh/auth_logout.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"github.com/AlecAivazis/survey/v2"
5+
"github.com/apex/log"
6+
"github.com/kyoh86/gogh/v2"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var logoutCommand = &cobra.Command{
11+
Use: "logout",
12+
Short: "Logout from the host and owner",
13+
RunE: func(cmd *cobra.Command, indices []string) error {
14+
if len(indices) == 0 {
15+
configured := tokens.Entries()
16+
if len(configured) == 0 {
17+
return nil
18+
}
19+
indices = make([]string, 0, len(configured))
20+
for _, c := range configured {
21+
indices = append(indices, c.String())
22+
}
23+
24+
var selected []string
25+
if err := survey.AskOne(&survey.MultiSelect{
26+
Message: "Hosts to logout from",
27+
Options: indices,
28+
}, &selected); err != nil {
29+
return err
30+
}
31+
indices = selected
32+
}
33+
34+
for _, target := range indices {
35+
log.FromContext(cmd.Context()).WithField("target", target).Info("logout from")
36+
target, err := gogh.ParseTokenTarget(target)
37+
if err != nil {
38+
log.FromContext(cmd.Context()).WithField("target", target).Error("invalid target")
39+
continue
40+
}
41+
tokens.Delete(target.Host, target.Owner)
42+
}
43+
return nil
44+
},
45+
}
46+
47+
func init() {
48+
authCommand.AddCommand(logoutCommand)
49+
}

cmd/gogh/bundle.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import (
77
var bundleCommand = &cobra.Command{
88
Use: "bundle",
99
Short: "Manage bundle",
10-
PersistentPostRunE: func(*cobra.Command, []string) error {
11-
return saveServers()
12-
},
1310
}
1411

1512
func init() {
16-
setup()
1713
facadeCommand.AddCommand(bundleCommand)
1814
}

0 commit comments

Comments
 (0)