Files
prx/internal/cli/lifecycle.go
T

82 lines
1.8 KiB
Go

// SPDX-License-Identifier: MIT
package cli
import (
"fmt"
"github.com/spf13/cobra"
"prx/internal/proto"
"prx/internal/state"
)
func (a *app) newOnCmd() *cobra.Command {
var skipEngine, dryRun bool
cmd := &cobra.Command{
Use: "on",
Short: "Enable proxy routing with inbound protection",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cfg, err := a.loadConfig()
if err != nil {
return err
}
err = a.dispatch(cmd.Context(), cfg, proto.ActionApply, func(p *proto.Plan) {
p.SkipEngine = skipEngine
p.DryRun = dryRun
})
if err != nil {
return err
}
if dryRun {
fmt.Fprintln(a.out, "dry-run complete; nothing changed")
}
return nil
},
}
cmd.Flags().BoolVar(&skipEngine, "skip-engine", false, "apply routing rules without touching the proxy engine")
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "validate the plan without changing anything")
return cmd
}
func (a *app) newOffCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "off",
Short: "Disable proxy routing and remove prx-managed rules",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cfg, err := a.loadConfig()
if err != nil {
return err
}
return a.dispatch(cmd.Context(), cfg, proto.ActionRemove, nil)
},
}
return cmd
}
func (a *app) newToggleCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "toggle",
Short: "Toggle proxy routing based on current state",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cfg, err := a.loadConfig()
if err != nil {
return err
}
st, err := state.Load(cfg.State.Path)
if err != nil {
return err
}
action := proto.ActionApply
if st.Enabled {
action = proto.ActionRemove
}
return a.dispatch(cmd.Context(), cfg, action, nil)
},
}
return cmd
}