fix: set config, set remote

This commit is contained in:
2018-11-19 22:36:21 +01:00
parent 54dd2191d6
commit dd7ea3156e
24 changed files with 1338 additions and 58 deletions

View File

@ -1,10 +1,13 @@
package cmd
import (
"git.cryptic.systems/fh-trier/go-flucky/cmd/temperature"
"git.cryptic.systems/fh-trier/go-flucky/cmd/config"
"git.cryptic.systems/fh-trier/go-flucky/cmd/remote"
"github.com/spf13/cobra"
)
var configDir string
var rootCmd = &cobra.Command{
Use: "flucky",
Short: "Read from sensors",
@ -14,7 +17,11 @@ var rootCmd = &cobra.Command{
func Execute(version string) {
rootCmd.Version = version
temperature.InitCmd(rootCmd)
rootCmd.PersistentFlags().StringVarP(&configDir, "config", "c", "/etc/flucky", "The base directory for all configuration files.")
config.InitCmd(rootCmd, configDir)
remote.InitCmd(rootCmd, configDir)
//temperature.InitCmd(rootCmd)
rootCmd.Execute()
}

19
cmd/config/config.go Normal file
View File

@ -0,0 +1,19 @@
package config
import (
"github.com/spf13/cobra"
)
var configDir string
var configCmd = &cobra.Command{
Use: "config",
Short: "config",
}
func InitCmd(cmd *cobra.Command, c string) {
configDir = c
cmd.AddCommand(configCmd)
}

26
cmd/config/create.go Normal file
View File

@ -0,0 +1,26 @@
package config
import (
"log"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
"github.com/spf13/cobra"
)
var force bool
var createConfigCmd = &cobra.Command{
Use: "create",
Short: "create",
Run: func(cmd *cobra.Command, args []string) {
if err := config.Create(configDir, force); err != nil {
log.Fatal(err)
}
},
}
func init() {
configCmd.AddCommand(createConfigCmd)
createConfigCmd.Flags().BoolVarP(&force, "force", "f", false, "Force the creation of a new configuration")
}

30
cmd/remote/add.go Normal file
View File

@ -0,0 +1,30 @@
package remote
import (
"log"
"git.cryptic.systems/fh-trier/go-flucky/pkg/remote"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
"github.com/spf13/cobra"
)
var addRemoteCmd = &cobra.Command{
Use: "add",
Short: "add",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
remoteObject := types.Remote{
Name: args[0],
Address: args[1],
}
if err := remote.Add(&remoteObject, configDir); err != nil {
log.Fatal(err)
}
},
}
func init() {
remoteCmd.AddCommand(addRemoteCmd)
}

24
cmd/remote/list.go Normal file
View File

@ -0,0 +1,24 @@
package remote
import (
"log"
"os"
"git.cryptic.systems/fh-trier/go-flucky/pkg/remote"
"github.com/spf13/cobra"
)
var listRemoteCmd = &cobra.Command{
Use: "list",
Short: "list",
Run: func(cmd *cobra.Command, args []string) {
if err := remote.List(os.Stdout, configDir); err != nil {
log.Fatal(err)
}
},
}
func init() {
remoteCmd.AddCommand(listRemoteCmd)
}

23
cmd/remote/register.go Normal file
View File

@ -0,0 +1,23 @@
package remote
import (
"log"
"git.cryptic.systems/fh-trier/go-flucky/pkg/remote"
"github.com/spf13/cobra"
)
var registerRemoteCmd = &cobra.Command{
Use: "register",
Short: "register on remote servers",
Run: func(cmd *cobra.Command, args []string) {
if err := remote.SendDevice(configDir); err != nil {
log.Fatal(err)
}
},
}
func init() {
remoteCmd.AddCommand(registerRemoteCmd)
}

18
cmd/remote/remote.go Normal file
View File

@ -0,0 +1,18 @@
package remote
import (
"github.com/spf13/cobra"
)
var configDir string
var remoteCmd = &cobra.Command{
Use: "remote",
Short: "remote",
}
func InitCmd(cmd *cobra.Command, c string) {
configDir = c
cmd.AddCommand(remoteCmd)
}

33
cmd/remote/remove.go Normal file
View File

@ -0,0 +1,33 @@
package remote
import (
"log"
"git.cryptic.systems/fh-trier/go-flucky/pkg/remote"
"github.com/spf13/cobra"
)
var all bool
var removeRemoteCmd = &cobra.Command{
Use: "remove",
Short: "remove",
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
if all {
if err := remote.RemoveAll(configDir); err != nil {
log.Fatal(err)
}
} else {
if err := remote.Remove(args[0], configDir); err != nil {
log.Fatal(err)
}
}
},
}
func init() {
remoteCmd.AddCommand(removeRemoteCmd)
removeRemoteCmd.Flags().BoolVarP(&all, "all", "a", false, "Select all remote connections")
}

25
cmd/temperature/get.go Normal file
View File

@ -0,0 +1,25 @@
package remote
import (
"log"
"os"
"git.cryptic.systems/fh-trier/go-flucky/pkg/temperature"
"github.com/spf13/cobra"
)
var getTemperatureCmd = &cobra.Command{
Use: "get",
Short: "get temperature from sensor",
// Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
if err := temperature.Get(os.Stdout); err != nil {
log.Fatal(err)
}
},
}
func init() {
temperatureCmd.AddCommand(getTemperatureCmd)
}

View File

@ -1,24 +1,16 @@
package temperature
package remote
import (
"log"
"os"
"git.cryptic.systems/fh-trier/go-flucky/pkg/temperature"
"github.com/spf13/cobra"
)
var temperatureCmd = &cobra.Command{
Use: "temperature",
Short: "temperature",
Run: func(cmd *cobra.Command, args []string) {
if err := temperature.Print(os.Stdout); err != nil {
log.Fatal(err)
}
},
Short: "Read temperature from sensor",
}
func InitCmd(cmd *cobra.Command) {
// Execute a
func Init(cmd *cobra.Command) {
cmd.AddCommand(temperatureCmd)