-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reorganizing into packages. Much nicer organization now.
- Loading branch information
Andrew Suderman
authored
Jan 20, 2020
1 parent
be7d703
commit 0dfc8de
Showing
17 changed files
with
293 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/klog" | ||
|
||
"github.com/sudermanjr/led-controller/pkg/homekit" | ||
"github.com/sudermanjr/led-controller/pkg/neopixel" | ||
) | ||
|
||
var homekitPin string | ||
|
||
func init() { | ||
rootCmd.AddCommand(homekitCmd) | ||
|
||
homekitCmd.Flags().StringVar(&homekitPin, "homekit-pin", "29847290", "The pin that homekit will use to authenticate with this device.") | ||
} | ||
|
||
var homekitCmd = &cobra.Command{ | ||
Use: "homekit", | ||
Short: "Run the lights as a homekit accessory.", | ||
Long: `Run the lights as a homekit accessory.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
led, err := neopixel.NewLEDArray(minBrightness, maxBrightness, ledCount, fadeDuration) | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
defer led.WS.Fini() | ||
|
||
homekit.Start(homekitPin, led) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cmd | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"k8s.io/klog" | ||
) | ||
|
||
var ( | ||
version = "development" | ||
commit = "n/a" | ||
ledCount int | ||
maxBrightness int | ||
minBrightness int | ||
fadeDuration int | ||
colorName string | ||
) | ||
|
||
func init() { | ||
// Flags | ||
rootCmd.PersistentFlags().IntVarP(&ledCount, "led-count", "l", 12, "The number of LEDs in the array.") | ||
rootCmd.PersistentFlags().IntVar(&maxBrightness, "max-brightness", 200, "The maximum brightness that will work within the 0-250 range.") | ||
rootCmd.PersistentFlags().IntVar(&minBrightness, "min-brightness", 25, "The minimum brightness that will work within the 0-250 range.") | ||
rootCmd.PersistentFlags().IntVarP(&fadeDuration, "fade-duration", "f", 100, "The duration of fade-ins and fade-outs in ms.") | ||
|
||
//Commands | ||
rootCmd.AddCommand(versionCmd) | ||
|
||
klog.InitFlags(nil) | ||
flag.Parse() | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
|
||
environmentVariables := map[string]string{ | ||
"LED_COUNT": "led-count", | ||
"MAX_BRIGHTNESS": "max-brightness", | ||
"MIN_BRIGHTNESS": "min-brightness", | ||
"FADE_DURTION": "fade-duration", | ||
} | ||
|
||
for env, flag := range environmentVariables { | ||
flag := rootCmd.PersistentFlags().Lookup(flag) | ||
flag.Usage = fmt.Sprintf("%v [%v]", flag.Usage, env) | ||
if value := os.Getenv(env); value != "" { | ||
err := flag.Value.Set(value) | ||
if err != nil { | ||
klog.Errorf("Error setting flag %v to %s from environment variable %s", flag, value, env) | ||
} | ||
} | ||
} | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "led-controller", | ||
Short: "led-controller", | ||
Long: `A cli for running neopixels`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
klog.Error("You must specify a sub-command.") | ||
err := cmd.Help() | ||
if err != nil { | ||
klog.Error(err) | ||
} | ||
os.Exit(1) | ||
}, | ||
} | ||
|
||
// Execute the stuff | ||
func Execute(VERSION string, COMMIT string) { | ||
version = VERSION | ||
commit = COMMIT | ||
if err := rootCmd.Execute(); err != nil { | ||
klog.Error(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Prints the current version of the tool.", | ||
Long: `Prints the current version.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("Version:" + version + " Commit:" + commit) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"k8s.io/klog" | ||
"github.com/sudermanjr/led-controller/cmd" | ||
) | ||
|
||
func main() { | ||
Execute(version, commit) | ||
} | ||
|
||
var ( | ||
version = "development" | ||
commit = "n/a" | ||
ledCount int | ||
maxBrightness int | ||
minBrightness int | ||
fadeDuration int | ||
colorName string | ||
// version is set during build | ||
version = "development" | ||
// commit is set during build | ||
commit = "n/a" | ||
) | ||
|
||
func init() { | ||
// Flags | ||
rootCmd.PersistentFlags().IntVarP(&ledCount, "led-count", "l", 12, "The number of LEDs in the array.") | ||
rootCmd.PersistentFlags().IntVar(&maxBrightness, "max-brightness", 200, "The maximum brightness that will work within the 0-250 range.") | ||
rootCmd.PersistentFlags().IntVar(&minBrightness, "min-brightness", 25, "The minimum brightness that will work within the 0-250 range.") | ||
rootCmd.PersistentFlags().IntVarP(&fadeDuration, "fade-duration", "f", 100, "The duration of fade-ins and fade-outs in ms.") | ||
|
||
//Commands | ||
rootCmd.AddCommand(versionCmd) | ||
|
||
klog.InitFlags(nil) | ||
flag.Parse() | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
|
||
environmentVariables := map[string]string{ | ||
"LED_COUNT": "led-count", | ||
"MAX_BRIGHTNESS": "max-brightness", | ||
"MIN_BRIGHTNESS": "min-brightness", | ||
"FADE_DURTION": "fade-duration", | ||
} | ||
|
||
for env, flag := range environmentVariables { | ||
flag := rootCmd.PersistentFlags().Lookup(flag) | ||
flag.Usage = fmt.Sprintf("%v [%v]", flag.Usage, env) | ||
if value := os.Getenv(env); value != "" { | ||
err := flag.Value.Set(value) | ||
if err != nil { | ||
klog.Errorf("Error setting flag %v to %s from environment variable %s", flag, value, env) | ||
} | ||
} | ||
} | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "led-controller", | ||
Short: "led-controller", | ||
Long: `A cli for running neopixels`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
klog.Error("You must specify a sub-command.") | ||
err := cmd.Help() | ||
if err != nil { | ||
klog.Error(err) | ||
} | ||
os.Exit(1) | ||
}, | ||
} | ||
|
||
// Execute the stuff | ||
func Execute(VERSION string, COMMIT string) { | ||
version = VERSION | ||
commit = COMMIT | ||
if err := rootCmd.Execute(); err != nil { | ||
klog.Error(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Prints the current version of the tool.", | ||
Long: `Prints the current version.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("Version:" + version + " Commit:" + commit) | ||
}, | ||
func main() { | ||
cmd.Execute(version, commit) | ||
} |
Oops, something went wrong.