From 1c0a863241075ec026723c37b1a058a8330c4ff0 Mon Sep 17 00:00:00 2001 From: Mark Spicer Date: Thu, 14 Apr 2022 12:44:15 -0400 Subject: [PATCH] chore: Refactor code out of the root (#253) * cmd: Add directory for commands. I went to add another command and realized the root of this project is becoming large. This commit moves commands to a cmd package to keep them out of the root. * docs: Move build instructions. This commit moves the build instructions into the docs folder to keep them out of the root. --- README.md | 2 +- encrypt.go => cmd/encrypt.go | 8 ++------ push.go => cmd/push.go | 11 +++++------ render.go => cmd/render.go | 11 +++++------ serve.go => cmd/serve.go | 11 +++++------ BUILD.md => docs/BUILD.md | 2 ++ BUILD_WINDOWS.md => docs/BUILD_WINDOWS.md | 0 main.go | 9 ++++++++- 8 files changed, 28 insertions(+), 26 deletions(-) rename encrypt.go => cmd/encrypt.go (93%) rename push.go => cmd/push.go (89%) rename render.go => cmd/render.go (91%) rename serve.go => cmd/serve.go (65%) rename BUILD.md => docs/BUILD.md (85%) rename BUILD_WINDOWS.md => docs/BUILD_WINDOWS.md (100%) diff --git a/README.md b/README.md index f00d86242b..a1518f79d4 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ brew install tidbyt/tidbyt/pixlet Download the `pixlet` binary from [the latest release][1]. -Alternatively you can [build from source](BUILD.md). +Alternatively you can [build from source](docs/BUILD.md). [1]: https://github.com/tidbyt/pixlet/releases/latest diff --git a/encrypt.go b/cmd/encrypt.go similarity index 93% rename from encrypt.go rename to cmd/encrypt.go index bc270b06f2..335cbae824 100644 --- a/encrypt.go +++ b/cmd/encrypt.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "fmt" @@ -26,11 +26,7 @@ const PublicKeysetJSON = `{ ] }` -func init() { - rootCmd.AddCommand(encryptCmd) -} - -var encryptCmd = &cobra.Command{ +var EncryptCmd = &cobra.Command{ Use: "encrypt [app name] [secret value]...", Short: "Encrypts secrets for use in an app that will be submitted to the Tidbyt community repo", Example: "encrypt weather my-top-secretweather-api-key-123456", diff --git a/push.go b/cmd/push.go similarity index 89% rename from push.go rename to cmd/push.go index 16ddc6ef57..28931a186b 100644 --- a/push.go +++ b/cmd/push.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "bytes" @@ -31,13 +31,12 @@ type TidbytPushJSON struct { } func init() { - rootCmd.AddCommand(pushCmd) - pushCmd.Flags().StringVarP(&apiToken, "api-token", "t", "", "Tidbyt API token") - pushCmd.Flags().StringVarP(&installationID, "installation-id", "i", "", "Give your installation an ID to keep it in the rotation") - pushCmd.Flags().BoolVarP(&background, "background", "b", false, "Don't immediately show the image on the device") + PushCmd.Flags().StringVarP(&apiToken, "api-token", "t", "", "Tidbyt API token") + PushCmd.Flags().StringVarP(&installationID, "installation-id", "i", "", "Give your installation an ID to keep it in the rotation") + PushCmd.Flags().BoolVarP(&background, "background", "b", false, "Don't immediately show the image on the device") } -var pushCmd = &cobra.Command{ +var PushCmd = &cobra.Command{ Use: "push [device ID] [webp image] [installationID]", Short: "Pushes a webp image to a Tidbyt device", Args: cobra.MinimumNArgs(2), diff --git a/render.go b/cmd/render.go similarity index 91% rename from render.go rename to cmd/render.go index 7893d0ebb1..db826fff8c 100644 --- a/render.go +++ b/cmd/render.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "fmt" @@ -21,10 +21,9 @@ var ( ) func init() { - rootCmd.AddCommand(renderCmd) - renderCmd.Flags().StringVarP(&output, "output", "o", "", "Path for rendered image") - renderCmd.Flags().BoolVarP(&renderGif, "gif", "", false, "Generate GIF instead of WebP") - renderCmd.Flags().IntVarP( + RenderCmd.Flags().StringVarP(&output, "output", "o", "", "Path for rendered image") + RenderCmd.Flags().BoolVarP(&renderGif, "gif", "", false, "Generate GIF instead of WebP") + RenderCmd.Flags().IntVarP( &magnify, "magnify", "m", @@ -33,7 +32,7 @@ func init() { ) } -var renderCmd = &cobra.Command{ +var RenderCmd = &cobra.Command{ Use: "render [script] [=value>]...", Short: "Runs script with provided config parameters.", Args: cobra.MinimumNArgs(1), diff --git a/serve.go b/cmd/serve.go similarity index 65% rename from serve.go rename to cmd/serve.go index 7c0cc6f88e..9f98ea3914 100644 --- a/serve.go +++ b/cmd/serve.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "github.com/spf13/cobra" @@ -13,13 +13,12 @@ var ( ) func init() { - rootCmd.AddCommand(serveCmd) - serveCmd.Flags().StringVarP(&host, "host", "i", "127.0.0.1", "Host interface for serving rendered images") - serveCmd.Flags().IntVarP(&port, "port", "p", 8080, "Port for serving rendered images") - serveCmd.Flags().BoolVarP(&watch, "watch", "w", false, "Reload scripts on change") + ServeCmd.Flags().StringVarP(&host, "host", "i", "127.0.0.1", "Host interface for serving rendered images") + ServeCmd.Flags().IntVarP(&port, "port", "p", 8080, "Port for serving rendered images") + ServeCmd.Flags().BoolVarP(&watch, "watch", "w", false, "Reload scripts on change") } -var serveCmd = &cobra.Command{ +var ServeCmd = &cobra.Command{ Use: "serve [script]", Short: "Serves a starlark render script over HTTP.", Args: cobra.ExactArgs(1), diff --git a/BUILD.md b/docs/BUILD.md similarity index 85% rename from BUILD.md rename to docs/BUILD.md index d3a7a24ca6..7db6dc8023 100644 --- a/BUILD.md +++ b/docs/BUILD.md @@ -1,6 +1,8 @@ Building Pixlet =============== +Note - if you're trying to build for windows, check out the [windows build instructions](BUILD_WINDOWS.md). + Prerequisites ------------- diff --git a/BUILD_WINDOWS.md b/docs/BUILD_WINDOWS.md similarity index 100% rename from BUILD_WINDOWS.md rename to docs/BUILD_WINDOWS.md diff --git a/main.go b/main.go index 8a3d6e4760..9dcb995913 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "os" "github.com/spf13/cobra" + "tidbyt.dev/pixlet/cmd" ) var rootCmd = &cobra.Command{ @@ -13,10 +14,16 @@ var rootCmd = &cobra.Command{ Long: "Pixlet renders graphics for pixel devices, like Tidbyt", } +func init() { + rootCmd.AddCommand(cmd.ServeCmd) + rootCmd.AddCommand(cmd.RenderCmd) + rootCmd.AddCommand(cmd.PushCmd) + rootCmd.AddCommand(cmd.EncryptCmd) +} + func main() { if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - }