github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/seautil/cmd/route.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "github.com/alibaba/sealer/utils" 19 20 "github.com/spf13/cobra" 21 ) 22 23 type RouteFlag struct { 24 host string 25 gatewayIP string 26 } 27 28 var routeFlag *RouteFlag 29 30 func NewRouteCmd() *cobra.Command { 31 var routeCmd = &cobra.Command{ 32 Use: "route", 33 Short: "A brief description of your command", 34 } 35 routeFlag = &RouteFlag{} 36 routeCmd.AddCommand(RouteAddCmd()) 37 routeCmd.AddCommand(RouteDelCmd()) 38 routeCmd.AddCommand(RouteCheckCmd()) 39 return routeCmd 40 } 41 42 func RouteCheckCmd() *cobra.Command { 43 var checkCmd = &cobra.Command{ 44 Use: "check", 45 Short: "A brief description of your command", 46 Long: `seautil route check --host 192.168.56.3`, 47 RunE: func(cmd *cobra.Command, args []string) error { 48 return utils.CheckIsDefaultRoute(routeFlag.host) 49 }, 50 } 51 checkCmd.Flags().StringVar(&routeFlag.host, "host", "", "check host ip address is default iFace") 52 return checkCmd 53 } 54 55 func RouteAddCmd() *cobra.Command { 56 var addCmd = &cobra.Command{ 57 Use: "add", 58 Short: "A brief description of your command", 59 Long: `seautil route add --host 192.168.0.2 --gateway 10.0.0.2`, 60 RunE: func(cmd *cobra.Command, args []string) error { 61 r := utils.NewRouter(routeFlag.host, routeFlag.gatewayIP) 62 return r.SetRoute() 63 }, 64 } 65 addCmd.Flags().StringVar(&routeFlag.host, "host", "", "route host ,ex ip route add host via gateway") 66 addCmd.Flags().StringVar(&routeFlag.gatewayIP, "gateway", "", "route gateway ,ex ip route add host via gateway") 67 return addCmd 68 } 69 70 func RouteDelCmd() *cobra.Command { 71 var delCmd = &cobra.Command{ 72 Use: "del", 73 Short: "delete router", 74 Long: `seautil route del --host 192.168.0.2 --gateway 10.0.0.2`, 75 RunE: func(cmd *cobra.Command, args []string) error { 76 r := utils.NewRouter(routeFlag.host, routeFlag.gatewayIP) 77 return r.DelRoute() 78 }, 79 } 80 delCmd.Flags().StringVar(&routeFlag.host, "host", "", "route host ,ex ip route del host via gateway") 81 delCmd.Flags().StringVar(&routeFlag.gatewayIP, "gateway", "", "route gateway ,ex ip route del host via gateway") 82 return delCmd 83 } 84 85 func init() { 86 rootCmd.AddCommand(NewRouteCmd()) 87 }