Files
openlan/cmd/api/v5/output.go
Daniel Ding 80ea5c26b4
Some checks failed
Coverage CI / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Ubuntu CI / build (push) Has been cancelled
fea: fallback for output.
2025-08-29 10:43:14 +08:00

131 lines
3.1 KiB
Go

package v5
import (
"github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/schema"
"github.com/urfave/cli/v2"
)
type Output struct {
Cmd
}
func (o Output) Url(prefix, name string) string {
return prefix + "/api/network/" + name + "/output"
}
func (o Output) Add(c *cli.Context) error {
network := c.String("name")
if len(network) == 0 {
return libol.NewErr("invalid network")
}
output := &schema.Output{
Network: network,
Remote: c.String("remote"),
Segment: c.Int("segment"),
Protocol: c.String("protocol"),
DstPort: c.Int("dstport"),
Secret: c.String("secret"),
Crypt: c.String("crypt"),
Fallback: c.String("fallback"),
}
url := o.Url(c.String("url"), network)
clt := o.NewHttp(c.String("token"))
if err := clt.PostJSON(url, output, nil); err != nil {
return err
}
return nil
}
func (o Output) Remove(c *cli.Context) error {
network := c.String("name")
if len(network) == 0 {
return libol.NewErr("invalid network")
}
output := &schema.Output{
Network: network,
Device: c.String("device"),
}
url := o.Url(c.String("url"), network)
clt := o.NewHttp(c.String("token"))
if err := clt.DeleteJSON(url, output, nil); err != nil {
return err
}
return nil
}
func (o Output) Save(c *cli.Context) error {
network := c.String("name")
url := o.Url(c.String("url"), network)
clt := o.NewHttp(c.String("token"))
if err := clt.PutJSON(url, nil, nil); err != nil {
return err
}
return nil
}
func (o Output) Tmpl() string {
return `# total {{ len . }}
{{ps -24 "network"}} {{ps -15 "protocol"}} {{ps -15 "Remote"}} {{ps -15 "segment"}} {{ps -15 "device"}}
{{- range . }}
{{ps -24 .Network}} {{ps -15 .Protocol}} {{ps -15 .Remote}} {{ if .Segment }}{{pi -15 .Segment }}{{ else }}{{ps -15 .Secret}}{{ end }} {{ps -15 .Device}} {{.Crypt}}
{{- end }}
`
}
func (o Output) List(c *cli.Context) error {
url := o.Url(c.String("url"), c.String("name"))
clt := o.NewHttp(c.String("token"))
var items []schema.Output
if err := clt.GetJSON(url, &items); err != nil {
return err
}
return o.Out(items, c.String("format"), o.Tmpl())
}
func (o Output) Commands() *cli.Command {
return &cli.Command{
Name: "output",
Usage: "Output links",
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "Add an output for the network",
Flags: []cli.Flag{
&cli.StringFlag{Name: "remote", Required: true},
&cli.StringFlag{Name: "fallback"},
&cli.IntFlag{Name: "segment"},
&cli.StringFlag{Name: "protocol"},
&cli.StringFlag{Name: "dstport"},
&cli.StringFlag{Name: "secret"},
&cli.StringFlag{Name: "crypt"},
},
Action: o.Add,
},
{
Name: "remove",
Usage: "Remove an output from the network",
Aliases: []string{"rm"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "device", Required: true},
},
Action: o.Remove,
},
{
Name: "list",
Usage: "Display all outputs of the network",
Aliases: []string{"ls"},
Action: o.List,
},
{
Name: "save",
Usage: "Save all outputs",
Aliases: []string{"sa"},
Action: o.Save,
},
},
}
}