Skip to content

Commit

Permalink
Refactor (#2)
Browse files Browse the repository at this point in the history
* refactor

* more refactor
  • Loading branch information
atye authored Jul 13, 2021
1 parent a1146ad commit 2c85714
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 58 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: goreleaser

on:
push:
tags:
- '*'

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
-
name: Run GoReleaser
uses: goreleaser/[email protected]
with:
args: release --rm-dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin/*
config.yaml
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build:
go build -o bin/ttchat cmd/main.go
-mkdir -p ./bin
CGO_ENABLED=0 go build -o ./bin/ttchat ./main.go

test:
go test -v -count=1 -v -cover -race ./...
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Connect to a Twitch channel's chat from your terminal

### With Go

```go get github.com/atye/ttchat```
```go get github.com/atye/ttchat/cmd```

### Clone and build
```git clone https://github.com/atye/ttchat.git```
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/atye/ttchat
module gitub.com/atye/ttchat

go 1.16

Expand All @@ -8,7 +8,7 @@ require (
github.com/charmbracelet/lipgloss v0.3.0
github.com/coreos/go-oidc/v3 v3.0.0
github.com/gempir/go-twitch-irc/v2 v2.5.0
github.com/google/uuid v1.2.0
github.com/google/uuid v1.3.0
github.com/nicklaw5/helix v1.24.0
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2
github.com/spf13/cobra v1.2.1
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/atye/ttchat v0.0.0-20210713012411-a1146ad9a397 h1:uJejCV1Mg7NDyVPfUDTKUo/RmHgj3PONxA5zo9Zh3Vc=
github.com/atye/ttchat v0.0.0-20210713012411-a1146ad9a397/go.mod h1:venTexKBMRBkwk+Sw8iIXkk1qDKd8xOJUV6P0CIa0bU=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -152,8 +154,9 @@ github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ func (t token) Claims(v interface{}) error {
}

func TestGetOAuthToken(t *testing.T) {
conf := &oauth2.Config{
ClientID: clientID,
Scopes: []string{"openid", "chat:read", "chat:edit"},
Endpoint: oauth2.Endpoint{
AuthURL: twitch.Endpoint.AuthURL,
TokenURL: twitch.Endpoint.TokenURL,
},
RedirectURL: fmt.Sprintf("http://localhost:%s", "9999"),
}
t.Run("success", func(t *testing.T) {
conf := &oauth2.Config{
ClientID: clientID,
Scopes: []string{"openid", "chat:read", "chat:edit"},
Endpoint: oauth2.Endpoint{
AuthURL: twitch.Endpoint.AuthURL,
TokenURL: twitch.Endpoint.TokenURL,
},
RedirectURL: fmt.Sprintf("http://localhost:%s", "9999"),
}

want := "123"
want := "123"

u := Utils{
OpenURL: func(url string) error {
go func() {
for {
u := Utils{
OpenURL: func(url string) error {
go func() {
r, err := http.NewRequest("GET", "http://localhost:9999/callback", nil)
if err != nil {
panic(err)
Expand All @@ -77,21 +77,21 @@ func TestGetOAuthToken(t *testing.T) {
_, callbackErr = http.DefaultClient.Do(r)
time.Sleep(500 * time.Millisecond)
}
}
}()
return nil
},
NewUUID: func() (string, error) {
return "000", nil
},
}
}()
return nil
},
NewUUID: func() (string, error) {
return "000", nil
},
}

tkn, err := GetOAuthToken(conf, verifier{}, u)
if err != nil {
t.Fatal(err)
}
tkn, err := GetOAuthToken(conf, verifier{}, u)
if err != nil {
t.Fatal(err)
}

if tkn != want {
t.Errorf("expected access token %s, got %s", want, tkn)
}
if tkn != want {
t.Errorf("expected access token %s, got %s", want, tkn)
}
})
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package openid
import (
"context"

"github.com/atye/ttchat/internal/irc/auth"
"github.com/coreos/go-oidc/v3/oidc"
"gitub.com/atye/ttchat/internal/auth"
)

type CoreOSVerifier struct {
Expand Down
26 changes: 10 additions & 16 deletions cmd/main.go → internal/cmd/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package cmd

import (
"context"
Expand All @@ -24,17 +24,17 @@ import (
"strings"
"time"

"github.com/atye/ttchat/internal/irc"
"github.com/atye/ttchat/internal/irc/auth"
"github.com/atye/ttchat/internal/irc/auth/openid"
ttchattwitch "github.com/atye/ttchat/internal/irc/twitch"
"github.com/atye/ttchat/internal/terminal"
tea "github.com/charmbracelet/bubbletea"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/google/uuid"
"github.com/nicklaw5/helix"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"gitub.com/atye/ttchat/internal/auth"
"gitub.com/atye/ttchat/internal/auth/openid"
"gitub.com/atye/ttchat/internal/irc"
"gitub.com/atye/ttchat/internal/irc/client"
"gitub.com/atye/ttchat/internal/terminal"
"golang.org/x/oauth2"
"golang.org/x/oauth2/twitch"
"gopkg.in/yaml.v3"
Expand All @@ -56,21 +56,15 @@ var (
ErrNoUsername = errors.New("no username in configuration file")
)

func main() {
if err := NewRootCmd().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func NewRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "ttchat",
Short: "Connect to twitch chat in your terminal",
Long: `
ttchat is a terminal application that connects to a twitch channel's
chat using a small configuration file. See repo for more details.
ttchat -h
ttchat --channel ludwig
ttchat --channel ludwig --lines 5
`,
Expand Down Expand Up @@ -133,7 +127,7 @@ ttchat --channel ludwig --lines 5
//

// Create IRC client and start
ircClient := ttchattwitch.NewGempirClient(conf.Username, channel, accessToken)
ircClient := client.NewGempirClient(conf.Username, channel, accessToken)
c := irc.NewIRCService(displayName, channel, ircClient)
if tea.NewProgram(terminal.NewModel(lines, c)).Start() != nil {
errExit(err)
Expand Down Expand Up @@ -218,7 +212,7 @@ func getAccessToken(tokenFlagValue string, conf Config, verifier auth.TokenVerif

t, err := auth.GetOAuthToken(oauthConf, verifier, u)
if err != nil {
return "", nil
return "", err
}
return t, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package twitch
package client

// Copyright © 2021 Dell Inc., or its subsidiaries. All Rights Reserved.
//
Expand All @@ -17,9 +17,9 @@ package twitch
import (
"fmt"

"github.com/atye/ttchat/internal/irc"
"github.com/atye/ttchat/internal/types"
"github.com/gempir/go-twitch-irc/v2"
"gitub.com/atye/ttchat/internal/irc"
"gitub.com/atye/ttchat/internal/types"
)

type Gempir struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/irc/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"fmt"
"strings"

"github.com/atye/ttchat/internal/terminal"
"github.com/atye/ttchat/internal/types"
"github.com/charmbracelet/lipgloss"
"gitub.com/atye/ttchat/internal/terminal"
"gitub.com/atye/ttchat/internal/types"
)

type IRC interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/irc/irc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"
"testing"

"github.com/atye/ttchat/internal/types"
"github.com/charmbracelet/lipgloss"
"gitub.com/atye/ttchat/internal/types"
)

// build a Private Message
Expand Down
2 changes: 1 addition & 1 deletion internal/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"fmt"
"strings"

"github.com/atye/ttchat/internal/types"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"gitub.com/atye/ttchat/internal/types"
)

type Twitch interface {
Expand Down
29 changes: 29 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright © 2021 Dell Inc., or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

"gitub.com/atye/ttchat/internal/cmd"
)

func main() {
if err := cmd.NewRootCmd().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

0 comments on commit 2c85714

Please sign in to comment.