missed files

This commit is contained in:
Darko Luketic 2017-05-19 19:31:18 +02:00
parent 7014551501
commit 4b72c101da
4 changed files with 85 additions and 33 deletions

View File

@ -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)
},
}

View File

@ -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
},
}

View File

@ -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
},
}

View File

@ -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
}
}