Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut committed Aug 14, 2024
0 parents commit 0b9f848
Show file tree
Hide file tree
Showing 29 changed files with 2,054 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Tests
on:
pull_request:
branches: [main]

push:
branches: [main]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
go: ["1.22"]
os: [ubuntu-latest, macOS-latest, windows-latest]
name: ${{ matrix.os }} Go ${{ matrix.go }} Tests
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: true
- name: Run tests
run: go test ./... -v -timeout 30s
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.DS_Store
bin/

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go workspace file
go.work
go.work.sum

# env file
.env
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Radu Lucuț

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# cleed

Simple feed reader for the command line.

![Test](https://github.com/radulucut/cleed/actions/workflows/tests.yml/badge.svg)

## Usage

#### Follow a feed

```bash
# Add a feed to the default list
cleed follow https://example.com/feed.xml

# Add multiple feeds to a list
cleed follow https://example.com/feed.xml https://example2.com/feed --list mylist
```

#### Display feeds

```bash
# Display feeds from all lists
cleed

# Display feeds from a specific list
cleed --list my-list

# Display feeds from the last 1 day
cleed --last 1d

# Display feeds since a specific date
cleed --since "2024-01-01 12:03:04"

# Display feeds from a specific list and limit the number of feeds
cleed --list my-list --limit 10
```

#### Unfollow a feed

```bash
# Remove a feed from the default list
cleed unfollow https://example.com/feed.xml

# Remove multiple feeds from a list
cleed unfollow https://example.com/feed.xml https://example2.com/feed --list mylist
```

#### List feeds

```bash
# Show all lists
cleed list

# Show all feeds in a list
cleed list mylist
```
36 changes: 36 additions & 0 deletions cmd/cleed/follow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"github.com/spf13/cobra"
)

func (r *Root) initFollow() {
cmd := &cobra.Command{
Use: "follow [feed]",
Short: "Follow a feed",
Long: `Follow a feed
Examples:
# Add a feed to the default list
cleed follow https://example.com/feed.xml
# Add multiple feeds to a list
cleed follow https://example.com/feed.xml https://example2.com/feed --list mylist
`,
RunE: r.RunFollow,
Args: cobra.MinimumNArgs(1),
}

flags := cmd.Flags()
flags.StringP("list", "L", "default", "the list to add the feed to")

r.Cmd.AddCommand(cmd)
}

func (r *Root) RunFollow(cmd *cobra.Command, args []string) error {
list, err := cmd.Flags().GetString("list")
if err != nil {
return err
}
return r.feed.Follow(args, list)
}
139 changes: 139 additions & 0 deletions cmd/cleed/follow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"bytes"
"fmt"
"os"
"path"
"testing"

"github.com/radulucut/cleed/internal"
"github.com/radulucut/cleed/internal/storage"
"github.com/radulucut/cleed/mocks"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func Test_Follow_Default(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

timeMock := mocks.NewMockTime(ctrl)
timeMock.EXPECT().Now().Return(defaultCurrentTime).AnyTimes()

out := new(bytes.Buffer)
printer := internal.NewPrinter(nil, out, out)
storage := storage.NewLocalStorage("cleed_test", timeMock)
defer localStorageCleanup(t, storage)

feed := internal.NewTerminalFeed(timeMock, printer, storage)
feed.SetAgent("cleed/test")

root, err := NewRoot("0.1.0", timeMock, printer, storage, feed)
assert.NoError(t, err)

os.Args = []string{"cleed", "follow", "https://example.com", "https://test.com"}

err = root.Cmd.Execute()
assert.NoError(t, err)
assert.Equal(t, "added 2 feeds to list: default\n", out.String())

configDir, err := os.UserConfigDir()
if err != nil {
t.Fatal(err)
}
listsDir := path.Join(configDir, "cleed_test", "lists")
files, err := os.ReadDir(listsDir)
if err != nil {
t.Fatal(err)
}
assert.Len(t, files, 1)
file := files[0]
assert.Equal(t, "default", file.Name())
b, err := os.ReadFile(path.Join(listsDir, file.Name()))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, fmt.Sprintf("%d %s\n%d %s\n",
defaultCurrentTime.Unix(), "https://example.com",
defaultCurrentTime.Unix(), "https://test.com",
), string(b))
}

func Test_Follow_Custom_List(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

timeMock := mocks.NewMockTime(ctrl)
timeMock.EXPECT().Now().Return(defaultCurrentTime).AnyTimes()

out := new(bytes.Buffer)
printer := internal.NewPrinter(nil, out, out)
storage := storage.NewLocalStorage("cleed_test", timeMock)
defer localStorageCleanup(t, storage)

feed := internal.NewTerminalFeed(timeMock, printer, storage)
feed.SetAgent("cleed/test")

root, err := NewRoot("0.1.0", timeMock, printer, storage, feed)
assert.NoError(t, err)

os.Args = []string{"cleed", "follow", "--list", "test", "https://example.com"}

err = root.Cmd.Execute()
assert.NoError(t, err)
assert.Equal(t, "added 1 feed to list: test\n", out.String())

configDir, err := os.UserConfigDir()
if err != nil {
t.Fatal(err)
}
listsDir := path.Join(configDir, "cleed_test", "lists")
files, err := os.ReadDir(listsDir)
if err != nil {
t.Fatal(err)
}
assert.Len(t, files, 1)
file := files[0]
assert.Equal(t, "test", file.Name())
b, err := os.ReadFile(path.Join(listsDir, file.Name()))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, fmt.Sprintf("%d %s\n", defaultCurrentTime.Unix(), "https://example.com"), string(b))
}

func Test_Follow_Invalid_URL(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

timeMock := mocks.NewMockTime(ctrl)
timeMock.EXPECT().Now().Return(defaultCurrentTime).AnyTimes()

out := new(bytes.Buffer)
printer := internal.NewPrinter(nil, out, out)
storage := storage.NewLocalStorage("cleed_test", timeMock)
defer localStorageCleanup(t, storage)

feed := internal.NewTerminalFeed(timeMock, printer, storage)
feed.SetAgent("cleed/test")

root, err := NewRoot("0.1.0", timeMock, printer, storage, feed)
assert.NoError(t, err)

os.Args = []string{"cleed", "follow", "invalid", "https://test.com"}

err = root.Cmd.Execute()
assert.EqualError(t, err, "failed to parse URL: invalid")

configDir, err := os.UserConfigDir()
if err != nil {
t.Fatal(err)
}
listsDir := path.Join(configDir, "cleed_test", "lists")
files, err := os.ReadDir(listsDir)
if err != nil {
t.Fatal(err)
}
assert.Len(t, files, 0)
}
31 changes: 31 additions & 0 deletions cmd/cleed/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import "github.com/spf13/cobra"

func (r *Root) initList() {
cmd := &cobra.Command{
Use: "list",
Short: "Show all lists or feeds in a list",
Long: `Show all lists or feeds in a list
Examples:
# Show all lists
cleed list
# Show all feeds in a list
cleed list mylist
`,

RunE: r.RunList,
Args: cobra.MaximumNArgs(1),
}

r.Cmd.AddCommand(cmd)
}

func (r *Root) RunList(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return r.feed.Lists()
}
return r.feed.ListFeeds(args[0])
}
Loading

0 comments on commit 0b9f848

Please sign in to comment.