mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-05-31 23:59:26 +03:00
Making a dent in golint: 94 -> 70
This commit is contained in:
parent
4c5dff7960
commit
6f7410c7a0
@ -9,6 +9,8 @@ import (
|
|||||||
const historyLen = 20
|
const historyLen = 20
|
||||||
const channelBuffer = 10
|
const channelBuffer = 10
|
||||||
|
|
||||||
|
// The error returned when a message is sent to a channel that is already
|
||||||
|
// closed.
|
||||||
var ErrChannelClosed = errors.New("channel closed")
|
var ErrChannelClosed = errors.New("channel closed")
|
||||||
|
|
||||||
// Channel definition, also a Set of User Items
|
// Channel definition, also a Set of User Items
|
||||||
@ -22,7 +24,7 @@ type Channel struct {
|
|||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new channel and start broadcasting goroutine.
|
// NewChannel creates a new channel.
|
||||||
func NewChannel() *Channel {
|
func NewChannel() *Channel {
|
||||||
broadcast := make(chan Message, channelBuffer)
|
broadcast := make(chan Message, channelBuffer)
|
||||||
|
|
||||||
@ -46,7 +48,7 @@ func (ch *Channel) Close() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle a message, will block until done.
|
// HandleMsg reacts to a message, will block until done.
|
||||||
func (ch *Channel) HandleMsg(m Message) {
|
func (ch *Channel) HandleMsg(m Message) {
|
||||||
switch m := m.(type) {
|
switch m := m.(type) {
|
||||||
case *CommandMsg:
|
case *CommandMsg:
|
||||||
|
@ -8,18 +8,27 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// The error returned when an invalid command is issued.
|
||||||
var ErrInvalidCommand = errors.New("invalid command")
|
var ErrInvalidCommand = errors.New("invalid command")
|
||||||
|
|
||||||
|
// The error returned when a command is given without an owner.
|
||||||
var ErrNoOwner = errors.New("command without owner")
|
var ErrNoOwner = errors.New("command without owner")
|
||||||
|
|
||||||
|
// The error returned when a command is performed without the necessary number
|
||||||
|
// of arguments.
|
||||||
var ErrMissingArg = errors.New("missing argument")
|
var ErrMissingArg = errors.New("missing argument")
|
||||||
|
|
||||||
|
// CommandHandler is the function signature for command handlers..
|
||||||
type CommandHandler func(*Channel, CommandMsg) error
|
type CommandHandler func(*Channel, CommandMsg) error
|
||||||
|
|
||||||
|
// Commands is a registry of available commands.
|
||||||
type Commands struct {
|
type Commands struct {
|
||||||
handlers map[string]CommandHandler
|
handlers map[string]CommandHandler
|
||||||
help map[string]string
|
help map[string]string
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCommands returns a new Commands registry.
|
||||||
func NewCommands() *Commands {
|
func NewCommands() *Commands {
|
||||||
return &Commands{
|
return &Commands{
|
||||||
handlers: map[string]CommandHandler{},
|
handlers: map[string]CommandHandler{},
|
||||||
@ -27,7 +36,8 @@ func NewCommands() *Commands {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register command. If help string is empty, it will be hidden from Help().
|
// Add will register a command. If help string is empty, it will be hidden from
|
||||||
|
// Help().
|
||||||
func (c Commands) Add(command string, help string, handler CommandHandler) {
|
func (c Commands) Add(command string, help string, handler CommandHandler) {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
@ -52,9 +62,9 @@ func (c Commands) Alias(command string, alias string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute command message, assumes IsCommand was checked.
|
// Run executes a command message.
|
||||||
func (c Commands) Run(channel *Channel, msg CommandMsg) error {
|
func (c Commands) Run(channel *Channel, msg CommandMsg) error {
|
||||||
if msg.from == nil {
|
if msg.From == nil {
|
||||||
return ErrNoOwner
|
return ErrNoOwner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
chat/set.go
35
chat/set.go
@ -6,35 +6,35 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrIdTaken error = errors.New("id already taken")
|
// The error returned when an added id already exists in the set.
|
||||||
var ErrItemMissing error = errors.New("item does not exist")
|
var ErrIdTaken = errors.New("id already taken")
|
||||||
|
|
||||||
// Unique identifier for an item
|
// The error returned when a requested item does not exist in the set.
|
||||||
|
var ErrItemMissing = errors.New("item does not exist")
|
||||||
|
|
||||||
|
// Id is a unique identifier for an item.
|
||||||
type Id string
|
type Id string
|
||||||
|
|
||||||
// A prefix for a unique identifier
|
// Item is an interface for items to store-able in the set
|
||||||
type IdPrefix Id
|
|
||||||
|
|
||||||
// An interface for items to store-able in the set
|
|
||||||
type Item interface {
|
type Item interface {
|
||||||
Id() Id
|
Id() Id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set with string lookup
|
// Set with string lookup.
|
||||||
// TODO: Add trie for efficient prefix lookup?
|
// TODO: Add trie for efficient prefix lookup?
|
||||||
type Set struct {
|
type Set struct {
|
||||||
lookup map[Id]Item
|
lookup map[Id]Item
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new set
|
// NewSet creates a new set.
|
||||||
func NewSet() *Set {
|
func NewSet() *Set {
|
||||||
return &Set{
|
return &Set{
|
||||||
lookup: map[Id]Item{},
|
lookup: map[Id]Item{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove all items and return the number removed
|
// Clear removes all items and returns the number removed.
|
||||||
func (s *Set) Clear() int {
|
func (s *Set) Clear() int {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
n := len(s.lookup)
|
n := len(s.lookup)
|
||||||
@ -43,12 +43,12 @@ func (s *Set) Clear() int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size of the set right now
|
// Len returns the size of the set right now.
|
||||||
func (s *Set) Len() int {
|
func (s *Set) Len() int {
|
||||||
return len(s.lookup)
|
return len(s.lookup)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if user belongs in this set
|
// In checks if an item exists in this set.
|
||||||
func (s *Set) In(item Item) bool {
|
func (s *Set) In(item Item) bool {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
_, ok := s.lookup[item.Id()]
|
_, ok := s.lookup[item.Id()]
|
||||||
@ -56,7 +56,7 @@ func (s *Set) In(item Item) bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user by name
|
// Get returns an item with the given Id.
|
||||||
func (s *Set) Get(id Id) (Item, error) {
|
func (s *Set) Get(id Id) (Item, error) {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
item, ok := s.lookup[id]
|
item, ok := s.lookup[id]
|
||||||
@ -69,7 +69,7 @@ func (s *Set) Get(id Id) (Item, error) {
|
|||||||
return item, nil
|
return item, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add user to set if user does not exist already
|
// Add item to this set if it does not exist already.
|
||||||
func (s *Set) Add(item Item) error {
|
func (s *Set) Add(item Item) error {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
@ -83,7 +83,7 @@ func (s *Set) Add(item Item) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove user from set
|
// Remove item from this set.
|
||||||
func (s *Set) Remove(item Item) error {
|
func (s *Set) Remove(item Item) error {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
@ -96,7 +96,8 @@ func (s *Set) Remove(item Item) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop over every item while holding a read lock and apply fn
|
// Each loops over every item while holding a read lock and applies fn to each
|
||||||
|
// element.
|
||||||
func (s *Set) Each(fn func(item Item)) {
|
func (s *Set) Each(fn func(item Item)) {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
for _, item := range s.lookup {
|
for _, item := range s.lookup {
|
||||||
@ -105,7 +106,7 @@ func (s *Set) Each(fn func(item Item)) {
|
|||||||
s.RUnlock()
|
s.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// List users by prefix, case insensitive
|
// ListPrefix returns a list of items with a prefix, case insensitive.
|
||||||
func (s *Set) ListPrefix(prefix string) []Item {
|
func (s *Set) ListPrefix(prefix string) []Item {
|
||||||
r := []Item{}
|
r := []Item{}
|
||||||
prefix = strings.ToLower(prefix)
|
prefix = strings.ToLower(prefix)
|
||||||
|
2
host.go
2
host.go
@ -122,7 +122,7 @@ func (h *Host) AutoCompleteFunction(line string, pos int, key rune) (newLine str
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// RefreshPrompt will update the terminal prompt with the latest user name.
|
// GetPrompt will render the terminal prompt string based on the user.
|
||||||
func GetPrompt(user *chat.User) string {
|
func GetPrompt(user *chat.User) string {
|
||||||
name := user.Name()
|
name := user.Name()
|
||||||
if user.Config.Theme != nil {
|
if user.Config.Theme != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user