1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-22 19:51:58 +01:00
sip/cmd/config.go
jolheiser 67326a7948
Start work on flags
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2020-09-18 14:28:02 -05:00

89 lines
1.6 KiB
Go

package cmd
import (
"gitea.com/jolheiser/sip/config"
"gitea.com/jolheiser/sip/prompt"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var (
originFlag = &cli.StringFlag{
Name: "origin",
Usage: "Default origin name",
Value: "origin",
}
upstreamFlag = &cli.StringFlag{
Name: "upstream",
Usage: "Default upstream name",
Value: "upstream",
}
)
var Config = cli.Command{
Name: "config",
Aliases: []string{"cfg"},
Usage: "Modify Sip config",
Action: doConfig,
Flags: []cli.Flag{
originFlag,
upstreamFlag,
},
Subcommands: []*cli.Command{
{
Name: "origin",
Usage: "Specify default origin name",
Flags: []cli.Flag{
originFlag,
},
Action: doConfigOrigin,
},
{
Name: "upstream",
Usage: "Specify default upstream name",
Flags: []cli.Flag{
upstreamFlag,
},
Action: doConfigUpstream,
},
},
}
func doConfig(ctx *cli.Context) error {
if err := doConfigOrigin(ctx); err != nil {
return err
}
return doConfigUpstream(ctx)
}
func doConfigOrigin(ctx *cli.Context) error {
origin, err := prompt.Input(ctx, originFlag, ctx.String("origin"), true)
if err != nil {
return err
}
config.Origin = origin
if err := config.Save(); err != nil {
return err
}
beaver.Info("Default origin saved!")
return nil
}
func doConfigUpstream(ctx *cli.Context) error {
upstream, err := prompt.Input(ctx, upstreamFlag, ctx.String("upstream"), true)
if err != nil {
return err
}
config.Upstream = upstream
if err := config.Save(); err != nil {
return err
}
beaver.Info("Default upstream saved!")
return nil
}