feat: updated implementations for cmds

This commit is contained in:
David Allen 2025-08-24 20:39:39 -06:00
parent 7a96bfd6c7
commit 59a5225b28
Signed by: towk
GPG key ID: 0430CDBE22619155
7 changed files with 331 additions and 102 deletions

View file

@ -1,42 +1,57 @@
package cmd
import (
"net/url"
"time"
"git.towk2.me/towk/configurator/pkg/service"
"git.towk2.me/towk/makeshift/pkg/service"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var serveCmd = &cobra.Command{
Use: "serve",
Use: "serve",
Example: `
# start the service in current directory
makeshift serve
# start the service with root path and initialize
makeshift serve --root ./test --init -l debug
`,
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
// try and set flags using env vars
setenv(&host, "CONFIGURATOR_HOST")
setenv(&rootPath, "CONFIGURATOR_ROOT")
setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "root", "MAKESHIFT_SERVER_ROOT")
setenv(cmd, "timeout", "MAKESHIFT_TIMEOUT")
},
Run: func(cmd *cobra.Command, args []string) {
var (
host string
rootPath string
server *service.Service
err error
host, _ = cmd.Flags().GetString("host")
rootPath, _ = cmd.Flags().GetString("root")
timeout, _ = cmd.Flags().GetInt("timeout")
parsed *url.URL
server *service.Service
err error
)
// get vars but don't modify
host, _ = cmd.Flags().GetString("host")
rootPath, _ = cmd.Flags().GetString("root")
// parse the host to remove scheme if needed
parsed, err = url.Parse(host)
if err != nil {
log.Warn().Err(err).
Str("host", host).
Msg("could not parse host")
}
// set the server values
server = service.New()
server.Addr = host
server.Addr = parsed.Host
server.RootPath = rootPath
server.Timeout = time.Duration(timeout) * time.Second
// show some debugging information
log.Debug().
Str("host", host).
Str("host", parsed.Host).
Any("paths", map[string]string{
"root": rootPath,
"data": server.PathForData(),
@ -59,9 +74,9 @@ var serveCmd = &cobra.Command{
func init() {
serveCmd.Flags().Bool("init", false, "Initializes default files at specified with the '--root' flag")
serveCmd.Flags().StringVar(&host, "host", "localhost:5050", "Set the configurator server host (can be set with CONFIGURATOR_HOST)")
serveCmd.Flags().StringVar(&rootPath, "root", "./", "Set the root path to serve files (can be set with CONFIGURATOR_ROOT)")
serveCmd.Flags().IntVarP(&timeout, "timeout", "t", 60, "Set the timeout in seconds for requests.")
serveCmd.Flags().String("host", "localhost:5050", "Set the configurator server host (can be set with MAKESHIFT_HOST)")
serveCmd.Flags().String("root", "./", "Set the root path to serve files (can be set with MAKESHIFT_ROOT)")
serveCmd.Flags().IntP("timeout", "t", 60, "Set the timeout in seconds for requests (can be set with MAKESHIFT_TIMEOUT)")
rootCmd.AddCommand(serveCmd)
}