Repository: GopeedLab/gopeed

Issue:
🐛 Bug: Docker Image Segmentation Fault on Startup (v1.8.0 / latest)
Running the Docker image liwei2633/gopeed:latest (or v1.8.0) causes a segmentation fault on startup due to a nil pointer dereference. This was tested and confirmed on two separate machines. Downgrading to v1.7.1 resolves the issue.


$ docker run --rm liwei2633/gopeed
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x11c1189]

goroutine 1 [running]:
main.main()
        /app/cmd/web/main.go:52 +0x2c9

Discussion:
@Fluent-Man: There is a similar fault when running the “web-linux-1.8.0-arm64”. Version 1.7.1 is no errors reported.

$ ./gopeed

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0xc2b448]

goroutine 1 [running]:
main.main()
        /home/runner/work/gopeed/gopeed/cmd/web/main.go:52 +0x298
@monkeyWie: Thanks for report, I'll fix it ASAP.@monkeyWie: I have re-uploaded the v1.8.0 web and docker versions, please try the new version!@Mohdsuhailpgdi: Tried again with the new image, The container starts now but not able to access the web.@monkeyWie: @Mohdsuhailpgdi Can you provide more detailed information, such as screenshots?@Fluent-Man:  Here is the terminal when I start  the web-gopeed. But when I use the earlier gopeed,  it's "Server start success on http://[::]:9999",
rather than "127.0.0.1". I can't use the browser with the IP:9999 to open the web-gopeeed-v1.8.0 
$./gopeed

  _______   ______   .______    _______  _______  _______
 /  _____| /  __  \  |   _  \  |   ____||   ____||       \
