Introduce an App struct that executes core business logic

The Cobra commands are now a light wrapper around the App struct.

Co-authored-by: Jose Garcia <josebalius@github.com>
This commit is contained in:
Mislav Marohnić 2021-09-24 16:03:44 +02:00
parent 8807b3a73a
commit ca0f89d3bc
15 changed files with 557 additions and 175 deletions

View file

@ -1,10 +1,8 @@
package ghcs
import (
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
@ -15,10 +13,7 @@ import (
var version = "DEV" // Replaced in the release build process (by GoReleaser or Homebrew) by the git tag version number.
// GithubToken is a temporary stopgap to make the token configurable by apps that import this package
var GithubToken = os.Getenv("GITHUB_TOKEN")
func NewRootCmd() *cobra.Command {
func NewRootCmd(app *App) *cobra.Command {
var lightstep string
root := &cobra.Command{
@ -32,28 +27,23 @@ token to access the GitHub API with.`,
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if os.Getenv("GITHUB_TOKEN") == "" {
return ErrTokenMissing
}
return initLightstep(lightstep)
},
}
root.PersistentFlags().StringVar(&lightstep, "lightstep", "", "Lightstep tracing endpoint (service:token@host:port)")
root.AddCommand(newCodeCmd())
root.AddCommand(newCreateCmd())
root.AddCommand(newDeleteCmd())
root.AddCommand(newListCmd())
root.AddCommand(newLogsCmd())
root.AddCommand(newPortsCmd())
root.AddCommand(newSSHCmd())
root.AddCommand(newCodeCmd(app))
root.AddCommand(newCreateCmd(app))
root.AddCommand(newDeleteCmd(app))
root.AddCommand(newListCmd(app))
root.AddCommand(newLogsCmd(app))
root.AddCommand(newPortsCmd(app))
root.AddCommand(newSSHCmd(app))
return root
}
var ErrTokenMissing = errors.New("GITHUB_TOKEN is missing")
// initLightstep parses the --lightstep=service:token@host:port flag and
// enables tracing if non-empty.
func initLightstep(config string) error {