Skip to content

Commit

Permalink
Merge pull request #42 from cloudnativedaysjp/refacotring
Browse files Browse the repository at this point in the history
rename package, struct, & interface names
  • Loading branch information
ShotaKitazawa authored Oct 9, 2022
2 parents 2a3c648 + 6e6422a commit 10a586f
Show file tree
Hide file tree
Showing 23 changed files with 73 additions and 76 deletions.
2 changes: 1 addition & 1 deletion cmd/cndctl/cmd/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/go-logr/logr"
"github.com/spf13/cobra"

"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/obsws"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/obsws"
pb "github.com/cloudnativedaysjp/cnd-operation-server/pkg/ws-proxy/schema"
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/dkwatcher/dkwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"go.uber.org/zap"
"golang.org/x/xerrors"

"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/dreamkast"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/sharedmem"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/dreamkast"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/sharedmem"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/model"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils"
)
Expand Down Expand Up @@ -75,7 +75,7 @@ func Run(ctx context.Context, conf Config) error {
}

func procedure(ctx context.Context,
dkClient dreamkast.ClientIface, mw sharedmem.WriterIface, mr sharedmem.ReaderIface,
dkClient dreamkast.Client, mw sharedmem.WriterIface, mr sharedmem.ReaderIface,
notificationEventSendChan chan<- model.CurrentAndNextTalk,
) error {
logger := utils.GetLogger(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"github.com/avast/retry-go"
"golang.org/x/xerrors"

"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/dreamkast/lib"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/dreamkast/lib"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/model"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils"
)

type ClientIface interface {
type Client interface {
ListTracks(ctx context.Context) ([]model.Track, error)
SetSpecifiedTalkOnAir(ctx context.Context, talkId int32) error
SetNextTalkOnAir(ctx context.Context, trackId int32) error
}

type Client struct {
client lib.DreamkastApi
type ClientImpl struct {
client lib.DreamkastClient

eventAbbr string
auth0Domain string
Expand All @@ -30,17 +30,17 @@ type Client struct {

func NewClient(eventAbbr, dkEndpointUrl string,
auth0Domain, auth0ClientId, auth0ClientSecret, auth0Audience string,
) (ClientIface, error) {
) (Client, error) {
c, err := lib.NewClient(dkEndpointUrl)
if err != nil {
return nil, err
}
return &Client{
return &ClientImpl{
c, eventAbbr, auth0Domain, auth0ClientId, auth0ClientSecret, auth0Audience,
}, nil
}

func (c *Client) ListTracks(ctx context.Context) ([]model.Track, error) {
func (c *ClientImpl) ListTracks(ctx context.Context) ([]model.Track, error) {
logger := utils.GetLogger(ctx)

tracks, err := c.client.ListTracks(ctx, c.eventAbbr)
Expand Down Expand Up @@ -88,7 +88,7 @@ func (c *Client) ListTracks(ctx context.Context) ([]model.Track, error) {
return result, nil
}

func (c *Client) SetSpecifiedTalkOnAir(ctx context.Context, talkId int32) error {
func (c *ClientImpl) SetSpecifiedTalkOnAir(ctx context.Context, talkId int32) error {
// If Auth0Token has been expired, retry only once.
err := retry.Do(
func() (err error) {
Expand All @@ -104,7 +104,7 @@ func (c *Client) SetSpecifiedTalkOnAir(ctx context.Context, talkId int32) error
return err
}

func (c *Client) setSpecifiedTalkOnAir(ctx context.Context, talkId int32) error {
func (c *ClientImpl) setSpecifiedTalkOnAir(ctx context.Context, talkId int32) error {
if err := c.client.GenerateAuth0Token(ctx,
c.auth0Domain, c.auth0ClientId, c.auth0ClientSecret, c.auth0Audience,
); err != nil {
Expand All @@ -117,7 +117,7 @@ func (c *Client) setSpecifiedTalkOnAir(ctx context.Context, talkId int32) error
return nil
}

func (c *Client) SetNextTalkOnAir(ctx context.Context, trackId int32) error {
func (c *ClientImpl) SetNextTalkOnAir(ctx context.Context, trackId int32) error {
// If Auth0Token has been expired, retry only once.
err := retry.Do(
func() (err error) {
Expand All @@ -133,7 +133,7 @@ func (c *Client) SetNextTalkOnAir(ctx context.Context, trackId int32) error {
return err
}

func (c *Client) setNextTalkOnAir(ctx context.Context, trackId int32) error {
func (c *ClientImpl) setNextTalkOnAir(ctx context.Context, trackId int32) error {
if err := c.client.GenerateAuth0Token(ctx,
c.auth0Domain, c.auth0ClientId, c.auth0ClientSecret, c.auth0Audience,
); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@ import (
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils"
)

type PrimitiveClient struct {
type DreamkastClient interface {
GenerateAuth0Token(ctx context.Context, auth0Domain, auth0ClientId, auth0ClientSecret, auth0Audience string) error
ListTracks(ctx context.Context, eventAbbr string) (ListTracksResp, error)
ListTalks(ctx context.Context, eventAbbr string, trackId int32) (ListTalksResp, error)
UpdateTalks(ctx context.Context, talkId int32, onAir bool) error
}

type DreamkastClientImpl struct {
client *http.Client
dkEndpointUrl url.URL
auth0Token string
}

func NewClient(dkEndpointUrl string) (DreamkastApi, error) {
func NewClient(dkEndpointUrl string) (DreamkastClient, error) {
dkUrl, err := url.Parse(dkEndpointUrl)
if err != nil {
return nil, err
}
return &PrimitiveClient{client: http.DefaultClient, dkEndpointUrl: *dkUrl}, nil
return &DreamkastClientImpl{client: http.DefaultClient, dkEndpointUrl: *dkUrl}, nil
}

func (c *PrimitiveClient) GenerateAuth0Token(ctx context.Context,
func (c *DreamkastClientImpl) GenerateAuth0Token(ctx context.Context,
auth0Domain, auth0ClientId, auth0ClientSecret, auth0Audience string,
) error {
if c.auth0Token != "" {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
)

func (c *PrimitiveClient) ListTalks(ctx context.Context, eventAbbr string, trackId int32) (ListTalksResp, error) {
func (c *DreamkastClientImpl) ListTalks(ctx context.Context, eventAbbr string, trackId int32) (ListTalksResp, error) {
url := c.dkEndpointUrl
url.Path = filepath.Join(url.Path, "/v1/talks")
req, err := http.NewRequest("GET", url.String(), nil)
Expand Down Expand Up @@ -37,7 +37,7 @@ func (c *PrimitiveClient) ListTalks(ctx context.Context, eventAbbr string, track

// TODO: set Token to header
// TODO: write test
func (c *PrimitiveClient) UpdateTalks(ctx context.Context, talkId int32, onAir bool) error {
func (c *DreamkastClientImpl) UpdateTalks(ctx context.Context, talkId int32, onAir bool) error {
url := c.dkEndpointUrl
url.Path = filepath.Join(url.Path, "/v1/talks", strconv.Itoa(int(talkId)))
reqBody, err := json.Marshal(&UpdateTalksReq{OnAir: onAir})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestPrimitiveClient_ListTalks(t *testing.T) {
if err != nil {
t.Fatal(err)
}
c := &PrimitiveClient{
c := &DreamkastClientImpl{
client: http.DefaultClient,
dkEndpointUrl: *u,
}
Expand All @@ -42,7 +42,7 @@ func TestPrimitiveClient_ListTalks(t *testing.T) {

got, err := c.ListTalks(ctx, dkEventAbbr, int32(trackId))
if err != nil {
t.Errorf("PrimitiveClient.ListTalks() error = %v", err)
t.Errorf("DreamkastClientImpl.ListTalks() error = %v", err)
return
}
pp.Print(got)
Expand All @@ -66,7 +66,7 @@ func TestPrimitiveClient_UpdateTalks(t *testing.T) {
if err != nil {
t.Fatal(err)
}
c := &PrimitiveClient{
c := &DreamkastClientImpl{
client: http.DefaultClient,
dkEndpointUrl: *u,
}
Expand All @@ -80,7 +80,7 @@ func TestPrimitiveClient_UpdateTalks(t *testing.T) {
}

if err := c.UpdateTalks(ctx, int32(talkId), true); err != nil {
t.Errorf("PrimitiveClient.UpdateTalks() error = %v", err)
t.Errorf("DreamkastClientImpl.UpdateTalks() error = %v", err)
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
)

func (c *PrimitiveClient) ListTracks(ctx context.Context, eventAbbr string) (ListTracksResp, error) {
func (c *DreamkastClientImpl) ListTracks(ctx context.Context, eventAbbr string) (ListTracksResp, error) {
url := c.dkEndpointUrl
url.Path = filepath.Join(url.Path, "/v1/tracks")
req, err := http.NewRequest("GET", url.String(), nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func TestPrimitiveClient_ListTracks(t *testing.T) {
if err != nil {
t.Fatal(err)
}
c := &PrimitiveClient{
c := &DreamkastClientImpl{
client: http.DefaultClient,
dkEndpointUrl: *u,
}

got, err := c.ListTracks(ctx, dkEventAbbr)
if err != nil {
t.Errorf("PrimitiveClient.ListTracks() error = %v", err)
t.Errorf("DreamkastClientImpl.ListTracks() error = %v", err)
return
}
pp.Print(got)
Expand Down
20 changes: 10 additions & 10 deletions pkg/infrastructure/obsws/client.go → pkg/infra/obsws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import (
"github.com/andreykaipov/goobs/api/requests/scenes"
"golang.org/x/xerrors"

"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/obsws/lib"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/obsws/lib"
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils"
)

type ClientIface interface {
type Client interface {
GetHost() string
ListScenes(ctx context.Context) ([]Scene, error)
MoveSceneToNext(ctx context.Context) error
GetRemainingTimeOnCurrentScene(ctx context.Context) (*DurationAndCursor, error)
}

type Client struct {
client lib.ObsWsApi
type ClientImpl struct {
client lib.ObsWsClient

host string
password string
}

func NewObsWebSocketClient(host, password string) (ClientIface, error) {
func NewObsWebSocketClient(host, password string) (Client, error) {
c := lib.NewClient()
if err := c.GenerateClient(host, password); err != nil {
return nil, err
}
return &Client{c, host, password}, nil
return &ClientImpl{c, host, password}, nil
}

func (c *Client) GetHost() string {
func (c *ClientImpl) GetHost() string {
return c.host
}

Expand All @@ -46,7 +46,7 @@ type Scene struct {
}

// ListScenes is output list of scenes. It is sorted order by as shown in OBS.
func (c *Client) ListScenes(ctx context.Context) ([]Scene, error) {
func (c *ClientImpl) ListScenes(ctx context.Context) ([]Scene, error) {
if err := c.client.GenerateClient(c.host, c.password); err != nil {
return nil, err
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func (c *Client) ListScenes(ctx context.Context) ([]Scene, error) {
return scenes, nil
}

func (c *Client) MoveSceneToNext(ctx context.Context) error {
func (c *ClientImpl) MoveSceneToNext(ctx context.Context) error {
logger := utils.GetLogger(ctx)
if err := c.client.GenerateClient(c.host, c.password); err != nil {
return err
Expand Down Expand Up @@ -120,7 +120,7 @@ type DurationAndCursor struct {
CursorMilliSecond float64
}

func (c *Client) GetRemainingTimeOnCurrentScene(ctx context.Context) (*DurationAndCursor, error) {
func (c *ClientImpl) GetRemainingTimeOnCurrentScene(ctx context.Context) (*DurationAndCursor, error) {
_ = utils.GetLogger(ctx)
if err := c.client.GenerateClient(c.host, c.password); err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/andreykaipov/goobs/api/requests/scenes"
)

type ObsWsApi interface {
type ObsWsClient interface {
GenerateClient(host, password string) error
MediaInputs() ObsWsMediaInputsApi
Scenes() ObsWsScenesApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"golang.org/x/xerrors"
)

type PrimitiveClient struct {
type ObsWsClientWrapper struct {
client *goobs.Client
}

func NewClient() ObsWsApi {
return &PrimitiveClient{}
func NewClient() ObsWsClient {
return &ObsWsClientWrapper{}
}

func (c *PrimitiveClient) GenerateClient(host, password string) error {
func (c *ObsWsClientWrapper) GenerateClient(host, password string) error {
if c.client != nil {
return nil
}
Expand All @@ -25,14 +25,14 @@ func (c *PrimitiveClient) GenerateClient(host, password string) error {
return nil
}

func (c *PrimitiveClient) MediaInputs() ObsWsMediaInputsApi {
func (c *ObsWsClientWrapper) MediaInputs() ObsWsMediaInputsApi {
return c.client.MediaInputs
}

func (c *PrimitiveClient) Scenes() ObsWsScenesApi {
func (c *ObsWsClientWrapper) Scenes() ObsWsScenesApi {
return c.client.Scenes
}

func (c *PrimitiveClient) SceneItems() ObsWsSceneItemsApi {
func (c *ObsWsClientWrapper) SceneItems() ObsWsSceneItemsApi {
return c.client.SceneItems
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions pkg/infrastructure/slack/slack.go → pkg/infra/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import (
"golang.org/x/xerrors"
)

type ClientIface interface {
type Client interface {
PostMessage(ctx context.Context, channel string, msg slack.Msg) error
}

type Client struct {
type ClientImpl struct {
client *slack.Client
botUserId string
}

func NewClient(botToken string) (ClientIface, error) {
func NewClient(botToken string) (Client, error) {
client := slack.New(botToken)
res, err := client.AuthTest()
if err != nil {
return nil, err
}

return &Client{client, res.UserID}, nil
return &ClientImpl{client, res.UserID}, nil
}

func (s *Client) PostMessage(ctx context.Context, channel string, msg slack.Msg) error {
func (s *ClientImpl) PostMessage(ctx context.Context, channel string, msg slack.Msg) error {
_, _, err := s.client.PostMessageContext(ctx, channel,
slack.MsgOptionText(msg.Text, false),
slack.MsgOptionAttachments(msg.Attachments...),
Expand Down
10 changes: 0 additions & 10 deletions pkg/infrastructure/dreamkast/lib/iface.go

This file was deleted.

Loading

0 comments on commit 10a586f

Please sign in to comment.