fix(dev): address CLI review feedback
This commit is contained in:
18 files changed
+307
-160
No files matched your search
@@ -1,20 +1,22 @@
|
||||
package run
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/homarr-labs/homarr/tools/homarr-dev/internal/docker"
|
||||
"github.com/homarr-labs/homarr/tools/homarr-dev/internal/gh"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Context context.Context
|
||||
PR int
|
||||
Tag string
|
||||
Demo bool
|
||||
Env []string
|
||||
FetchTitle bool
|
||||
FindPort func(int) int
|
||||
}
|
||||
|
||||
type Plan struct {
|
||||
@@ -22,7 +24,6 @@ type Plan struct {
|
||||
Name string
|
||||
Volume string
|
||||
Label string
|
||||
TabTitle string
|
||||
HostPort int
|
||||
Env []string
|
||||
Platform string
|
||||
@@ -32,6 +33,14 @@ type Plan struct {
|
||||
|
||||
func BuildPlan(opts Options) (*Plan, error) {
|
||||
p := &Plan{}
|
||||
ctx := opts.Context
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
findPort := opts.FindPort
|
||||
if findPort == nil {
|
||||
findPort = docker.FindFreePort
|
||||
}
|
||||
|
||||
if opts.PR > 0 {
|
||||
p.PRNumber = opts.PR
|
||||
@@ -42,9 +51,8 @@ func BuildPlan(opts Options) (*Plan, error) {
|
||||
p.Pull = true
|
||||
p.Platform = "linux/amd64"
|
||||
if opts.FetchTitle {
|
||||
if pr, err := gh.GetPR(opts.PR); err == nil && pr.Title != "" {
|
||||
if pr, err := gh.GetPR(ctx, opts.PR); err == nil && pr.Title != "" {
|
||||
p.Label = fmt.Sprintf("PR #%d — %s", opts.PR, pr.Title)
|
||||
p.TabTitle = truncate(fmt.Sprintf("🦞 %s", pr.Title), 40)
|
||||
}
|
||||
}
|
||||
} else if opts.Tag != "" {
|
||||
@@ -56,11 +64,10 @@ func BuildPlan(opts Options) (*Plan, error) {
|
||||
return nil, fmt.Errorf("need a tag or --pr")
|
||||
}
|
||||
|
||||
if p.TabTitle == "" {
|
||||
p.TabTitle = "🦞 " + strings.TrimPrefix(p.Label, "tag: ")
|
||||
p.HostPort = findPort(7575)
|
||||
if p.HostPort == 0 {
|
||||
return nil, fmt.Errorf("no free host port is available")
|
||||
}
|
||||
|
||||
p.HostPort = docker.FindFreePort(7575)
|
||||
p.Env = append(p.Env, opts.Env...)
|
||||
if opts.Demo {
|
||||
p.Env = append(p.Env, "DEMO_MODE=true", "UNSAFE_ENABLE_MOCK_INTEGRATION=true")
|
||||
@@ -101,38 +108,12 @@ func start(p *Plan, daemon bool) error {
|
||||
}
|
||||
previousPort := p.HostPort
|
||||
p.HostPort = docker.FindFreePort(p.HostPort + 1)
|
||||
if p.HostPort == 0 {
|
||||
return fmt.Errorf("no free host port is available: %w", err)
|
||||
}
|
||||
if !daemon {
|
||||
fmt.Fprintf(os.Stderr, "Port %d unavailable; retrying on %d.\n", previousPort, p.HostPort)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetTerminalChrome(title string) {
|
||||
fmt.Printf("\033]0;%s\007", title)
|
||||
if os.Getenv("ITERM_SESSION_ID") != "" {
|
||||
fmt.Printf("\033]6;1;bg;red;brightness;%d\007", randInt())
|
||||
fmt.Printf("\033]6;1;bg;green;brightness;%d\007", randInt())
|
||||
fmt.Printf("\033]6;1;bg;blue;brightness;%d\007", randInt())
|
||||
}
|
||||
if tabID := os.Getenv("TABBY_TAB_ID"); tabID != "" {
|
||||
if dir := os.Getenv("TABBY_TITLES_DIR"); dir != "" {
|
||||
os.WriteFile(fmt.Sprintf("%s/%s.txt", dir, tabID), []byte(title), 0644)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var seed = int64(os.Getpid())
|
||||
|
||||
func randInt() int {
|
||||
seed = seed*1103515245 + 12345
|
||||
return int((seed / 65536) % 256)
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= n {
|
||||
return s
|
||||
}
|
||||
return string(r[:n-1]) + "…"
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func TestPRPlanUsesAMD64(t *testing.T) {
|
||||
plan, err := BuildPlan(Options{PR: 1})
|
||||
plan, err := BuildPlan(Options{PR: 1, FindPort: func(int) int { return 7575 }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -18,6 +18,13 @@ func TestPRPlanUsesAMD64(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPlanFailsWhenNoPortIsAvailable(t *testing.T) {
|
||||
_, err := BuildPlan(Options{Tag: "dev", FindPort: func(int) int { return 0 }})
|
||||
if err == nil {
|
||||
t.Fatal("expected no-free-port error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartPRIntegration(t *testing.T) {
|
||||
if os.Getenv("HOMARR_DOCKER_INTEGRATION_TEST") != "1" {
|
||||
t.Skip("set HOMARR_DOCKER_INTEGRATION_TEST=1 and HOMARR_TEST_PR=<number>")
|
||||
|
||||
Reference in new issue
Block a user