-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
240 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2022, NewReleases CLI AUTHORS. | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"newreleases.io/newreleases" | ||
) | ||
|
||
func (c *command) initMatrixCmd() (err error) { | ||
cmd := &cobra.Command{ | ||
Use: "matrix", | ||
Short: "List Matrix integrations", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
ctx, cancel := newClientContext(c.config) | ||
defer cancel() | ||
|
||
rooms, err := c.matrixRoomsService.List(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(rooms) == 0 { | ||
cmd.Println("No Matrix Rooms found.") | ||
return nil | ||
} | ||
|
||
printMatrixRoomsTable(cmd, rooms) | ||
|
||
return nil | ||
}, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
if err := addClientConfigOptions(cmd, c.config); err != nil { | ||
return err | ||
} | ||
return c.setMatrixRoomsService(cmd, args) | ||
}, | ||
} | ||
|
||
c.root.AddCommand(cmd) | ||
return addClientFlags(cmd) | ||
} | ||
|
||
func (c *command) setMatrixRoomsService(cmd *cobra.Command, args []string) (err error) { | ||
if c.matrixRoomsService != nil { | ||
return nil | ||
} | ||
client, err := c.getClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
c.matrixRoomsService = client.MatrixRooms | ||
return nil | ||
} | ||
|
||
type matrixRoomsService interface { | ||
List(ctx context.Context) (rooms []newreleases.MatrixRoom, err error) | ||
} | ||
|
||
func printMatrixRoomsTable(cmd *cobra.Command, rooms []newreleases.MatrixRoom) { | ||
table := newTable(cmd.OutOrStdout()) | ||
table.SetHeader([]string{"ID", "Name", "Homeserver URL", "Internal Room ID"}) | ||
for _, e := range rooms { | ||
table.Append([]string{e.ID, e.Name, e.HomeserverURL, e.InternalRoomID}) | ||
} | ||
table.Render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) 2022, NewReleases CLI AUTHORS. | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
|
||
"newreleases.io/cmd/newreleases/cmd" | ||
"newreleases.io/newreleases" | ||
) | ||
|
||
func TestMatrixCmd(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
matrixRoomsService cmd.MatrixRoomsService | ||
wantOutput string | ||
wantError error | ||
}{ | ||
{ | ||
name: "no rooms", | ||
matrixRoomsService: newMockMatrixRoomsService(nil, nil), | ||
wantOutput: "No Matrix Rooms found.\n", | ||
}, | ||
{ | ||
name: "with rooms", | ||
matrixRoomsService: newMockMatrixRoomsService([]newreleases.MatrixRoom{ | ||
{ | ||
ID: "znne04qO5y6acw7sg5a9b1pc9t16rpym8jwn", | ||
Name: "NewReleases", | ||
HomeserverURL: "https://matrix-client.matrix.org", | ||
InternalRoomID: "!CklbIcwhYKygGsulFi:matrix.org", | ||
}, | ||
{ | ||
ID: "9t4qOp16gmcw7z8jwrpyc6anne0sn5y5a9b1", | ||
Name: "Awesome project", | ||
HomeserverURL: "https://matrix-client.example.com", | ||
InternalRoomID: "!CkGsIcwhKyulFYlbgi:example.com", | ||
}, | ||
}, nil), | ||
wantOutput: "ID NAME HOMESERVER URL INTERNAL ROOM ID \nznne04qO5y6acw7sg5a9b1pc9t16rpym8jwn NewReleases https://matrix-client.matrix.org !CklbIcwhYKygGsulFi:matrix.org \n9t4qOp16gmcw7z8jwrpyc6anne0sn5y5a9b1 Awesome project https://matrix-client.example.com !CkGsIcwhKyulFYlbgi:example.com \n", | ||
}, | ||
{ | ||
name: "error", | ||
matrixRoomsService: newMockMatrixRoomsService(nil, errTest), | ||
wantError: errTest, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var outputBuf bytes.Buffer | ||
if err := newCommand(t, | ||
cmd.WithArgs("matrix"), | ||
cmd.WithOutput(&outputBuf), | ||
cmd.WithMatrixRoomsService(tc.matrixRoomsService), | ||
).Execute(); err != tc.wantError { | ||
t.Fatalf("got error %v, want %v", err, tc.wantError) | ||
} | ||
|
||
gotOutput := outputBuf.String() | ||
if gotOutput != tc.wantOutput { | ||
t.Errorf("got output %q, want %q", gotOutput, tc.wantOutput) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type mockMatrixRoomsService struct { | ||
rooms []newreleases.MatrixRoom | ||
err error | ||
} | ||
|
||
func newMockMatrixRoomsService(rooms []newreleases.MatrixRoom, err error) mockMatrixRoomsService { | ||
return mockMatrixRoomsService{rooms: rooms, err: err} | ||
} | ||
|
||
func (s mockMatrixRoomsService) List(ctx context.Context) ([]newreleases.MatrixRoom, error) { | ||
return s.rooms, s.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.