NewDevCommand function

NewDevCommand returns the dev command.

Returns:

  • *command.Command
Show/Hide Function Body
{
	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
}

runDev function

Parameters:

  • cmd *command.Command
  • _ *command.RootFlags
  • _ []string

Returns:

  • error
Show/Hide Function Body
{
	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()
}

readPortFromManifest function

Returns:

  • string
Show/Hide Function Body
{
	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 function

NewInitCommand returns the init command.

Returns:

  • *command.Command
Show/Hide Function Body
{
	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
}

runInit function

Parameters:

  • cmd *command.Command
  • _ *command.RootFlags
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	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 function

NewBuildCommand returns the build command.

Returns:

  • *command.Command
Show/Hide Function Body
{
	cmd := &command.Command{
		Name:        "build",
		Usage:       "build",
		Description: "Build the current project",
		Run:         runBuild,
	}
	return cmd
}

runBuild function

Parameters:

  • cmd *command.Command
  • _ *command.RootFlags
  • _ []string

Returns:

  • error
Show/Hide Function Body
{
	if err := build.Build(); err != nil {
		return err
	}
	cmd.Logger.Success("Build completed")
	return nil
}

TestReadPortFromManifest function

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.

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	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)
	}
}

encoding/json import

Import example:

import "encoding/json"

os import

Import example:

import "os"

strconv import

Import example:

import "strconv"

github.com/mirkobrombin/go-cli-builder/v1/command import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/command"

github.com/rfwlab/rfw/cmd/rfw/server import

Import example:

import "github.com/rfwlab/rfw/cmd/rfw/server"

fmt import

Import example:

import "fmt"

github.com/mirkobrombin/go-cli-builder/v1/command import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/command"

github.com/rfwlab/rfw/cmd/rfw/initproj import

Import example:

import "github.com/rfwlab/rfw/cmd/rfw/initproj"

github.com/mirkobrombin/go-cli-builder/v1/command import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/command"

github.com/rfwlab/rfw/cmd/rfw/build import

Import example:

import "github.com/rfwlab/rfw/cmd/rfw/build"

os import

Import example:

import "os"

path/filepath import

Import example:

import "path/filepath"

testing import

Import example:

import "testing"