Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 1baf4c8

Browse files
committed
feat: Add apply command to dhclientctl (#12)
1 parent d854127 commit 1baf4c8

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

cmd/dhclientctl/cmd/apply.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
constants "github.com/pojntfx/godhcpd/cmd"
7+
godhcpd "github.com/pojntfx/godhcpd/pkg/proto/generated"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
"gitlab.com/bloom42/libs/rz-go"
11+
"gitlab.com/bloom42/libs/rz-go/log"
12+
"google.golang.org/grpc"
13+
)
14+
15+
var applyCmd = &cobra.Command{
16+
Use: "apply",
17+
Aliases: []string{"a"},
18+
Short: "Apply a dhcp client",
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
if !(viper.GetString(configFileKey) == configFileDefault) {
21+
viper.SetConfigFile(viper.GetString(configFileKey))
22+
23+
if err := viper.ReadInConfig(); err != nil {
24+
return err
25+
}
26+
}
27+
28+
conn, err := grpc.Dial(viper.GetString(serverHostPortKey), grpc.WithInsecure(), grpc.WithBlock())
29+
if err != nil {
30+
return err
31+
}
32+
defer conn.Close()
33+
34+
client := godhcpd.NewDHClientManagerClient(conn)
35+
36+
ctx, cancel := context.WithCancel(context.Background())
37+
defer cancel()
38+
39+
response, err := client.Create(ctx, &godhcpd.DHClient{
40+
Device: viper.GetString(deviceKey),
41+
})
42+
if err != nil {
43+
return err
44+
}
45+
46+
fmt.Printf("dhcp client \"%s\" created\n", response.GetId())
47+
48+
return nil
49+
},
50+
}
51+
52+
func init() {
53+
var (
54+
serverHostPortFlag string
55+
configFileFlag string
56+
deviceFlag string
57+
)
58+
59+
applyCmd.PersistentFlags().StringVarP(&serverHostPortFlag, serverHostPortKey, "s", constants.DHClientDHostPortDefault, "Host:port of the dhclientd server to use.")
60+
applyCmd.PersistentFlags().StringVarP(&configFileFlag, configFileKey, "f", configFileDefault, "Configuration file to use.")
61+
applyCmd.PersistentFlags().StringVarP(&deviceFlag, deviceKey, "d", "edge1", "Device to bind to.")
62+
63+
if err := viper.BindPFlags(applyCmd.PersistentFlags()); err != nil {
64+
log.Fatal(constants.CouldNotBindFlagsErrorMessage, rz.Err(err))
65+
}
66+
67+
viper.AutomaticEnv()
68+
69+
rootCmd.AddCommand(applyCmd)
70+
}

cmd/dhcpdctl/cmd/apply.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
var applyCmd = &cobra.Command{
1717
Use: "apply",
1818
Aliases: []string{"a"},
19-
Short: "Apply an dhcp server",
19+
Short: "Apply a dhcp server",
2020
RunE: func(cmd *cobra.Command, args []string) error {
2121
var subnets []*godhcpd.Subnet
2222

@@ -69,7 +69,7 @@ func init() {
6969
subnetsFlag string
7070
)
7171

72-
applyCmd.PersistentFlags().StringVarP(&serverHostPortFlag, serverHostPortKey, "s", constants.DHCPDDHostPortDefault, "Host:port of the godhcpd server to use.")
72+
applyCmd.PersistentFlags().StringVarP(&serverHostPortFlag, serverHostPortKey, "s", constants.DHCPDDHostPortDefault, "Host:port of the dhcpdd server to use.")
7373
applyCmd.PersistentFlags().StringVarP(&configFileFlag, configFileKey, "f", configFileDefault, "Configuration file to use.")
7474
applyCmd.PersistentFlags().StringVarP(&deviceFlag, deviceKey, "d", "edge0", "Device to bind to.")
7575
applyCmd.PersistentFlags().StringVarP(&subnetsFlag, subnetsKey, "n", `[

0 commit comments

Comments
 (0)