Skip to content

Commit

Permalink
chore: Refactor code out of the root (#253)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
betterengineering authored Apr 14, 2022
1 parent 0b2b1ba commit 1c0a863
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions encrypt.go → cmd/encrypt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 5 additions & 6 deletions push.go → cmd/push.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 5 additions & 6 deletions render.go → cmd/render.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand All @@ -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",
Expand All @@ -33,7 +32,7 @@ func init() {
)
}

var renderCmd = &cobra.Command{
var RenderCmd = &cobra.Command{
Use: "render [script] [<key>=value>]...",
Short: "Runs script with provided config parameters.",
Args: cobra.MinimumNArgs(1),
Expand Down
11 changes: 5 additions & 6 deletions serve.go → cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"github.com/spf13/cobra"
Expand All @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions BUILD.md → docs/BUILD.md
Original file line number Diff line number Diff line change
@@ -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
-------------

Expand Down
File renamed without changes.
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
"tidbyt.dev/pixlet/cmd"
)

var rootCmd = &cobra.Command{
Expand All @@ -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)
}

}

0 comments on commit 1c0a863

Please sign in to comment.