add subcomands for notifications #283

Closed
khmarbaise wants to merge 7 commits from khmarbaise/tea:issue-243-add_subcomands_for_notifications into master
  1. 11
      cmd/flags/flags.go
  2. 34
      cmd/notifications.go
  3. 61
      cmd/notifications/list.go
  4. 54
      cmd/notifications/notifications_list.go
  5. 27
      cmd/notifications/pinned.go
  6. 27
      cmd/notifications/read.go
  7. 27
      cmd/notifications/unread.go

11
cmd/flags/flags.go

@ -91,3 +91,14 @@ var IssuePRFlags = append([]cli.Flag{ @@ -91,3 +91,14 @@ var IssuePRFlags = append([]cli.Flag{
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...)
// NotificationFlags defines flags that should be available on notifications.
var NotificationFlags = append([]cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Usage: "Show notifications across all your repositories instead of the current repository only",
},
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...)

34
cmd/notifications.go

@ -5,7 +5,9 @@ @@ -5,7 +5,9 @@
package cmd
import (
"code.gitea.io/tea/cmd/flags"
"log"
"code.gitea.io/tea/cmd/notifications"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/print"
@ -18,27 +20,26 @@ var CmdNotifications = cli.Command{ @@ -18,27 +20,26 @@ var CmdNotifications = cli.Command{
Name: "notifications",
Aliases: []string{"notification", "n"},
Usage: "Show notifications",
Description: "Show notifications, by default based of the current repo and unread one",
Description: "Show notifications, by default based of the current repo",
Action: runNotifications,
Subcommands: []*cli.Command{
&notifications.CmdNotificationsList,
&notifications.CmdNotificationsPinned,
&notifications.CmdNotificationsRead,
&notifications.CmdNotificationsUnread,
},
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Usage: "show all notifications of related gitea instance",
},
&cli.BoolFlag{
Name: "read",
Aliases: []string{"rd"},
Usage: "show read notifications instead unread",
},
&cli.BoolFlag{
Name: "pinned",
Aliases: []string{"pd"},
Usage: "show pinned notifications instead unread",
&cli.StringFlag{
Name: "state",
Usage: "set milestone state (default is open)",
DefaultText: "open",
},
&flags.PaginationPageFlag,
&flags.PaginationLimitFlag,
}, flags.AllDefaultFlags...),
}),
}
func runNotifications(cmd *cli.Context) error {
@ -80,3 +81,8 @@ func runNotifications(cmd *cli.Context) error { @@ -80,3 +81,8 @@ func runNotifications(cmd *cli.Context) error {
print.NotificationsList(news, ctx.Output, ctx.Bool("all"))
return nil
}
func runNotificationsDetails(ctx *cli.Context) error {
log.Fatal("Use tea notif --help to see all available commands.")
return nil
}

61
cmd/notifications/list.go

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package notifications
import (
"code.gitea.io/sdk/gitea"
"fmt"
"github.com/urfave/cli/v2"
)
// CmdNotificationsList represents a sub command of notifications to list notifications
var CmdNotificationsList = cli.Command{
Name: "ls",
Aliases: []string{"list"},
Usage: "List notifications",
Description: `List notifications`,
Action: RunNotificationsList,
// Flags: flags.NotificationFlags,
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Usage: "show all notifications of related gitea instance",
},
&cli.StringFlag{
Name: "state",
Usage: "set notification state (default is all), pinned,read,unread",
DefaultText: "xxx",
},
}),
}
// notif ls
// notif ls --state all
// notif ls --state pinned
// notif ls --state read
// notif ls --state unread
// RunNotificationsList list notifications
func RunNotificationsList(ctx *cli.Context) error {
// --states all
states := []gitea.NotifyStatus{}
switch ctx.String("state") {
case "all":
states = []gitea.NotifyStatus{gitea.NotifyStatusPinned, gitea.NotifyStatusRead, gitea.NotifyStatusUnread}
case "pinned":
states = []gitea.NotifyStatus{gitea.NotifyStatusPinned}
case "read":
states = []gitea.NotifyStatus{gitea.NotifyStatusRead}
case "unread":
states = []gitea.NotifyStatus{gitea.NotifyStatusUnread}
default:
return fmt.Errorf("invalid notification state type '%s'. valid: all, pinned,read,unread", ctx.String("state"))
}
return listNotifications(ctx, states)
}

54
cmd/notifications/notifications_list.go

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package notifications
import (
"fmt"
"log"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/print"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli/v2"
)
//listNotifications will get the notifications based on status
func listNotifications(ctx *cli.Context, status []gitea.NotifyStatus) error {
//This enforces pagination.
listOpts := flags.GetListOptions(ctx)
if listOpts.Page == 0 {
listOpts.Page = 1
}
var news []*gitea.NotificationThread
var err error
var allRelated = ctx.Bool("all")
fmt.Printf("allRelated: %t\n", allRelated)
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
if allRelated {
fmt.Printf("login: %s owner: %s repo:%s\n", login.Name, owner, repo)
news, _, err = login.Client().ListNotifications(gitea.ListNotificationOptions{
ListOptions: listOpts,
Status: status,
})
} else {
fmt.Printf("login: %s owner: %s repo:%s\n", login.Name, owner, repo)
news, _, err = login.Client().ListRepoNotifications(owner, repo, gitea.ListNotificationOptions{
ListOptions: listOpts,
Status: status,
})
}
if err != nil {
log.Fatal(err)
}
print.NotificationsList(news, flags.GlobalOutputValue, allRelated)
return nil
}

27
cmd/notifications/pinned.go

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package notifications
import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsPinned represents a sub command of notifications to list pinned notifications
var CmdNotificationsPinned = cli.Command{
Name: "pinned",
Aliases: []string{"pd"},
Usage: "show pinned notifications",
Description: `show pinned notifications`,
Action: RunNotificationsPinned,
Flags: flags.NotificationFlags,
}
// RunNotificationsPinned will show notifications with status pinned.
func RunNotificationsPinned(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned}
return listNotifications(ctx, statuses)
}

27
cmd/notifications/read.go

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package notifications
import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsRead represents a sub command of notifications to list read notifications
var CmdNotificationsRead = cli.Command{
Name: "read",
Aliases: []string{},
Usage: "show read notifications instead",
Description: `show read notifications instead`,
Action: RunNotificationsRead,
Flags: flags.NotificationFlags,
}
// RunNotificationsRead will show notifications with status read.
func RunNotificationsRead(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead}
return listNotifications(ctx, statuses)
}

27
cmd/notifications/unread.go

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package notifications
import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsUnread represents a sub command of notifications to list unread notifications.
var CmdNotificationsUnread = cli.Command{
Name: "unread",
Aliases: []string{},
Usage: "show unread notifications",
Description: `show unread notifications`,
Action: RunNotificationsUnread,
Flags: flags.NotificationFlags,
}
// RunNotificationsUnread will show notifications with status unread.
func RunNotificationsUnread(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread}
return listNotifications(ctx, statuses)
}
Loading…
Cancel
Save