diff --git a/cmd/http.go b/cmd/http.go index 5c2a566..e861bd1 100644 --- a/cmd/http.go +++ b/cmd/http.go @@ -15,24 +15,21 @@ package cmd import ( - "fmt" + "net/http" + "git.icod.de/dalu/wifitoggle/server" "github.com/spf13/cobra" ) // httpCmd represents the http command var httpCmd = &cobra.Command{ Use: "http", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - // TODO: Work your own magic here - fmt.Println("http called") + Short: "starts the http server", + Long: `starts the http server`, + RunE: func(cmd *cobra.Command, args []string) error { + h := new(server.Handler) + http.Handle("/", h) + return http.ListenAndServe(":59595", nil) }, } diff --git a/cmd/off.go b/cmd/off.go index 8808b17..772d928 100644 --- a/cmd/off.go +++ b/cmd/off.go @@ -15,7 +15,7 @@ package cmd import ( - "fmt" + "os/exec" "github.com/spf13/cobra" ) @@ -23,16 +23,16 @@ import ( // offCmd represents the off command var offCmd = &cobra.Command{ Use: "off", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - // TODO: Work your own magic here - fmt.Println("off called") + Short: "turns off wifi hotspot", + Long: `turns off wifi hotspot`, + RunE: func(cmd *cobra.Command, args []string) error { + if e := exec.Command("nmcli", "connection", "down", "Hotspot").Run(); e != nil { + return e + } + if e := exec.Command("nmcli", "radio", "wifi", "off").Run(); e != nil { + return e + } + return nil }, } diff --git a/cmd/on.go b/cmd/on.go index bde9050..5a64381 100644 --- a/cmd/on.go +++ b/cmd/on.go @@ -15,7 +15,7 @@ package cmd import ( - "fmt" + "os/exec" "github.com/spf13/cobra" ) @@ -23,16 +23,16 @@ import ( // onCmd represents the on command var onCmd = &cobra.Command{ Use: "on", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - // TODO: Work your own magic here - fmt.Println("on called") + Short: "turns on wifi hotspot", + Long: `turns on wifi hotspot`, + RunE: func(cmd *cobra.Command, args []string) error { + if e := exec.Command("nmcli", "radio", "wifi", "on").Run(); e != nil { + return e + } + if e := exec.Command("nmcli", "connection", "up", "Hotspot", "ifname", "wlp2s0").Run(); e != nil { + return e + } + return nil }, } diff --git a/server/handler.go b/server/handler.go index abb4e43..e619304 100644 --- a/server/handler.go +++ b/server/handler.go @@ -1 +1,56 @@ package server + +import ( + "net/http" + "os/exec" +) + +type Handler struct{} + +func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + toggle := r.URL.Query().Get("toggle") + switch toggle { + case "on": + if !h.WifiEnabled(w, r) { + if e := exec.Command("nmcli", "radio", "wifi", "on").Run(); e != nil { + http.Error(w, e.Error(), http.StatusInternalServerError) + return + } + if e := exec.Command("nmcli", "connection", "up", "Hotspot", "ifname", "wlp2s0").Run(); e != nil { + http.Error(w, e.Error(), http.StatusInternalServerError) + return + } + } + + case "off": + if h.WifiEnabled(w, r) { + if e := exec.Command("nmcli", "connection", "down", "Hotspot").Run(); e != nil { + http.Error(w, e.Error(), http.StatusInternalServerError) + return + } + if e := exec.Command("nmcli", "radio", "wifi", "off").Run(); e != nil { + http.Error(w, e.Error(), http.StatusInternalServerError) + return + } + } + } + + default: + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + } +} + +func (h *Handler) WifiEnabled(w http.ResponseWriter, r *http.Request) bool { + status, e := exec.Command("nmcli", "radio", "wifi").Output() + if e != nil { + http.Error(w, e.Error(), http.StatusInternalServerError) + panic(e) + } + if string(status) == "enabled" { + return true + } else { + return false + } +}