feat: use netrc to login github

main
sudo pacman -Syu 2023-08-15 16:41:35 +07:00
parent 938a2ca0f6
commit 14b5b51504
4 changed files with 27 additions and 4 deletions

View File

@ -2,7 +2,7 @@
Write markdown, convert to html, then publish using Github Pages.
First add GitHub access token in `.github_access_token`.
First add GitHub access token in `~/.netrc`.
Steps:

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.18
require (
github.com/google/go-github/v53 v53.2.0
github.com/make-go-great/netrc-go v0.0.2
golang.org/x/oauth2 v0.11.0
golang.org/x/sync v0.3.0
)

6
go.sum
View File

@ -4,6 +4,7 @@ github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7N
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
@ -15,6 +16,10 @@ github.com/google/go-github/v53 v53.2.0 h1:wvz3FyF53v4BK+AsnvCmeNhf8AkTaeh2SoYu/
github.com/google/go-github/v53 v53.2.0/go.mod h1:XhFRObz+m/l+UCm9b7KSIC3lT3NWSXGt7mOsAWEloao=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/make-go-great/netrc-go v0.0.2 h1:+slj+cMcNL+W/xbHvLPJ8DyTrjcfzQt7bYWZtNV+Isg=
github.com/make-go-great/netrc-go v0.0.2/go.mod h1:fwFh8yeVT+d88SzIyH+0dWyfgcba8SUchzFuQXlhwws=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
@ -45,3 +50,4 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

22
main.go
View File

@ -12,6 +12,8 @@ import (
"github.com/google/go-github/v53/github"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
"github.com/make-go-great/netrc-go"
)
const (
@ -21,6 +23,9 @@ const (
generatedPath = "docs"
extHTML = ".html"
netrcPath = "~/.netrc"
netrcMachineGitHub = "github.com"
)
type templatePostData struct {
@ -58,14 +63,25 @@ func main() {
}
// Prepare GitHub
ghAccessTokenBytes, err := os.ReadFile(".github_access_token")
netrcData, err := netrc.ParseFile(netrcPath)
if err != nil {
log.Fatalln("Failed to read file", ".github_access_token", err)
log.Fatalln("Failed to read file netrc", err)
}
var ghAccessToken string
for _, machine := range netrcData.Machines {
if machine.Name == netrcMachineGitHub {
ghAccessToken = machine.Password
break
}
}
if ghAccessToken == "" {
log.Fatalln("Empty GitHub token in netrc")
}
ghTokenSrc := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: strings.TrimSpace(string(ghAccessTokenBytes)),
AccessToken: strings.TrimSpace(ghAccessToken),
},
)
ghHTTPClient := oauth2.NewClient(ctx, ghTokenSrc)