service: make name arg optional for network commands
Makes commands less verbose.
This commit is contained in:
parent
57715d8ce2
commit
3f91cfb8c3
@ -236,19 +236,25 @@ abbreviated form, for instance *network* can be abbreviated as *net* or just
|
|||||||
The flag can be specified multiple times to send multiple IRC messages.
|
The flag can be specified multiple times to send multiple IRC messages.
|
||||||
To clear all commands, set it to the empty string.
|
To clear all commands, set it to the empty string.
|
||||||
|
|
||||||
*network update* <name> [options...]
|
*network update* [name] [options...]
|
||||||
Update an existing network. The options are the same as the
|
Update an existing network. The options are the same as the
|
||||||
_network create_ command.
|
_network create_ command.
|
||||||
|
|
||||||
When this command is executed, soju will disconnect and re-connect to the
|
When this command is executed, soju will disconnect and re-connect to the
|
||||||
network.
|
network.
|
||||||
|
|
||||||
*network delete* <name>
|
If _name_ is not specified, the current network is updated.
|
||||||
|
|
||||||
|
*network delete* [name]
|
||||||
Disconnect and delete a network.
|
Disconnect and delete a network.
|
||||||
|
|
||||||
*network quote* <name> <command>
|
If _name_ is not specified, the current network is deleted.
|
||||||
|
|
||||||
|
*network quote* [name] <command>
|
||||||
Send a raw IRC line as-is to a network.
|
Send a raw IRC line as-is to a network.
|
||||||
|
|
||||||
|
If _name_ is not specified, the command is sent to the current network.
|
||||||
|
|
||||||
*network status*
|
*network status*
|
||||||
Show a list of saved networks and their current status.
|
Show a list of saved networks and their current status.
|
||||||
|
|
||||||
|
65
service.go
65
service.go
@ -208,17 +208,17 @@ func init() {
|
|||||||
handle: handleServiceNetworkStatus,
|
handle: handleServiceNetworkStatus,
|
||||||
},
|
},
|
||||||
"update": {
|
"update": {
|
||||||
usage: "<name> [-addr addr] [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-enabled enabled] [-connect-command command]...",
|
usage: "[name] [-addr addr] [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-enabled enabled] [-connect-command command]...",
|
||||||
desc: "update a network",
|
desc: "update a network",
|
||||||
handle: handleServiceNetworkUpdate,
|
handle: handleServiceNetworkUpdate,
|
||||||
},
|
},
|
||||||
"delete": {
|
"delete": {
|
||||||
usage: "<name>",
|
usage: "[name]",
|
||||||
desc: "delete a network",
|
desc: "delete a network",
|
||||||
handle: handleServiceNetworkDelete,
|
handle: handleServiceNetworkDelete,
|
||||||
},
|
},
|
||||||
"quote": {
|
"quote": {
|
||||||
usage: "<name> <command>",
|
usage: "[name] <command>",
|
||||||
desc: "send a raw line to a network",
|
desc: "send a raw line to a network",
|
||||||
handle: handleServiceNetworkQuote,
|
handle: handleServiceNetworkQuote,
|
||||||
},
|
},
|
||||||
@ -411,6 +411,22 @@ func (f boolPtrFlag) Set(s string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getNetworkFromArg(dc *downstreamConn, params []string) (*network, []string, error) {
|
||||||
|
name, params := popArg(params)
|
||||||
|
if name == "" {
|
||||||
|
if dc.network == nil {
|
||||||
|
return nil, params, fmt.Errorf("no network selected, a name argument is required")
|
||||||
|
}
|
||||||
|
return dc.network, params, nil
|
||||||
|
} else {
|
||||||
|
net := dc.user.getNetwork(name)
|
||||||
|
if net == nil {
|
||||||
|
return nil, params, fmt.Errorf("unknown network %q", name)
|
||||||
|
}
|
||||||
|
return net, params, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type networkFlagSet struct {
|
type networkFlagSet struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
Addr, Name, Nick, Username, Pass, Realname *string
|
Addr, Name, Nick, Username, Pass, Realname *string
|
||||||
@ -550,18 +566,14 @@ func handleServiceNetworkStatus(ctx context.Context, dc *downstreamConn, params
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleServiceNetworkUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
|
func handleServiceNetworkUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
|
||||||
if len(params) < 1 {
|
net, params, err := getNetworkFromArg(dc, params)
|
||||||
return fmt.Errorf("expected at least one argument")
|
if err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
fs := newNetworkFlagSet()
|
|
||||||
if err := fs.Parse(params[1:]); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
net := dc.user.getNetwork(params[0])
|
fs := newNetworkFlagSet()
|
||||||
if net == nil {
|
if err := fs.Parse(params); err != nil {
|
||||||
return fmt.Errorf("unknown network %q", params[0])
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
record := net.Network // copy network record because we'll mutate it
|
record := net.Network // copy network record because we'll mutate it
|
||||||
@ -579,13 +591,9 @@ func handleServiceNetworkUpdate(ctx context.Context, dc *downstreamConn, params
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleServiceNetworkDelete(ctx context.Context, dc *downstreamConn, params []string) error {
|
func handleServiceNetworkDelete(ctx context.Context, dc *downstreamConn, params []string) error {
|
||||||
if len(params) != 1 {
|
net, params, err := getNetworkFromArg(dc, params)
|
||||||
return fmt.Errorf("expected exactly one argument")
|
if err != nil {
|
||||||
}
|
return err
|
||||||
|
|
||||||
net := dc.user.getNetwork(params[0])
|
|
||||||
if net == nil {
|
|
||||||
return fmt.Errorf("unknown network %q", params[0])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dc.user.deleteNetwork(ctx, net.ID); err != nil {
|
if err := dc.user.deleteNetwork(ctx, net.ID); err != nil {
|
||||||
@ -597,23 +605,26 @@ func handleServiceNetworkDelete(ctx context.Context, dc *downstreamConn, params
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleServiceNetworkQuote(ctx context.Context, dc *downstreamConn, params []string) error {
|
func handleServiceNetworkQuote(ctx context.Context, dc *downstreamConn, params []string) error {
|
||||||
if len(params) != 2 {
|
if len(params) != 1 && len(params) != 2 {
|
||||||
return fmt.Errorf("expected exactly two arguments")
|
return fmt.Errorf("expected one or two arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
net := dc.user.getNetwork(params[0])
|
raw := params[len(params)-1]
|
||||||
if net == nil {
|
params = params[:len(params)-1]
|
||||||
return fmt.Errorf("unknown network %q", params[0])
|
|
||||||
|
net, params, err := getNetworkFromArg(dc, params)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
uc := net.conn
|
uc := net.conn
|
||||||
if uc == nil {
|
if uc == nil {
|
||||||
return fmt.Errorf("network %q is not currently connected", params[0])
|
return fmt.Errorf("network %q is not currently connected", net.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := irc.ParseMessage(params[1])
|
m, err := irc.ParseMessage(raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to parse command %q: %v", params[1], err)
|
return fmt.Errorf("failed to parse command %q: %v", raw, err)
|
||||||
}
|
}
|
||||||
uc.SendMessage(ctx, m)
|
uc.SendMessage(ctx, m)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user