Skip to content

Commit 2cdb61b

Browse files
committed
Add a flag to use a different base URL than api.tidbyt.com.
1 parent 9674b0c commit 2cdb61b

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

cmd/delete.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
"tidbyt.dev/pixlet/cmd/config"
1111
)
1212

13-
const (
14-
TidbytAPIDelete = "https://api.tidbyt.com/v0/devices/%s/installations/%s"
15-
)
13+
var deleteURL string
1614

1715
func init() {
1816
DeleteCmd.Flags().StringVarP(&apiToken, "api-token", "t", "", "Tidbyt API token")
17+
DeleteCmd.Flags().StringVarP(&deleteURL, "url", "u", "https://api.tidbyt.com", "base URL of Tidbyt API")
1918
}
2019

2120
var DeleteCmd = &cobra.Command{
@@ -44,7 +43,7 @@ func delete(cmd *cobra.Command, args []string) error {
4443
client := &http.Client{}
4544
req, err := http.NewRequest(
4645
"DELETE",
47-
fmt.Sprintf(TidbytAPIDelete, deviceID, installationID),
46+
fmt.Sprintf("%s/v0/devices/%s/installations/%s", deleteURL, deviceID, installationID),
4847
nil,
4948
)
5049
if err != nil {

cmd/devices.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
"tidbyt.dev/pixlet/cmd/config"
1212
)
1313

14-
const (
15-
TidbytAPIListDevices = "https://api.tidbyt.com/v0/devices"
16-
)
14+
var devicesURL string
15+
16+
func init() {
17+
DevicesCmd.Flags().StringVarP(&devicesURL, "url", "u", "https://api.tidbyt.com", "base URL of Tidbyt API")
18+
}
1719

1820
var DevicesCmd = &cobra.Command{
1921
Use: "devices",
@@ -29,7 +31,7 @@ func devices(cmd *cobra.Command, args []string) {
2931
}
3032

3133
client := &http.Client{}
32-
req, err := http.NewRequest("GET", TidbytAPIListDevices, nil)
34+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v0/devices", devicesURL), nil)
3335
if err != nil {
3436
fmt.Printf("creating GET request: %v\n", err)
3537
os.Exit(1)

cmd/list.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"tidbyt.dev/pixlet/cmd/config"
1313
)
1414

15-
const (
16-
TidbytAPIList = "https://api.tidbyt.com/v0/devices/%s/installations"
15+
var (
16+
listURL string
1717
)
1818

1919
type TidbytInstallationJSON struct {
@@ -27,6 +27,7 @@ type TidbytInstallationListJSON struct {
2727

2828
func init() {
2929
ListCmd.Flags().StringVarP(&apiToken, "api-token", "t", "", "Tidbyt API token")
30+
ListCmd.Flags().StringVarP(&listURL, "url", "u", "https://api.tidbyt.com", "base URL of Tidbyt API")
3031
}
3132

3233
var ListCmd = &cobra.Command{
@@ -54,7 +55,7 @@ func listInstallations(cmd *cobra.Command, args []string) error {
5455
client := &http.Client{}
5556
req, err := http.NewRequest(
5657
"GET",
57-
fmt.Sprintf(TidbytAPIList, deviceID), nil)
58+
fmt.Sprintf("%s/v0/devices/%s/installations", listURL, deviceID), nil)
5859
if err != nil {
5960
return fmt.Errorf("creating GET request: %w", err)
6061
}

cmd/push.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
)
1515

1616
const (
17-
TidbytAPIPush = "https://api.tidbyt.com/v0/devices/%s/push"
18-
APITokenEnv = "TIDBYT_API_TOKEN"
17+
APITokenEnv = "TIDBYT_API_TOKEN"
1918
)
2019

2120
var (
2221
apiToken string
2322
installationID string
2423
background bool
24+
pushURL string
2525
)
2626

2727
type TidbytPushJSON struct {
@@ -35,6 +35,7 @@ func init() {
3535
PushCmd.Flags().StringVarP(&apiToken, "api-token", "t", "", "Tidbyt API token")
3636
PushCmd.Flags().StringVarP(&installationID, "installation-id", "i", "", "Give your installation an ID to keep it in the rotation")
3737
PushCmd.Flags().BoolVarP(&background, "background", "b", false, "Don't immediately show the image on the device")
38+
PushCmd.Flags().StringVarP(&pushURL, "url", "u", "https://api.tidbyt.com", "base URL of Tidbyt API")
3839
}
3940

4041
var PushCmd = &cobra.Command{
@@ -49,14 +50,14 @@ func push(cmd *cobra.Command, args []string) error {
4950
image := args[1]
5051

5152
// TODO (mark): This is better served as a flag, but I don't want to break
52-
// folks in the short term. We should consider dropping this as an arguement
53+
// folks in the short term. We should consider dropping this as an argument
5354
// in a future release.
5455
if len(args) == 3 {
5556
installationID = args[2]
5657
}
5758

5859
if background && len(installationID) == 0 {
59-
return fmt.Errorf("Background push won't do anything unless you also specify an installation ID")
60+
return fmt.Errorf("background push won't do anything unless you also specify an installation ID")
6061
}
6162

6263
if apiToken == "" {
@@ -91,7 +92,7 @@ func push(cmd *cobra.Command, args []string) error {
9192
client := &http.Client{}
9293
req, err := http.NewRequest(
9394
"POST",
94-
fmt.Sprintf(TidbytAPIPush, deviceID),
95+
fmt.Sprintf("%s/v0/devices/%s/push", pushURL, deviceID),
9596
bytes.NewReader(payload),
9697
)
9798
if err != nil {

0 commit comments

Comments
 (0)