Skip to content

Commit 5e032a2

Browse files
committed
Monday catchup commit
1 parent 95105e9 commit 5e032a2

Some content is hidden

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

73 files changed

+3533
-331
lines changed

.goreleaser.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ before:
55
- go mod download
66
builds:
77
- id: twitch-darwin
8-
ldflags: -X main.buildVersion={{ .Version }}
8+
ldflags:
9+
- -s -w -X main.buildVersion={{ .Version }}
910
binary: twitch
1011
env:
1112
- CGO_ENABLED=1
@@ -16,7 +17,8 @@ builds:
1617
goarch:
1718
- amd64
1819
- id: twitch-linux
19-
ldflags: -X main.buildVersion={{ .Version }}
20+
ldflags:
21+
- -s -w -X main.buildVersion={{ .Version }}
2022
binary: twitch
2123
env:
2224
- CGO_ENABLED=1
@@ -25,7 +27,8 @@ builds:
2527
goarch:
2628
- amd64
2729
- id: twitch-windows-x64
28-
ldflags: -X main.buildVersion={{ .Version }}
30+
ldflags:
31+
- -s -w -X main.buildVersion={{ .Version }}
2932
binary: twitch
3033
main: ./main.go
3134
env:
@@ -37,7 +40,8 @@ builds:
3740
goarch:
3841
- amd64
3942
- id: twitch-windows-i386
40-
ldflags: -X main.buildVersion={{ .Version }}
43+
ldflags:
44+
- -s -w -X main.buildVersion={{ .Version }}
4145
binary: twitch
4246
main: ./main.go
4347
env:

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ test-release:
1717
twitch-cli:latest --rm-dist --skip-publish --snapshot
1818

1919
build:
20-
go build --ldflags "-X main.buildVersion=source"
20+
go build --ldflags "-s -w -X main.buildVersion=source"
2121

2222
build_all:
23-
xgo -out build/twitch --targets "darwin/amd64,windows/amd64,linux/amd64" --ldflags "-X main.buildVersion=source" ./
23+
xgo -out build/twitch --targets "darwin/amd64,windows/amd64,linux/amd64" --ldflags "-s -w -X main.buildVersion=source" ./
2424

2525
clean:
2626
rm -rf ~/.twitch-cli/eventCache.db

cmd/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func init() {
9898

9999
mockCmd.AddCommand(startCmd, generateCmd)
100100

101-
startCmd.Flags().IntVarP(&port, "port", "p", 3000, "Defines the port that the mock API will run on.")
101+
startCmd.Flags().IntVarP(&port, "port", "p", 8080, "Defines the port that the mock API will run on.")
102102

103103
generateCmd.Flags().IntVarP(&count, "count", "c", 10, "Defines the number of fake users to generate.")
104104
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
77
github.com/fatih/color v1.10.0
88
github.com/hokaccha/go-prettyjson v0.0.0-20201222001619-a42f9ac2ec8e // indirect
9-
github.com/jmoiron/sqlx v1.3.3 // indirect
9+
github.com/jmoiron/sqlx v1.3.3
1010
github.com/karalabe/xgo v0.0.0-20191115072854-c5ccff8648a7 // indirect
1111
github.com/manifoldco/promptui v0.8.0
1212
github.com/mattn/go-sqlite3 v1.14.7

internal/api/api.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,16 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
100100
data = apiResponse
101101
break
102102
}
103+
d := data.Data.([]interface{})
104+
data.Data = append(d, apiResponse.Data)
103105

