Skip to content

Commit

Permalink
cleanup, add error handling, add viper configuration, enable travis
Browse files Browse the repository at this point in the history
  • Loading branch information
choppedpork committed Apr 16, 2019
1 parent c9192e4 commit 026b327
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 134 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: go

go:
- 1.12.x
25 changes: 11 additions & 14 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,31 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/choppedpork/ohctl/openhab"
"github.com/spf13/cobra"
)

// cmdCmd represents the cmd command
var cmdCmd = &cobra.Command{
Use: "cmd",
Short: "Sends a command to an item.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
oh := openhab.NewClient()
oh.Cmd(args[0], strings.ToUpper(args[1]))

oh := openhab.NewClient(Config.Host, Config.Port)
err := oh.Cmd(args[0], strings.ToUpper(args[1]))

if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}

},
}

func init() {
RootCmd.AddCommand(cmdCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// cmdCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// cmdCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
11 changes: 0 additions & 11 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,4 @@ To configure your bash shell to load completions for each session add to your ba

func init() {
RootCmd.AddCommand(completionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// completionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// completionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
39 changes: 39 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var Config config

type config struct {
Host string `mapstructure:"host"`
Port uint16 `mapstructure:"port"`
}

func init() {
cobra.OnInitialize(initConfig)
}

func initConfig() {
viper.SetConfigName(".ohctl")
viper.AddConfigPath("$HOME")
viper.SetEnvPrefix("ohctl")
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
fmt.Println("error reading config: ", err)
os.Exit(1)
}

err := viper.Unmarshal(&Config)

if err != nil {
fmt.Println("error loading config: ", err)
os.Exit(1)
}
}
18 changes: 2 additions & 16 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,11 @@ import (
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get",
Short: "Gets items",
Short: "Gets stuff",
ValidArgs: []string{"item", "items"},
// Run: func(cmd *cobra.Command, args []string) {
// // TODO: Work your own magic here
// fmt.Println("get called")
// },
// no Run here
}

func init() {
RootCmd.AddCommand(getCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
22 changes: 8 additions & 14 deletions cmd/getitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ import (
"github.com/spf13/cobra"
)

// getitemCmd represents the getitem command
// getitemCmd represents the "get item" command
var getitemCmd = &cobra.Command{
Use: "item",
Short: "Get item",
Long: `Gets specific item in openhab`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {

oh := openhab.NewClient()
item := oh.GetItem(args[0])
oh := openhab.NewClient(Config.Host, Config.Port)
item, err := oh.GetItem(args[0])

if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}

w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
Expand All @@ -47,15 +52,4 @@ var getitemCmd = &cobra.Command{

func init() {
getCmd.AddCommand(getitemCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getitemCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// getitemCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
21 changes: 7 additions & 14 deletions cmd/getitems.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ var getitemsCmd = &cobra.Command{
Short: "List all items",
Long: `List all items in openhab`,
Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("get items called")

oh := openhab.NewClient()
items := oh.GetItems()
oh := openhab.NewClient(Config.Host, Config.Port)
items, err := oh.GetItems()

if err != nil {
fmt.Println("error retrieving items:", err)
os.Exit(1)
}

if quiet {
for _, item := range items {
Expand All @@ -61,15 +65,4 @@ var getitemsCmd = &cobra.Command{
func init() {
getitemsCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "quiet mode - prints item names only")
getCmd.AddCommand(getitemsCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getitemsCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
getitemsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
32 changes: 0 additions & 32 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "ohctl",
Expand All @@ -39,32 +36,3 @@ func Execute() {
os.Exit(-1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ohctl.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
}

viper.SetConfigName(".ohctl") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
14 changes: 1 addition & 13 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,10 @@ 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("set called")
fmt.Println("not implemented")
},
}

func init() {
RootCmd.AddCommand(setCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// setCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// setCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package main

import "github.com/choppedpork/ohctl/cmd"
import (
"github.com/choppedpork/ohctl/cmd"
)

func main() {
cmd.Execute()
Expand Down
Loading

0 comments on commit 026b327

Please sign in to comment.