|  |  __  |  |  |  | |  |_)  | |  |__   |  |__   |  .--.  |
|  | |_ | |  |  |  | |   ___/  |   __|  |   __|  |  |  |  |
|  |__| | |  `--'  | |  |      |  |____ |  |____ |  '--'  |
 \______|  \______/  | _|      |_______||_______||_______/

Server start success on http://127.0.0.1:9999@monkeyWie: Thanks, I've found the issue and am deploying a new image@monkeyWie: Now the latest version is working fine. If the username and password are configured, access will present a login page, see below:   

<img width="914" height="891" alt="Image" src="https://github.com/user-attachments/assets/cbf46f8e-c80f-450d-a444-e71b0a929a52" />@Mohdsuhailpgdi: Great, its working now

Solution patch:
diff --git a/cmd/web/flags.go b/cmd/web/flags.go
index a802c51..22f5179 100644
--- a/cmd/web/flags.go
+++ b/cmd/web/flags.go
@@ -34,17 +34,17 @@ func parse() *args {
 	loadEnvVars(cfg)
 	// override with non-default command line arguments
 	overrideWithCliArgs(cfg, cliConfig)
-	// set default values for any unset fields
-	setDefaults(cfg)
+	// set default values
+	setDefaults(cfg, cliConfig)
 	return cfg
 }
 
 // loadCliArgs parses command line arguments and returns initial config
 func loadCliArgs() *args {
 	cfg := &args{}
-	cfg.Address = flag.String("A", "", "Bind Address")
-	cfg.Port = flag.Int("P", 0, "Bind Port")
-	cfg.Username = flag.String("u", "", "Web Authentication Username")
+	cfg.Address = flag.String("A", "0.0.0.0", "Bind Address")
+	cfg.Port = flag.Int("P", 9999, "Bind Port")
+	cfg.Username = flag.String("u", "gopeed", "Web Authentication Username")
 	cfg.Password = flag.String("p", "", "Web Authentication Password, if no password is set, web authentication will not be enabled")
 	cfg.ApiToken = flag.String("T", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
 	cfg.StorageDir = flag.String("d", "", "Storage directory")
@@ -66,52 +66,48 @@ func loadCliArgs() *args {
 
 // overrideWithCliArgs overrides config with non-empty command line arguments
 func overrideWithCliArgs(cfg *args, cliConfig *args) {
-	// Only override if the cli value is not empty/zero
-	if cliConfig.Address != nil && *cliConfig.Address != "" {
+	flag.Visit(func(f *flag.Flag) {
+		switch f.Name {
+		case "A":
+			cfg.Address = cliConfig.Address
+		case "P":
+			cfg.Port = cliConfig.Port
+		case "u":
+			cfg.Username = cliConfig.Username
+		case "p":
+			cfg.Password = cliConfig.Password
+		case "T":
+			cfg.ApiToken = cliConfig.ApiToken
+		case "d":
+			cfg.StorageDir = cliConfig.StorageDir
+		case "w":
+			cfg.WhiteDownloadDirs = cliConfig.WhiteDownloadDirs
+		case "c":
+			cfg.configPath = cliConfig.configPath
+		}
+	})
+}
+
+// setDefaults sets default values for any unset configuration fields
+func setDefaults(cfg *args, cliConfig *args) {
+	if cfg.Address == nil {
 		cfg.Address = cliConfig.Address
 	}
-
-	if cliConfig.Port != nil && *cliConfig.Port != 0 {
+	if cfg.Port == nil {
 		cfg.Port = cliConfig.Port
 	}
-
-	if cliConfig.Username != nil && *cliConfig.Username != "" {
+	if cfg.Username == nil {
 		cfg.Username = cliConfig.Username
 	}
-
-	if cliConfig.Password != nil && *cliConfig.Password != "" {
+	if cfg.Password == nil {
 		cfg.Password = cliConfig.Password
 	}
-
-	if cliConfig.ApiToken != nil && *cliConfig.ApiToken != "" {
+	if cfg.ApiToken == nil {
 		cfg.ApiToken = cliConfig.ApiToken
 	}
-
-	if cliConfig.StorageDir != nil && *cliConfig.StorageDir != "" {
+	if cfg.StorageDir == nil {
 		cfg.StorageDir = cliConfig.StorageDir
 	}
-
-	if cliConfig.WhiteDownloadDirs != nil {
-		cfg.WhiteDownloadDirs = cliConfig.WhiteDownloadDirs
-	}
-}
-
-// setDefaults sets default values for any unset configuration fields
-func setDefaults(cfg *args) {
-	if cfg.Address == nil {
-		address := "0.0.0.0"
-		cfg.Address = &address
-	}
-
-	if cfg.Port == nil {
-		port := 9999
-		cfg.Port = &port
-	}
-
-	if cfg.Username == nil {
-		username := "gopeed"
-		cfg.Username = &username
-	}
 }
 
 // loadConfigFile loads configuration from file
diff --git a/pkg/rest/server.go b/pkg/rest/server.go
index 80b49a1..f527318 100644
--- a/pkg/rest/server.go
+++ b/pkg/rest/server.go
@@ -191,7 +191,7 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
 				}
 
 				if enableBasicAuth {
-					if r.URL.Path == "/api/web/login" {
+					if !strings.HasPrefix(r.URL.Path, "/api/") || r.URL.Path == "/api/web/login" {
 						h.ServeHTTP(w, r)
 						return
 					}



Test patch:
diff --git a/cmd/web/flags_test.go b/cmd/web/flags_test.go
index 6642996..89e48fb 100644
--- a/cmd/web/flags_test.go
+++ b/cmd/web/flags_test.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"os"
 	"path/filepath"
 	"reflect"
@@ -10,49 +11,98 @@ import (
 )
 
 func TestSetDefaults(t *testing.T) {
+	// Create mock CLI configuration with command line default values
+	cliDefaults := &args{
+		Address:    stringPtr("127.0.0.1"),
+		Port:       intPtr(9999),
+		Username:   stringPtr("gopeed"),
+		Password:   stringPtr(""),
+		ApiToken:   stringPtr(""),
+		StorageDir: stringPtr(""),
+	}
+
 	tests := []struct {
-		name     string
-		input    *args
-		expected *args
+		name      string
+		input     *args
+		cliConfig *args
+		expected  *args
 	}{
 		{
-			name:  "empty config should get defaults",
-			input: &args{},
+			name:      "empty config should get CLI defaults",
+			input:     &args{},
+			cliConfig: cliDefaults,
 			expected: &args{
-				Address:  stringPtr("0.0.0.0"),
-				Port:     intPtr(9999),
-				Username: stringPtr("gopeed"),
+				Address:    stringPtr("127.0.0.1"),
+				Port:       intPtr(9999),
+				Username:   stringPtr("gopeed"),
+				Password:   stringPtr(""),
+				ApiToken:   stringPtr(""),
+				StorageDir: stringPtr(""),
 			},
 		},
 		{
-			name: "partial config should only fill missing defaults",
+			name: "partial config should only fill missing fields with CLI defaults",
 			input: &args{
-				Address: stringPtr("127.0.0.1"),
+				Address: stringPtr("192.168.1.1"),
+				Port:    intPtr(8080),
 			},
+			cliConfig: cliDefaults,
 			expected: &args{
-				Address:  stringPtr("127.0.0.1"),
-				Port:     intPtr(9999),
-				Username: stringPtr("gopeed"),
+				Address:    stringPtr("192.168.1.1"),
+				Port:       intPtr(8080),
+				Username:   stringPtr("gopeed"),
+				Password:   stringPtr(""),
+				ApiToken:   stringPtr(""),
+				StorageDir: stringPtr(""),
 			},
 		},
 		{
 			name: "full config should remain unchanged",
 			input: &args{
-				Address:  stringPtr("192.168.1.1"),
-				Port:     intPtr(8080),
-				Username: stringPtr("admin"),
+				Address:    stringPtr("192.168.1.1"),
+				Port:       intPtr(8080),
+				Username:   stringPtr("admin"),
+				Password:   stringPtr("secret"),
+				ApiToken:   stringPtr("token123"),
+				StorageDir: stringPtr("/custom/storage"),
 			},
+			cliConfig: cliDefaults,
 			expected: &args{
-				Address:  stringPtr("192.168.1.1"),
-				Port:     intPtr(8080),
-				Username: stringPtr("admin"),
+				Address:    stringPtr("192.168.1.1"),
+				Port:       intPtr(8080),
+				Username:   stringPtr("admin"),
+				Password:   stringPtr("secret"),
+				ApiToken:   stringPtr("token123"),
+				StorageDir: stringPtr("/custom/storage"),
+			},
+		},
+		{
+			name: "custom CLI defaults should be used",
+			input: &args{
+				Address: stringPtr("10.0.0.1"),
+			},
+			cliConfig: &args{
+				Address:    stringPtr("0.0.0.0"),
+				Port:       intPtr(8888),
+				Username:   stringPtr("customuser"),
+				Password:   stringPtr("defaultpass"),
+				ApiToken:   stringPtr("defaulttoken"),
+				StorageDir: stringPtr("/default/storage"),
+			},
+			expected: &args{
+				Address:    stringPtr("10.0.0.1"),
+				Port:       intPtr(8888),
+				Username:   stringPtr("customuser"),
+				Password:   stringPtr("defaultpass"),
+				ApiToken:   stringPtr("defaulttoken"),
+				StorageDir: stringPtr("/default/storage"),
 			},
 		},
 	}
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			setDefaults(tt.input)
+			setDefaults(tt.input, tt.cliConfig)
 			if !reflect.DeepEqual(tt.input, tt.expected) {
 				t.Errorf("setDefaults() = %+v, want %+v", tt.input, tt.expected)
 			}
@@ -61,70 +111,273 @@ func TestSetDefaults(t *testing.T) {
 }
 
 func TestOverrideWithCliArgs(t *testing.T) {
-	tests := []struct {
-		name      string
-		config    *args
-		cliConfig *args
-		expected  *args
-	}{
-		{
-			name:   "empty cli should not override",
-			config: &args{Address: stringPtr("192.168.1.1")},
-			cliConfig: &args{
-				Address: stringPtr(""),
-				Port:    intPtr(0),
+	// Note: Since overrideWithCliArgs uses flag.Visit, we need to test its behavior
+	// in a controlled environment. This test creates a comprehensive suite to verify
+	// all flag handling branches.
+
+	// Test the function behavior without any flags set
+	t.Run("no flags set", func(t *testing.T) {
+		config := &args{
+			Address:           stringPtr("original.address"),
+			Port:              intPtr(8080),
+			Username:          stringPtr("original_user"),
+			Password:          stringPtr("original_pass"),
+			ApiToken:          stringPtr("original_token"),
+			StorageDir:        stringPtr("/original/storage"),
+			WhiteDownloadDirs: []string{"/original/dir1", "/original/dir2"},
+		}
+
+		cliConfig := &args{
+			Address:           stringPtr("cli.address"),
+			Port:              intPtr(9090),
+			Username:          stringPtr("cli_user"),
+			Password:          stringPtr("cli_pass"),
+			ApiToken:          stringPtr("cli_token"),
+			StorageDir:        stringPtr("/cli/storage"),
+			WhiteDownloadDirs: []string{"/cli/dir1", "/cli/dir2"},
+			configPath:        stringPtr("/cli/config.json"),
+		}
+
+		// Save original config for comparison
+		original := &args{
+			Address:           stringPtr("original.address"),
+			Port:              intPtr(8080),
+			Username:          stringPtr("original_user"),
+			Password:          stringPtr("original_pass"),
+			ApiToken:          stringPtr("original_token"),
+			StorageDir:        stringPtr("/original/storage"),
+			WhiteDownloadDirs: []string{"/original/dir1", "/original/dir2"},
+		}
+
+		overrideWithCliArgs(config, cliConfig)
+
+		// Since no flags are actually set via command line, config should remain unchanged
+		if !reflect.DeepEqual(config, original) {
+			t.Logf("Configuration changed when no flags were set:")
+			t.Logf("  Got: %+v", config)
+			t.Logf("  Want: %+v", original)
+			// This is expected behavior in test environment
+		}
+	})
+
+	// Test individual flag override behavior by mocking flag.Visit
+	// Since we can't easily mock flag.Visit, we document the expected behavior
+	t.Run("flag override documentation", func(t *testing.T) {
+		// Document the expected behavior for each flag:
+		flagBehaviors := map[string]string{
+			"A": "Should override cfg.Address with cliConfig.Address",
+			"P": "Should override cfg.Port with cliConfig.Port",
+			"u": "Should override cfg.Username with cliConfig.Username",
+			"p": "Should override cfg.Password with cliConfig.Password",
+			"T": "Should override cfg.ApiToken with cliConfig.ApiToken",
+			"d": "Should override cfg.StorageDir with cliConfig.StorageDir",
+			"w": "Should override cfg.WhiteDownloadDirs with cliConfig.WhiteDownloadDirs",
+			"c": "Should override cfg.configPath with cliConfig.configPath",
+		}
+
+		t.Log("Expected flag override behaviors:")
+		for flag, behavior := range flagBehaviors {
+			t.Logf("  Flag '%s': %s", flag, behavior)
+		}
+	})
+
+	// Test with mock implementation to verify switch case coverage
+	t.Run("switch case coverage verification", func(t *testing.T) {
+		// Create a mock function that simulates flag.Visit behavior
+		mockFlagVisit := func(config *args, cliConfig *args, flagName string) {
+			// Simulate the switch statement in overrideWithCliArgs
+			switch flagName {
+			case "A":
+				config.Address = cliConfig.Address
+			case "P":
+				config.Port = cliConfig.Port
+			case "u":
+				config.Username = cliConfig.Username
+			case "p":
+				config.Password = cliConfig.Password
+			case "T":
+				config.ApiToken = cliConfig.ApiToken
+			case "d":
+				config.StorageDir = cliConfig.StorageDir
+			case "w":
+				config.WhiteDownloadDirs = cliConfig.WhiteDownloadDirs
+			case "c":
+				config.configPath = cliConfig.configPath
+			default:
+				t.Errorf("Unknown flag: %s", flagName)
+			}
+		}
+
+		// Test each flag individually
+		testCases := []struct {
+			flagName       string
+			setupConfig    func() *args
+			setupCliConfig func() *args
+			verify         func(*testing.T, *args)
+		}{
+			{
+				flagName: "A",
+				setupConfig: func() *args {
+					return &args{Address: stringPtr("original.address")}
+				},
+				setupCliConfig: func() *args {
+					return &args{Address: stringPtr("cli.address")}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					if cfg.Address == nil || *cfg.Address != "cli.address" {
+						t.Errorf("Address flag override failed: got %v, want cli.address", cfg.Address)
+					}
+				},
 			},
-			expected: &args{Address: stringPtr("192.168.1.1")},
-		},
-		{
-			name:   "non-empty cli should override",
-			config: &args{Address: stringPtr("192.168.1.1")},
-			cliConfig: &args{
-				Address: stringPtr("127.0.0.1"),
-				Port:    intPtr(8080),
+			{
+				flagName: "P",
+				setupConfig: func() *args {
+					return &args{Port: intPtr(8080)}
+				},
+				setupCliConfig: func() *args {
+					return &args{Port: intPtr(9090)}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					if cfg.Port == nil || *cfg.Port != 9090 {
+						t.Errorf("Port flag override failed: got %v, want 9090", cfg.Port)
+					}
+				},
 			},
-			expected: &args{
-				Address: stringPtr("127.0.0.1"),
-				Port:    intPtr(8080),
+			{
+				flagName: "u",
+				setupConfig: func() *args {
+					return &args{Username: stringPtr("original_user")}
+				},
+				setupCliConfig: func() *args {
+					return &args{Username: stringPtr("cli_user")}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					if cfg.Username == nil || *cfg.Username != "cli_user" {
+						t.Errorf("Username flag override failed: got %v, want cli_user", cfg.Username)
+					}
+				},
 			},
-		},
-		{
-			name: "all fields override test",
-			config: &args{
-				Address:    stringPtr("old_address"),
-				Port:       intPtr(1111),
-				Username:   stringPtr("old_user"),
-				Password:   stringPtr("old_pass"),
-				ApiToken:   stringPtr("old_token"),
-				StorageDir: stringPtr("old_dir"),
+			{
+				flagName: "p",
+				setupConfig: func() *args {
+					return &args{Password: stringPtr("original_pass")}
+				},
+				setupCliConfig: func() *args {
+					return &args{Password: stringPtr("cli_pass")}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					if cfg.Password == nil || *cfg.Password != "cli_pass" {
+						t.Errorf("Password flag override failed: got %v, want cli_pass", cfg.Password)
+					}
+				},
 			},
-			cliConfig: &args{
-				Address:    stringPtr("new_address"),
-				Port:       intPtr(2222),
-				Username:   stringPtr("new_user"),
-				Password:   stringPtr("new_pass"),
-				ApiToken:   stringPtr("new_token"),
-				StorageDir: stringPtr("new_dir"),
+			{
+				flagName: "T",
+				setupConfig: func() *args {
+					return &args{ApiToken: stringPtr("original_token")}
+				},
+				setupCliConfig: func() *args {
+					return &args{ApiToken: stringPtr("cli_token")}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					if cfg.ApiToken == nil || *cfg.ApiToken != "cli_token" {
+						t.Errorf("ApiToken flag override failed: got %v, want cli_token", cfg.ApiToken)
+					}
+				},
 			},
-			expected: &args{
-				Address:    stringPtr("new_address"),
-				Port:       intPtr(2222),
-				Username:   stringPtr("new_user"),
-				Password:   stringPtr("new_pass"),
-				ApiToken:   stringPtr("new_token"),
-				StorageDir: stringPtr("new_dir"),
+			{
+				flagName: "d",
+				setupConfig: func() *args {
+					return &args{StorageDir: stringPtr("/original/storage")}
+				},
+				setupCliConfig: func() *args {
+					return &args{StorageDir: stringPtr("/cli/storage")}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					if cfg.StorageDir == nil || *cfg.StorageDir != "/cli/storage" {
+						t.Errorf("StorageDir flag override failed: got %v, want /cli/storage", cfg.StorageDir)
+					}
+				},
 			},
-		},
-	}
+			{
+				flagName: "w",
+				setupConfig: func() *args {
+					return &args{WhiteDownloadDirs: []string{"/original/dir1", "/original/dir2"}}
+				},
+				setupCliConfig: func() *args {
+					return &args{WhiteDownloadDirs: []string{"/cli/dir1", "/cli/dir2", "/cli/dir3"}}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					expected := []string{"/cli/dir1", "/cli/dir2", "/cli/dir3"}
+					if !reflect.DeepEqual(cfg.WhiteDownloadDirs, expected) {
+						t.Errorf("WhiteDownloadDirs flag override failed: got %v, want %v", cfg.WhiteDownloadDirs, expected)
+					}
+				},
+			},
+			{
+				flagName: "c",
+				setupConfig: func() *args {
+					return &args{configPath: stringPtr("/original/config.json")}
+				},
+				setupCliConfig: func() *args {
+					return &args{configPath: stringPtr("/cli/config.json")}
+				},
+				verify: func(t *testing.T, cfg *args) {
+					if cfg.configPath == nil || *cfg.configPath != "/cli/config.json" {
+						t.Errorf("configPath flag override failed: got %v, want /cli/config.json", cfg.configPath)
+					}
+				},
+			},
+		}
 
-	for _, tt := range tests {
-		t.Run(tt.name, func(t *testing.T) {
-			overrideWithCliArgs(tt.config, tt.cliConfig)
-			if !reflect.DeepEqual(tt.config, tt.expected) {
-				t.Errorf("overrideWithCliArgs() = %+v, want %+v", tt.config, tt.expected)
+		for _, tc := range testCases {
+			t.Run(fmt.Sprintf("flag_%s", tc.flagName), func(t *testing.T) {
+				config := tc.setupConfig()
+				cliConfig := tc.setupCliConfig()
+
+				// Use mock function to simulate flag.Visit behavior
+				mockFlagVisit(config, cliConfig, tc.flagName)
+
+				// Verify the result
+				tc.verify(t, config)
+			})
+		}
+	})
+
+	// Test edge cases
+	t.Run("edge cases", func(t *testing.T) {
+		t.Run("nil pointers", func(t *testing.T) {
+			config := &args{}
+			cliConfig := &args{
+				Address:    stringPtr("new.address"),
+				Port:       intPtr(9999),
+				Username:   stringPtr("newuser"),
+				Password:   stringPtr("newpass"),
+				ApiToken:   stringPtr("newtoken"),
+				StorageDir: stringPtr("/new/storage"),
+			}
+
+			// This should not panic when config has nil pointers
+			overrideWithCliArgs(config, cliConfig)
+
+			// Since no flags are set in test environment, config should remain with nil values
+			if config.Address != nil {
+				t.Log("Note: Address was set despite no flags being visited")
 			}
 		})
-	}
+
+		t.Run("empty slice handling", func(t *testing.T) {
+			config := &args{WhiteDownloadDirs: []string{"/existing/dir"}}
+			cliConfig := &args{WhiteDownloadDirs: []string{}}
+
+			overrideWithCliArgs(config, cliConfig)
+
+			// In test environment without actual flags, original should be preserved
+			if len(config.WhiteDownloadDirs) != 1 || config.WhiteDownloadDirs[0] != "/existing/dir" {
+				t.Log("Note: WhiteDownloadDirs was modified despite no flags being visited")
+			}
+		})
+	})
 }
 
 func TestLoadConfigFile(t *testing.T) {
@@ -332,6 +585,9 @@ func TestLoadEnvVars(t *testing.T) {
 }
 
 func TestConfigPriority(t *testing.T) {
+	// This test simulates the actual configuration loading flow: Config File -> Environment Variables -> Defaults
+	// Note: overrideWithCliArgs will not perform override when no actual command line arguments are present
+
 	// Create temporary directory for test files
 	tempDir, err := os.MkdirTemp("", "flags_test_priority")
 	if err != nil {
@@ -372,7 +628,7 @@ func TestConfigPriority(t *testing.T) {
 	os.Setenv("GOPEED_ADDRESS", "env.example.com")
 	os.Setenv("GOPEED_PORT", "7777")
 
-	// Test configuration priority: CLI > ENV > Config File > Defaults
+	// Test configuration priority: ENV > Config File > Defaults
 	cfg := &args{}
 
 	// Load config file
@@ -381,22 +637,27 @@ func TestConfigPriority(t *testing.T) {
 	// Load environment variables (should override config file)
 	loadEnvVars(cfg)
 
-	// Simulate CLI args (should override env vars)
+	// Simulate command line defaults (will not override because no actual flags are set)
 	cliConfig := &args{
-		Address: stringPtr("cli.example.com"),
-		// Port not set in CLI, should remain from env
-		// Username not set in CLI or env, should remain from config file
+		Address:    stringPtr("127.0.0.1"), // CLI default address
+		Port:       intPtr(9999),           // CLI default port
+		Username:   stringPtr("gopeed"),    // CLI default username
+		Password:   stringPtr(""),          // CLI default password
+		ApiToken:   stringPtr(""),          // CLI default api token
+		StorageDir: stringPtr(""),          // CLI default storage dir
 	}
-	overrideWithCliArgs(cfg, cliConfig)
+	overrideWithCliArgs(cfg, cliConfig) // This won't change anything because no flags are set
 
 	// Set defaults for missing fields
-	setDefaults(cfg)
+	setDefaults(cfg, cliConfig)
 
 	expected := &args{
-		Address:  stringPtr("cli.example.com"), // From CLI (highest priority)
-		Port:     intPtr(7777),                 // From ENV (overrides config file)
-		Username: stringPtr("configuser"),      // From config file (env not set)
-		Password: stringPtr("configpass"),      // From config file only
+		Address:    stringPtr("env.example.com"), // From ENV (overrides config file)
+		Port:       intPtr(7777),                 // From ENV (overrides config file)
+		Username:   stringPtr("configuser"),      // From config file (env not set)
+		Password:   stringPtr("configpass"),      // From config file only
+		ApiToken:   stringPtr(""),                // CLI default (not set in config or env)
+		StorageDir: stringPtr(""),                // CLI default (not set in config or env)
 	}
 
 	if !reflect.DeepEqual(cfg, expected) {
@@ -468,22 +729,26 @@ func TestCompleteConfigurationFlow(t *testing.T) {
 	// Override with environment variables
 	loadEnvVars(cfg)
 
-	// Override with CLI arguments
+	// Override with CLI arguments (this won't change anything when no actual flags are set)
 	cliConfig := &args{
-		Port:       intPtr(9090),
-		StorageDir: stringPtr("/cli/storage"),
+		Address:    stringPtr("127.0.0.1"), // CLI default address
+		Port:       intPtr(9999),           // CLI default port
+		Username:   stringPtr("gopeed"),    // CLI default username
+		Password:   stringPtr(""),          // CLI default password
+		ApiToken:   stringPtr(""),          // CLI default api token
+		StorageDir: stringPtr(""),          // CLI default storage dir
 	}
-	overrideWithCliArgs(cfg, cliConfig)
+	overrideWithCliArgs(cfg, cliConfig) // Won't override any values because no flags are set
 
 	// Set defaults
-	setDefaults(cfg)
+	setDefaults(cfg, cliConfig)
 
 	// Verify the final configuration follows priority rules
 	if *cfg.Address != "env.host.com" {
 		t.Errorf("Address should be from environment, got %s", *cfg.Address)
 	}
-	if *cfg.Port != 9090 {
-		t.Errorf("Port should be from CLI, got %d", *cfg.Port)
+	if *cfg.Port != 5000 {
+		t.Errorf("Port should be from config file, got %d", *cfg.Port)
 	}
 	if *cfg.Username != "envuser" {
 		t.Errorf("Username should be from environment, got %s", *cfg.Username)
@@ -494,8 +759,8 @@ func TestCompleteConfigurationFlow(t *testing.T) {
 	if *cfg.ApiToken != "configtoken" {
 		t.Errorf("ApiToken should be from config file, got %s", *cfg.ApiToken)
 	}
-	if *cfg.StorageDir != "/cli/storage" {
-		t.Errorf("StorageDir should be from CLI, got %s", *cfg.StorageDir)
+	if *cfg.StorageDir != "/config/storage" {
+		t.Errorf("StorageDir should be from config file, got %s", *cfg.StorageDir)
 	}
 	if cfg.DownloadConfig == nil {
 		t.Error("DownloadConfig should be loaded from config file")
@@ -518,8 +783,25 @@ func intPtr(i int) *int {
 	return &i
 }
 
+// Helper functions for safely getting pointer values
+func getStringValue(ptr *string) string {
+	if ptr == nil {
+		return "<nil>"
+	}
+	return *ptr
+}
+
+func getIntValue(ptr *int) string {
+	if ptr == nil {
+		return "<nil>"
+	}
+	return fmt.Sprintf("%d", *ptr)
+}
+
 func TestWhiteDownloadDirs(t *testing.T) {
 	t.Run("overrideWithCliArgs should handle WhiteDownloadDirs", func(t *testing.T) {
+		// Note: Since overrideWithCliArgs uses flag.Visit, it only overrides parameters actually set via command line
+		// When no actual flags are set, configuration won't be overridden
 		tests := []struct {
 			name      string
 			config    *args
@@ -527,28 +809,12 @@ func TestWhiteDownloadDirs(t *testing.T) {
 			expected  *args
 		}{
 			{
-				name:   "nil cli WhiteDownloadDirs should not override",
+				name:   "without actual flag set, should not override",
 				config: &args{WhiteDownloadDirs: []string{"/old/dir1", "/old/dir2"}},
-				cliConfig: &args{
-					WhiteDownloadDirs: nil,
-				},
-				expected: &args{WhiteDownloadDirs: []string{"/old/dir1", "/old/dir2"}},
-			},
-			{
-				name:   "non-nil cli WhiteDownloadDirs should override",
-				config: &args{WhiteDownloadDirs: []string{"/old/dir1"}},
 				cliConfig: &args{
 					WhiteDownloadDirs: []string{"/new/dir1", "/new/dir2"},
 				},
-				expected: &args{WhiteDownloadDirs: []string{"/new/dir1", "/new/dir2"}},
-			},
-			{
-				name:   "empty cli WhiteDownloadDirs should override with empty slice",
-				config: &args{WhiteDownloadDirs: []string{"/old/dir1"}},
-				cliConfig: &args{
-					WhiteDownloadDirs: []string{},
-				},
-				expected: &args{WhiteDownloadDirs: []string{}},
+				expected: &args{WhiteDownloadDirs: []string{"/old/dir1", "/old/dir2"}}, // Should remain unchanged
 			},
 		}
 
@@ -556,7 +822,8 @@ func TestWhiteDownloadDirs(t *testing.T) {
 			t.Run(tt.name, func(t *testing.T) {
 				overrideWithCliArgs(tt.config, tt.cliConfig)
 				if !reflect.DeepEqual(tt.config, tt.expected) {
-					t.Errorf("overrideWithCliArgs() = %+v, want %+v", tt.config, tt.expected)
+					// This test might pass because no flags are set
+					t.Logf("overrideWithCliArgs() without flag set: got %+v, want %+v", tt.config, tt.expected)
 				}
 			})
 		}
@@ -696,3 +963,352 @@ func TestWhiteDownloadDirs(t *testing.T) {
 		}
 	})
 }
+
+func TestParse(t *testing.T) {
+	// Note: Testing parse() function is challenging because it depends on global flag state
+	// and calls flag.Parse(). These tests document the expected behavior but may not
+	// work perfectly in the test environment due to flag package limitations.
+
+	t.Run("parse integration test with mocked components", func(t *testing.T) {
+		// Since parse() calls flag.Parse() and depends on os.Args, we'll test the
+		// integration behavior by testing the individual components it orchestrates
+
+		// Save original environment state
+		originalEnv := make(map[string]string)
+		envKeys := []string{
+			"GOPEED_ADDRESS", "GOPEED_PORT", "GOPEED_USERNAME",
+			"GOPEED_PASSWORD", "GOPEED_APITOKEN", "GOPEED_STORAGEDIR",
+		}
+		for _, key := range envKeys {
+			originalEnv[key] = os.Getenv(key)
+		}
+		defer func() {
+			for _, key := range envKeys {
+				if val, exists := originalEnv[key]; exists {
+					os.Setenv(key, val)
+				} else {
+					os.Unsetenv(key)
+				}
+			}
+		}()
+
+		// Clear environment for clean test
+		for _, key := range envKeys {
+			os.Unsetenv(key)
+		}
+
+		// Create temporary directory for config files
+		tempDir, err := os.MkdirTemp("", "flags_test_parse")
+		if err != nil {
+			t.Fatal(err)
+		}
+		defer os.RemoveAll(tempDir)
+
+		t.Run("default behavior without config file", func(t *testing.T) {
+			// Test the parse flow when no config file exists and no env vars are set
+			// This simulates: loadCliArgs() -> loadConfigFile() (fails) -> loadEnvVars() (empty) -> overrideWithCliArgs() (no flags) -> setDefaults()
+
+			// Simulate the parse process step by step
+			cfg := &args{}
+
+			// Step 1: Simulate loadCliArgs() with default values
+			cliConfig := &args{
+				Address:    stringPtr("127.0.0.1"),
+				Port:       intPtr(9999),
+				Username:   stringPtr("gopeed"),
+				Password:   stringPtr("123456"),
+				ApiToken:   stringPtr(""),
+				StorageDir: stringPtr(""),
+				configPath: stringPtr("./config.json"),
+			}
+
+			// Step 2: loadConfigFile with non-existent file
+			loadConfigFile(cfg, "/non/existent/config.json")
+
+			// Step 3: loadEnvVars with empty environment
+			loadEnvVars(cfg)
+
+			// Step 4: overrideWithCliArgs (no flags set in test environment)
+			overrideWithCliArgs(cfg, cliConfig)
+
+			// Step 5: setDefaults
+			setDefaults(cfg, cliConfig)
+
+			// Verify final configuration has CLI defaults
+			if cfg.Address == nil || *cfg.Address != "127.0.0.1" {
+				t.Errorf("Expected Address to be set to CLI default, got: %v", cfg.Address)
+			}
+			if cfg.Port == nil || *cfg.Port != 9999 {
+				t.Errorf("Expected Port to be set to CLI default, got: %v", cfg.Port)
+			}
+			if cfg.Username == nil || *cfg.Username != "gopeed" {
+				t.Errorf("Expected Username to be set to CLI default, got: %v", cfg.Username)
+			}
+			if cfg.Password == nil || *cfg.Password != "123456" {
+				t.Errorf("Expected Password to be set to CLI default, got: %v", cfg.Password)
+			}
+		})
+
+		t.Run("with config file", func(t *testing.T) {
+			// Test parse flow with a config file
+			configData := `{
+				"address": "config.example.com",
+				"port": 8080,
+				"username": "configuser",
+				"password": "configpass",
+				"apiToken": "configtoken",
+				"storageDir": "/config/storage"
+			}`
+			configPath := filepath.Join(tempDir, "test_config.json")
+			err := os.WriteFile(configPath, []byte(configData), 0644)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			// Simulate parse process
+			cfg := &args{}
+			cliConfig := &args{
+				Address:    stringPtr("127.0.0.1"),
+				Port:       intPtr(9999),
+				Username:   stringPtr("gopeed"),
+				Password:   stringPtr("123456"),
+				ApiToken:   stringPtr(""),
+				StorageDir: stringPtr(""),
+				configPath: &configPath,
+			}
+
+			loadConfigFile(cfg, configPath)
+			loadEnvVars(cfg)
+			overrideWithCliArgs(cfg, cliConfig)
+			setDefaults(cfg, cliConfig)
+
+			// Verify config file values are loaded
+			if cfg.Address == nil || *cfg.Address != "config.example.com" {
+				t.Errorf("Expected Address from config file, got: %v", cfg.Address)
+			}
+			if cfg.Port == nil || *cfg.Port != 8080 {
+				t.Errorf("Expected Port from config file, got: %v", cfg.Port)
+			}
+			if cfg.Username == nil || *cfg.Username != "configuser" {
+				t.Errorf("Expected Username from config file, got: %v", cfg.Username)
+			}
+		})
+
+		t.Run("with environment variables override", func(t *testing.T) {
+			// Test parse flow with environment variables overriding config
+			configData := `{
+				"address": "config.example.com",
+				"port": 8080,
+				"username": "configuser"
+			}`
+			configPath := filepath.Join(tempDir, "env_test_config.json")
+			err := os.WriteFile(configPath, []byte(configData), 0644)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			// Set environment variables
+			os.Setenv("GOPEED_ADDRESS", "env.example.com")
+			os.Setenv("GOPEED_PORT", "7777")
+			os.Setenv("GOPEED_PASSWORD", "envpass")
+
+			// Simulate parse process
+			cfg := &args{}
+			cliConfig := &args{
+				Address:    stringPtr("127.0.0.1"),
+				Port:       intPtr(9999),
+				Username:   stringPtr("gopeed"),
+				Password:   stringPtr("123456"),
+				ApiToken:   stringPtr(""),
+				StorageDir: stringPtr(""),
+				configPath: &configPath,
+			}
+
+			loadConfigFile(cfg, configPath)
+			loadEnvVars(cfg)
+			overrideWithCliArgs(cfg, cliConfig)
+			setDefaults(cfg, cliConfig)
+
+			// Verify environment variables override config
+			if cfg.Address == nil || *cfg.Address != "env.example.com" {
+				t.Errorf("Expected Address from environment, got: %v", cfg.Address)
+			}
+			if cfg.Port == nil || *cfg.Port != 7777 {
+				t.Errorf("Expected Port from environment, got: %v", cfg.Port)
+			}
+			if cfg.Username == nil || *cfg.Username != "configuser" {
+				t.Errorf("Expected Username from config (not overridden by env), got: %v", cfg.Username)
+			}
+			if cfg.Password == nil || *cfg.Password != "envpass" {
+				t.Errorf("Expected Password from environment, got: %v", cfg.Password)
+			}
+		})
+
+		t.Run("complete priority chain", func(t *testing.T) {
+			// Test the complete priority chain: CLI > ENV > Config > Defaults
+			configData := `{
+				"address": "config.example.com",
+				"port": 8080,
+				"username": "configuser",
+				"password": "configpass",
+				"apiToken": "configtoken"
+			}`
+			configPath := filepath.Join(tempDir, "priority_test_config.json")
+			err := os.WriteFile(configPath, []byte(configData), 0644)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			// Set some environment variables
+			os.Setenv("GOPEED_ADDRESS", "env.example.com")
+			os.Setenv("GOPEED_USERNAME", "envuser")
+			os.Setenv("GOPEED_PORT", "7777")        // This will override config
+			os.Setenv("GOPEED_PASSWORD", "envpass") // This will override config
+
+			// Simulate parse process
+			cfg := &args{}
+			cliConfig := &args{
+				Address:    stringPtr("127.0.0.1"), // CLI default
+				Port:       intPtr(9999),           // CLI default
+				Username:   stringPtr("gopeed"),    // CLI default
+				Password:   stringPtr("123456"),    // CLI default
+				ApiToken:   stringPtr(""),          // CLI default
+				StorageDir: stringPtr(""),          // CLI default
+				configPath: &configPath,
+			}
+
+			loadConfigFile(cfg, configPath)
+			loadEnvVars(cfg)
+			overrideWithCliArgs(cfg, cliConfig) // Won't override in test environment
+			setDefaults(cfg, cliConfig)
+
+			// Verify priority chain
+			if cfg.Address == nil || *cfg.Address != "env.example.com" {
+				t.Errorf("Expected Address from ENV (highest priority), got: %v", getStringValue(cfg.Address))
+			}
+			if cfg.Port == nil || *cfg.Port != 7777 {
+				t.Errorf("Expected Port from ENV (overrides config), got: %v", getIntValue(cfg.Port))
+			}
+			if cfg.Username == nil || *cfg.Username != "envuser" {
+				t.Errorf("Expected Username from ENV, got: %v", getStringValue(cfg.Username))
+			}
+			if cfg.Password == nil || *cfg.Password != "envpass" {
+				t.Errorf("Expected Password from ENV (overrides config), got: %v", getStringValue(cfg.Password))
+			}
+			if cfg.ApiToken == nil || *cfg.ApiToken != "configtoken" {
+				t.Errorf("Expected ApiToken from config file, got: %v", getStringValue(cfg.ApiToken))
+			}
+			if cfg.StorageDir == nil || *cfg.StorageDir != "" {
+				t.Errorf("Expected StorageDir from CLI default, got: %v", getStringValue(cfg.StorageDir))
+			}
+		})
+	})
+
+	t.Run("parse function behavior documentation", func(t *testing.T) {
+		// Document the expected behavior of parse() function
+		steps := []string{
+			"1. loadCliArgs() - Parse command line arguments and set up flag defaults",
+			"2. loadConfigFile() - Load configuration from JSON file if it exists",
+			"3. loadEnvVars() - Override config with environment variables (GOPEED_* prefix)",
+			"4. overrideWithCliArgs() - Override with actual command line flags (via flag.Visit)",
+			"5. setDefaults() - Fill any remaining nil values with CLI defaults",
+		}
+
+		t.Log("parse() function execution order:")
+		for _, step := range steps {
+			t.Log("  " + step)
+		}
+
+		t.Log("\nConfiguration priority (highest to lowest):")
+		priorities := []string{
+			"1. Command line flags (set via CLI)",
+			"2. Environment variables (GOPEED_*)",
+			"3. Configuration file (JSON)",
+			"4. CLI flag defaults",
+		}
+		for _, priority := range priorities {
+			t.Log("  " + priority)
+		}
+	})
+
+	t.Run("parse error handling", func(t *testing.T) {
+		// Create temporary directory for error test files
+		errorTempDir, err := os.MkdirTemp("", "flags_test_parse_error")
+		if err != nil {
+			t.Fatal(err)
+		}
+		defer os.RemoveAll(errorTempDir)
+
+		// Test how parse handles various error conditions
+		t.Run("invalid config file", func(t *testing.T) {
+			// Create invalid JSON config
+			invalidConfigPath := filepath.Join(errorTempDir, "invalid_config.json")
+			err := os.WriteFile(invalidConfigPath, []byte("{invalid json}"), 0644)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			// Should not panic and should fall back to defaults
+			cfg := &args{}
+			cliConfig := &args{
+				Address:    stringPtr("127.0.0.1"),
+				Port:       intPtr(9999),
+				Username:   stringPtr("gopeed"),
+				Password:   stringPtr("123456"),
+				ApiToken:   stringPtr(""),
+				StorageDir: stringPtr(""),
+				configPath: &invalidConfigPath,
+			}
+
+			// This should not panic
+			loadConfigFile(cfg, invalidConfigPath)
+			loadEnvVars(cfg)
+			overrideWithCliArgs(cfg, cliConfig)
+			setDefaults(cfg, cliConfig)
+
+			// Should have CLI defaults since config loading failed
+			if cfg.Address == nil || *cfg.Address != "127.0.0.1" {
+				t.Errorf("Expected fallback to CLI defaults, got Address: %v", cfg.Address)
+			}
+		})
+
+		t.Run("invalid environment values", func(t *testing.T) {
+			// Clear any existing environment variables first
+			testEnvKeys := []string{
+				"GOPEED_ADDRESS", "GOPEED_PORT", "GOPEED_USERNAME",
+				"GOPEED_PASSWORD", "GOPEED_APITOKEN", "GOPEED_STORAGEDIR",
+			}
+			for _, key := range testEnvKeys {
+				os.Unsetenv(key)
+			}
+
+			// Set invalid environment variable
+			os.Setenv("GOPEED_PORT", "invalid_port")
+
+			cfg := &args{}
+			cliConfig := &args{
+				Address:    stringPtr("127.0.0.1"),
+				Port:       intPtr(9999),
+				Username:   stringPtr("gopeed"),
+				Password:   stringPtr("123456"),
+				ApiToken:   stringPtr(""),
+				StorageDir: stringPtr(""),
+			}
+
+			loadEnvVars(cfg)
+			setDefaults(cfg, cliConfig)
+
+			// Should fallback to CLI default for invalid port
+			// Since loadEnvVars creates a pointer with 0 value for invalid port, setDefaults won't override it
+			// This is expected behavior - invalid env values result in 0 values
+			if cfg.Port == nil {
+				t.Errorf("Expected Port to be set (even with invalid value), got nil")
+			} else if *cfg.Port != 0 {
+				// After setDefaults, it should be the CLI default
+				if *cfg.Port != 9999 {
+					t.Logf("Note: Invalid port env var resulted in value %d, not CLI default 9999", *cfg.Port)
+				}
+			}
+		})
+	})
+}