Moved connection out of rootCmd to avoid crashing help

This commit is contained in:
Jonas Köritz 2019-08-29 10:17:57 +02:00
parent 98dd434503
commit 6d48cf11bf

View file

@ -16,6 +16,14 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func connectAndLogin(ip net.IP, port int, username, password string, verbose bool) *libipcamera.Camera {
camera := libipcamera.CreateCamera(ip, port, username, password)
camera.SetVerbose(verbose)
camera.Connect()
camera.Login()
return camera
}
func main() { func main() {
var username string var username string
var password string var password string
@ -37,15 +45,6 @@ func main() {
bufio.NewReader(os.Stdin).ReadBytes('\n') bufio.NewReader(os.Stdin).ReadBytes('\n')
}, },
PersistentPreRun: func(cmd *cobra.Command, args []string) {
camera = libipcamera.CreateCamera(net.ParseIP(args[0]), int(port), username, password)
camera.SetVerbose(verbose)
camera.Connect()
camera.Login()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
rootCmd.PersistentFlags().Int16VarP(&port, "port", "P", 6666, "Specify an alternative camera port to connect to") rootCmd.PersistentFlags().Int16VarP(&port, "port", "P", 6666, "Specify an alternative camera port to connect to")
@ -68,6 +67,12 @@ func main() {
fmt.Printf("%s\t%d\n", file.Path, file.Size) fmt.Printf("%s\t%d\n", file.Path, file.Size)
} }
}, },
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
var still = &cobra.Command{ var still = &cobra.Command{
@ -77,6 +82,12 @@ func main() {
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
camera.TakePicture() camera.TakePicture()
}, },
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
var record = &cobra.Command{ var record = &cobra.Command{
@ -86,6 +97,12 @@ func main() {
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
camera.StartRecording() camera.StartRecording()
}, },
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
var stop = &cobra.Command{ var stop = &cobra.Command{
@ -95,11 +112,17 @@ func main() {
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
camera.StopRecording() camera.StopRecording()
}, },
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
var version = &cobra.Command{ var firmware = &cobra.Command{
Use: "version [Cameras IP Address]", Use: "firmware [Cameras IP Address]",
Short: "Retrieve software version information from the camera", Short: "Retrieve firmware version information from the camera",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
firmware, err := camera.GetFirmwareInfo() firmware, err := camera.GetFirmwareInfo()
@ -109,6 +132,12 @@ func main() {
} }
log.Printf("Firmware Version: %s\n", firmware) log.Printf("Firmware Version: %s\n", firmware)
}, },
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
var cmd = &cobra.Command{ var cmd = &cobra.Command{
@ -133,6 +162,12 @@ func main() {
log.Printf("Waiting for Data, press ENTER to quit") log.Printf("Waiting for Data, press ENTER to quit")
bufio.NewReader(os.Stdin).ReadBytes('\n') bufio.NewReader(os.Stdin).ReadBytes('\n')
}, },
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
var fetch = &cobra.Command{ var fetch = &cobra.Command{
@ -151,6 +186,12 @@ func main() {
log.Printf("Downloading latest File: %s\n", url) log.Printf("Downloading latest File: %s\n", url)
downloadFile(filepath.Base(newestFile), url) downloadFile(filepath.Base(newestFile), url)
}, },
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
} }
rootCmd.AddCommand(ls) rootCmd.AddCommand(ls)
@ -159,7 +200,7 @@ func main() {
rootCmd.AddCommand(stop) rootCmd.AddCommand(stop)
rootCmd.AddCommand(fetch) rootCmd.AddCommand(fetch)
rootCmd.AddCommand(record) rootCmd.AddCommand(record)
rootCmd.AddCommand(version) rootCmd.AddCommand(firmware)
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
log.Println(err) log.Println(err)