missed files
This commit is contained in:
parent
7014551501
commit
4b72c101da
19
cmd/http.go
19
cmd/http.go
@ -15,24 +15,21 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"net/http"
|
||||||
|
|
||||||
|
"git.icod.de/dalu/wifitoggle/server"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// httpCmd represents the http command
|
// httpCmd represents the http command
|
||||||
var httpCmd = &cobra.Command{
|
var httpCmd = &cobra.Command{
|
||||||
Use: "http",
|
Use: "http",
|
||||||
Short: "A brief description of your command",
|
Short: "starts the http server",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `starts the http server`,
|
||||||
and usage of using your command. For example:
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
h := new(server.Handler)
|
||||||
Cobra is a CLI library for Go that empowers applications.
|
http.Handle("/", h)
|
||||||
This application is a tool to generate the needed files
|
return http.ListenAndServe(":59595", nil)
|
||||||
to quickly create a Cobra application.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
// TODO: Work your own magic here
|
|
||||||
fmt.Println("http called")
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
cmd/off.go
22
cmd/off.go
@ -15,7 +15,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -23,16 +23,16 @@ import (
|
|||||||
// offCmd represents the off command
|
// offCmd represents the off command
|
||||||
var offCmd = &cobra.Command{
|
var offCmd = &cobra.Command{
|
||||||
Use: "off",
|
Use: "off",
|
||||||
Short: "A brief description of your command",
|
Short: "turns off wifi hotspot",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `turns off wifi hotspot`,
|
||||||
and usage of using your command. For example:
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if e := exec.Command("nmcli", "connection", "down", "Hotspot").Run(); e != nil {
|
||||||
Cobra is a CLI library for Go that empowers applications.
|
return e
|
||||||
This application is a tool to generate the needed files
|
}
|
||||||
to quickly create a Cobra application.`,
|
if e := exec.Command("nmcli", "radio", "wifi", "off").Run(); e != nil {
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
return e
|
||||||
// TODO: Work your own magic here
|
}
|
||||||
fmt.Println("off called")
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
cmd/on.go
22
cmd/on.go
@ -15,7 +15,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -23,16 +23,16 @@ import (
|
|||||||
// onCmd represents the on command
|
// onCmd represents the on command
|
||||||
var onCmd = &cobra.Command{
|
var onCmd = &cobra.Command{
|
||||||
Use: "on",
|
Use: "on",
|
||||||
Short: "A brief description of your command",
|
Short: "turns on wifi hotspot",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `turns on wifi hotspot`,
|
||||||
and usage of using your command. For example:
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if e := exec.Command("nmcli", "radio", "wifi", "on").Run(); e != nil {
|
||||||
Cobra is a CLI library for Go that empowers applications.
|
return e
|
||||||
This application is a tool to generate the needed files
|
}
|
||||||
to quickly create a Cobra application.`,
|
if e := exec.Command("nmcli", "connection", "up", "Hotspot", "ifname", "wlp2s0").Run(); e != nil {
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
return e
|
||||||
// TODO: Work your own magic here
|
}
|
||||||
fmt.Println("on called")
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1 +1,56 @@
|
|||||||
package server
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user