Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
add new botway feature
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Aug 18, 2022
1 parent 92dea8f commit 21a7a34
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/authflow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"

"github.com/abdfnx/botway/constants"
"github.com/botwayorg/gh/api"
"github.com/botwayorg/gh/core/ghinstance"
"github.com/botwayorg/gh/pkg/cmdutil"
"github.com/botwayorg/gh/pkg/iostreams"
"github.com/cli/oauth"
"github.com/tidwall/sjson"
)

var (
Expand All @@ -27,6 +30,22 @@ type iconfig interface {
Write() error
}

func AddToBWConfig(token string) {
botTypeContent, _ := sjson.Set(string(constants.BotwayConfig), "github.token", token)

remove := os.Remove(constants.BotwayConfigFile)

if remove != nil {
log.Fatal(remove)
}

newBotConfig := os.WriteFile(constants.BotwayConfigFile, []byte(botTypeContent), 0644)

if newBotConfig != nil {
panic(newBotConfig)
}
}

func AuthFlowWithConfig(cfg iconfig, IO *iostreams.IOStreams, hostname, notice string, additionalScopes []string) (string, error) {
// TODO this probably shouldn't live in this package. It should probably be in a new package that
// depends on both iostreams and config.
Expand All @@ -42,7 +61,11 @@ func AuthFlowWithConfig(cfg iconfig, IO *iostreams.IOStreams, hostname, notice s
if err != nil {
return "", err
}

err = cfg.Set(hostname, "oauth_token", token)

AddToBWConfig(token)

if err != nil {
return "", err
}
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/MakeNowJust/heredoc v1.0.0
github.com/abdfnx/botway v0.1.81
github.com/botwayorg/git v0.0.0-20220620131453-e25d6a207203
github.com/briandowns/spinner v1.19.0
github.com/charmbracelet/bubbles v0.13.0
Expand All @@ -30,12 +31,14 @@ require (
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
github.com/tidwall/sjson v1.2.5
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/abdfnx/tran v0.1.43 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
Expand All @@ -61,6 +64,9 @@ require (
github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/tidwall/gjson v1.14.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/abdfnx/botway v0.1.81 h1:7eNcdMm0vkDf+ByF3f3xkMrnIskfpfJ9EU1YtndC9SM=
github.com/abdfnx/botway v0.1.81/go.mod h1:PvWggkUzfkMdNc43tpnfYYyWUf9arDjlkkTfy3rwct8=
github.com/abdfnx/tran v0.1.43 h1:+Pt+eueAfqbskCuPMILmzKL3bRxWdVRfGfoS8XNp5gM=
github.com/abdfnx/tran v0.1.43/go.mod h1:MtLBLPXDmxFyZaNdF0+Q9K2yd0nd+zXTSlrxE7IBP8w=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
Expand Down Expand Up @@ -275,6 +279,14 @@ github.com/stretchr/testify v1.7.3 h1:dAm0YRdRQlWojc3CrCRgPBzG5f941d0zvAKu7qY4e+
github.com/stretchr/testify v1.7.3/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/auth/shared/login_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func Login(opts *LoginOptions) error {
if err := cfg.Set(hostname, "oauth_token", authToken); err != nil {
return err
}

authflow.AddToBWConfig(authToken)
}

var username string
Expand Down

0 comments on commit 21a7a34

Please sign in to comment.