104-
data.Data = append(data.Data, apiResponse.Data...)
105-
106-
if autopaginate == false {
107-
data.Pagination.Cursor = apiResponse.Pagination.Cursor
106+
if apiResponse.Pagination == nil || *&apiResponse.Pagination.Cursor == "" {
108107
break
109108
}
110109

111-
if apiResponse.Pagination.Cursor == "" {
110+
// log.Printf("%v", apiResponse)
111+
if autopaginate == false {
112+
data.Pagination.Cursor = apiResponse.Pagination.Cursor
112113
break
113114
}
114115

@@ -120,7 +121,7 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
120121
}
121122

122123
// handle json marshalling better; returns empty slice vs. null
123-
if len(data.Data) == 0 && data.Error == "" {
124+
if len(data.Data.([]interface{})) == 0 && data.Error == "" {
124125
data.Data = make([]interface{}, 0)
125126
}
126127

internal/database/_schema.sql

+44-20
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ create table events (
44
to_user text not null, transport text not null,
55
timestamp text not null
66
);
7+
create table categories(
8+
id text not null primary key, category_name text not null
9+
);
710
create table users(
811
id text not null primary key, user_login text not null,
912
display_name text not null, email text not null,
1013
user_type text, broadcaster_type text,
1114
user_description text, created_at text not null,
12-
modified_at text
15+
category_id text,
16+
modified_at text,
17+
stream_language text not null default 'en',
18+
title text not null default '',
19+
delay int not null default 0,
20+
foreign key (category_id) references categories(id)
1321
);
1422
create table follows (
1523
broadcaster_id text not null,
@@ -64,54 +72,57 @@ create table moderator_actions (
6472
foreign key (broadcaster_id) references users(id),
6573
foreign key (user_id) references users(id)
6674
);
75+
create table editors (
76+
broadcaster_id text not null,
77+
user_id text not null,
78+
created_at text not null,
79+
primary key (broadcaster_id, user_id),
80+
foreign key (broadcaster_id) references users(id),
81+
foreign key (user_id) references users(id)
82+
);
6783
create table channel_points_rewards(
6884
id text not null primary key,
6985
broadcaster_id text not null,
7086
reward_image text,
7187
background_color text,
7288
is_enabled boolean not null default false,
73-
cost number not null default 0,
89+
cost int not null default 0,
7490
title text not null,
7591
reward_prompt text,
7692
is_user_input_required boolean default false,
7793
stream_max_enabled boolean default false,
78-
stream_max_count number default 0,
94+
stream_max_count int default 0,
7995
stream_user_max_enabled boolean default false,
80-
stream_user_max_count number default 0,
96+
stream_user_max_count int default 0,
8197
global_cooldown_enabled boolean default false,
82-
global_cooldown_seconds number default 0,
98+
global_cooldown_seconds int default 0,
8399
is_paused boolean default false,
84100
is_in_stock boolean default true,
85101
should_redemptions_skip_queue boolean default false,
86-
redemptions_redeemed_current_stream number,
102+
redemptions_redeemed_current_stream int,
87103
cooldown_expires_at text,
88104
foreign key (broadcaster_id) references users(id)
89105
);
90106
create table channel_points_redemptions(
91107
id text not null primary key,
92108
reward_id text not null,
93109
broadcaster_id text not null,
110+
user_id text not null,
94111
user_input text,
95112
redemption_status text not null,
96113
redeemed_at text,
97114
foreign key (reward_id) references channel_points_rewards(id),
98-
foreign key (broadcaster_id) references users(id)
99-
);
100-
create table categories(
101-
id text not null primary key, category_name text not null
115+
foreign key (broadcaster_id) references users(id),
116+
foreign key (user_id) references users(id)
102117
);
103118
create table streams(
104119
id text not null primary key,
105120
broadcaster_id id text not null,
106-
category_id text,
107121
stream_type text not null default 'live',
108-
title text not null,
109-
viewer_count number not null,
122+
viewer_count int not null,
110123
started_at text not null,
111-
stream_language text not null default 'en',
112124
is_mature boolean not null default false,
113-
foreign key (broadcaster_id) references users(id),
114-
foreign key (category_id) references categories(id)
125+
foreign key (broadcaster_id) references users(id)
115126
);
116127
create table tags(
117128
id text not null primary key, is_auto boolean not null default false,
@@ -145,7 +156,7 @@ create table videos(
145156
created_at text not null,
146157
published_at text,
147158
viewable text not null,
148-
view_count number not null default 0,
159+
view_count int not null default 0,
149160
duration text not null,
150161
video_language text not null default 'en',
151162
foreign key (stream_id) references streams(id),
@@ -163,8 +174,8 @@ create table stream_markers(
163174
);
164175
create table video_muted_segments (
165176
video_id text not null,
166-
video_offset number not null,
167-
duration number not null,
177+
video_offset int not null,
178+
duration int not null,
168179
primary key (video_id, video_offset),
169180
foreign key (video_id) references videos(id)
170181
);
@@ -195,7 +206,7 @@ create table clients (
195206
name text not null
196207
);
197208
create table authorizations (
198-
id integer not null primary key,
209+
id integer not null primary key AUTOINCREMENT,
199210
client_id text not null,
200211
user_id text,
201212
token text not null unique,
@@ -252,3 +263,16 @@ create table prediction_predictions (
252263
foreign key(user_id) references users(id),
253264
foreign key(prediction_id) references predictions(id)
254265
);
266+
create table clips (
267+
id text not null primary key,
268+
broadcaster_id text not null,
269+
creator_id text not null,
270+
video_id text not null,
271+
game_id text not null,
272+
title text not null,
273+
view_count int default 0,
274+
created_at text not null,
275+
duration real not null,
276+
foreign key (broadcaster_id) references users(id),
277+
foreign key (creator_id) references users(id)
278+
);

internal/database/api.go

+19
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,28 @@
22
// SPDX-License-Identifier: Apache-2.0
33
package database
44

5+
import (
6+
"log"
7+
"strings"
8+
)
9+
510
type DBResposne struct {
611
Cursor string `json:"cursor"`
712
Total int `json:"total"`
813
Limit int `json:"-"`
914
Data interface{} `json:"data"`
1015
}
16+
17+
func (c CLIDatabase) IsFirstRun() bool {
18+
var userCount = 0
19+
20+
err := c.DB.Get(&userCount, "select count(*) from users")
21+
if err != nil {
22+
if strings.Contains(err.Error(), "no rows in result set") {
23+
return true
24+
}
25+
log.Print(err)
26+
}
27+
28+
return userCount == 0
29+
}

internal/database/authentication.go

+46-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"database/sql"
88
"errors"
99
"fmt"
10+
"log"
1011
"time"
1112

1213
"github.com/twitchdev/twitch-cli/internal/util"
@@ -20,12 +21,12 @@ type AuthenticationClient struct {
2021
}
2122

2223
type Authorization struct {
23-
ID string `db:"id"`
24-
ClientID string `db:"client_id"`
25-
UserID sql.NullString `db:"user_id"`
26-
Token string `db:"token"`
27-
ExpiresAt string `db:"expires_at"`
28-
Scopes sql.NullString `db:"scopes"`
24+
ID int `db:"id" dbi:"false"`
25+
ClientID string `db:"client_id"`
26+
UserID string `db:"user_id"`
27+
Token string `db:"token"`
28+
ExpiresAt string `db:"expires_at"`
29+
Scopes string `db:"scopes"`
2930
}
3031

3132
func (q *Query) GetAuthorizationByToken(token string) (Authorization, error) {
@@ -73,15 +74,52 @@ func (q *Query) CreateAuthorization(a Authorization) (Authorization, error) {
7374
for {
7475
// loop to create unique tokens; likely won't happen, but is worth handling regardless
7576
tx := db.MustBegin()
76-
tx.NamedExec(`insert into authorizations (client_id, user_id, token, expires_at) values (:client_id, :user_id, :token, :expires_at)`, a)
77-
err := tx.Commit()
77+
stmt := generateInsertSQL("authorizations", "", a, false)
78+
_, err := tx.NamedExec(stmt, a)
79+
if err != nil {
80+
log.Print(err)
81+
return a, nil
82+
}
83+
84+
err = tx.Commit()
7885
if err == nil {
7986
return a, nil
8087
}
8188
a.Token = generateString(15)
8289
}
8390
}
8491

92+
func (q *Query) GetAuthenticationClient(ac AuthenticationClient) (*DBResposne, error) {
93+
var r []AuthenticationClient
94+
rows, err := q.DB.NamedQuery(generateSQL("select * from clients", ac, SEP_AND)+q.SQL, ac)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
for rows.Next() {
100+
var ac AuthenticationClient
101+
err := rows.StructScan(&ac)
102+
if err != nil {
103+
return nil, err
104+
}
105+
r = append(r, ac)
106+
}
107+
108+
dbr := DBResposne{
109+
Data: r,
110+
Limit: q.Limit,
111+
Total: len(r),
112+
}
113+
114+
if len(r) != q.Limit {
115+
q.PaginationCursor = ""
116+
}
117+
118+
dbr.Cursor = q.PaginationCursor
119+
120+
return &dbr, err
121+
}
122+
85123
func generateString(length int) string {
86124
b := make([]byte, length)
87125
rand.Read(b)

0 commit comments

Comments
 (0)