NewDevCommand returns the dev command.
{
cmd := &command.Command{
Name: "dev",
Usage: "dev [--port <port>] [--host]",
Description: "Start the development server",
Run: runDev,
}
cmd.AddFlag("port", "p", "Port to serve on", "", true)
cmd.AddBoolFlag("host", "", "Expose the server to the network", false, false)
cmd.AddBoolFlag("debug", "", "Enable debug logs, profiling and overlay", false, false)
return cmd
}
{
port := os.Getenv("RFW_PORT")
if port == "" {
port = cmd.GetFlagString("port")
if port == "" {
port = readPortFromManifest()
if port == "" {
port = "8080"
}
}
}
host := cmd.GetFlagBool("host")
debug := cmd.GetFlagBool("debug")
if debug {
os.Setenv("RFW_DEVTOOLS", "1")
}
os.Setenv("RFW_DEV_BUILD", "1")
srv := server.NewServer(port, host, debug)
return srv.Start()
}
{
var manifest struct {
Port int `json:"port"`
}
data, err := os.ReadFile("rfw.json")
if err != nil {
return ""
}
if err := json.Unmarshal(data, &manifest); err != nil {
return ""
}
if manifest.Port == 0 {
return ""
}
return strconv.Itoa(manifest.Port)
}
NewInitCommand returns the init command.
{
cmd := &command.Command{
Name: "init",
Usage: "init <project-name>",
Description: "Initialize a new rfw project",
Run: runInit,
}
cmd.AddBoolFlag("skip-tidy", "", "Skip running go mod tidy", false, false)
return cmd
}
{
if len(args) < 1 {
return fmt.Errorf("please specify a project name")
}
projectName := args[0]
skipTidy := cmd.GetFlagBool("skip-tidy")
if err := initproj.InitProject(projectName, skipTidy); err != nil {
return err
}
cmd.Logger.Success("Project initialized")
return nil
}
NewBuildCommand returns the build command.
{
cmd := &command.Command{
Name: "build",
Usage: "build",
Description: "Build the current project",
Run: runBuild,
}
return cmd
}
{
if err := build.Build(); err != nil {
return err
}
cmd.Logger.Success("Build completed")
return nil
}
TestReadPortFromManifest verifies that readPortFromManifest reads the port
value from rfw.json when present and falls back to an empty string when the
file is missing or malformed.
{
origWD, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
t.Cleanup(func() { _ = os.Chdir(origWD) })
dir := t.TempDir()
if err := os.Chdir(dir); err != nil {
t.Fatalf("chdir: %v", err)
}
// Missing file should return empty string.
if got := readPortFromManifest(); got != "" {
t.Fatalf("expected empty string, got %q", got)
}
// Create manifest with a port value.
data := []byte(`{"port": 9090}`)
if err := os.WriteFile(filepath.Join(dir, "rfw.json"), data, 0o644); err != nil {
t.Fatalf("write rfw.json: %v", err)
}
if got := readPortFromManifest(); got != "9090" {
t.Fatalf("expected 9090, got %q", got)
}
// Zero port should return empty string.
if err := os.WriteFile("rfw.json", []byte(`{"port":0}`), 0o644); err != nil {
t.Fatalf("rewrite rfw.json: %v", err)
}
if got := readPortFromManifest(); got != "" {
t.Fatalf("expected empty string for zero port, got %q", got)
}
}
import "encoding/json"
import "os"
import "strconv"
import "github.com/mirkobrombin/go-cli-builder/v1/command"
import "github.com/rfwlab/rfw/cmd/rfw/server"
import "fmt"
import "github.com/mirkobrombin/go-cli-builder/v1/command"
import "github.com/rfwlab/rfw/cmd/rfw/initproj"
import "github.com/mirkobrombin/go-cli-builder/v1/command"
import "github.com/rfwlab/rfw/cmd/rfw/build"
import "os"
import "path/filepath"
import "testing"