Skip to content

Commit 5e217d8

Browse files
committed
fix: resolve golangci-lint issues in cmd/cli
- Fix errcheck errors by handling return values properly - Update octal literals to new style (0o755, 0o600) - Rename stuttering types (*Response -> Response) - Fix unused parameters and shadow variables - Convert if-else chains to switch statements - Improve code formatting and naming conventions
1 parent b4676a4 commit 5e217d8

File tree

12 files changed

+128
-105
lines changed

12 files changed

+128
-105
lines changed

cmd/cli/internal/auth/auth.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func Login(args []string) error {
200200
if err == nil && storedTenantURL != "" {
201201
// Use stored tenant URL
202202
tenantURL = storedTenantURL
203-
//fmt.Printf("Using stored tenant URL: %s\n", tenantURL)
203+
// fmt.Printf("Using stored tenant URL: %s\n", tenantURL)
204204
} else {
205205
// Get tenant URL from command line or prompt
206206
if len(args) > 3 && strings.HasPrefix(args[3], "--tenant=") {
@@ -218,13 +218,13 @@ func Login(args []string) error {
218218

219219
// Get optional session name
220220
sessionName := "reDB CLI"
221-
//var sessionName string
222-
//fmt.Print("Session Name (optional, default: reDB CLI): ")
223-
//sessionName, _ = reader.ReadString('\n')
224-
//sessionName = strings.TrimSpace(sessionName)
225-
//if sessionName == "" {
226-
// sessionName = "reDB CLI"
227-
//}
221+
// var sessionName string
222+
// fmt.Print("Session Name (optional, default: reDB CLI): ")
223+
// sessionName, _ = reader.ReadString('\n')
224+
// sessionName = strings.TrimSpace(sessionName)
225+
// if sessionName == "" {
226+
// sessionName = "reDB CLI"
227+
// }
228228

229229
// Get system information
230230
platform, operatingSystem, deviceType := getSystemInfo()
@@ -260,7 +260,7 @@ func Login(args []string) error {
260260
}
261261

262262
if err := config.StoreToken(username, loginResp.AccessToken); err != nil {
263-
return fmt.Errorf("failed to store access token: %v", err)
263+
return fmt.Errorf("failed to store token: %v", err)
264264
}
265265

266266
if err := config.StoreRefreshToken(username, loginResp.RefreshToken); err != nil {
@@ -276,11 +276,11 @@ func Login(args []string) error {
276276
}
277277

278278
if err := config.StoreTenant(username, tenantURL); err != nil {
279-
return fmt.Errorf("failed to store tenant URL: %v", err)
279+
return fmt.Errorf("failed to store tenant: %v", err)
280280
}
281281

282282
fmt.Printf("Successfully logged in as %s\n", username)
283-
//fmt.Printf("Tenant: %s\n", tenantURL)
283+
// fmt.Printf("Tenant: %s\n", tenantURL)
284284
fmt.Printf("Session: %s (ID: %s)\n", sessionName, loginResp.SessionID)
285285

286286
// Check if workspace is already selected
@@ -332,7 +332,10 @@ func Logout() error {
332332

333333
// Attempt to logout from server (ignore errors as we'll clear local credentials anyway)
334334
var logoutResp LogoutResponse
335-
client.Post(url, logoutReq, &logoutResp, false)
335+
if err := client.Post(url, logoutReq, &logoutResp, false); err != nil {
336+
// Log the error but don't fail the logout process
337+
fmt.Printf("Warning: failed to logout from server: %v\n", err)
338+
}
336339
}
337340
}
338341

@@ -661,14 +664,14 @@ func Status() error {
661664
var accessExpiry, refreshExpiry string
662665
var accessExpiryTime, refreshExpiryTime time.Time
663666

664-
if accessClaims, err := parseJWTToken(accessToken); err == nil {
667+
if accessClaims, parseErr := parseJWTToken(accessToken); parseErr == nil {
665668
accessExpiryTime = time.Unix(accessClaims.Exp, 0)
666669
accessExpiry = accessExpiryTime.Format("2006-01-02 15:04:05 MST")
667670
} else {
668671
accessExpiry = "Unable to parse"
669672
}
670673

671-
if refreshClaims, err := parseJWTToken(refreshToken); err == nil {
674+
if refreshClaims, parseErr := parseJWTToken(refreshToken); parseErr == nil {
672675
refreshExpiryTime = time.Unix(refreshClaims.Exp, 0)
673676
refreshExpiry = refreshExpiryTime.Format("2006-01-02 15:04:05 MST")
674677
} else {

cmd/cli/internal/branches/branches.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type DeleteBranchRequest struct {
5353
}
5454

5555
// parseRepoBranch parses repo/branch format and returns repo and branch names
56-
func parseRepoBranch(repoBranchStr string) (string, string, error) {
56+
func parseRepoBranch(repoBranchStr string) (repoName, branchName string, err error) {
5757
parts := strings.Split(repoBranchStr, "/")
5858
if len(parts) != 2 {
5959
return "", "", fmt.Errorf("invalid format. Expected repo/branch")
@@ -223,7 +223,7 @@ func AttachBranch(repoBranchStr string, args []string) error {
223223
}
224224

225225
// DetachBranch detaches a branch from an attached database
226-
func DetachBranch(repoBranchStr string, args []string) error {
226+
func DetachBranch(repoBranchStr string, _ []string) error {
227227
repoName, branchName, err := parseRepoBranch(repoBranchStr)
228228
if err != nil {
229229
return err

cmd/cli/internal/commits/commits.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type BranchCommitRequest struct {
3030
}
3131

3232
// parseRepoBranchCommit parses repo/branch/commit format and returns repo, branch, and commit names
33-
func parseRepoBranchCommit(repoBranchCommitStr string) (string, string, string, error) {
33+
func parseRepoBranchCommit(repoBranchCommitStr string) (repoName, branchName, commitCode string, err error) {
3434
parts := strings.Split(repoBranchCommitStr, "/")
3535
if len(parts) != 3 {
3636
return "", "", "", fmt.Errorf("invalid format. Expected repo/branch/commit")
@@ -177,7 +177,7 @@ func BranchCommit(repoBranchCommitStr string, args []string) error {
177177
}
178178

179179
// MergeCommit merges a commit to the parent branch
180-
func MergeCommit(repoBranchCommitStr string, args []string) error {
180+
func MergeCommit(repoBranchCommitStr string, _ []string) error {
181181
repoName, branchName, commitCode, err := parseRepoBranchCommit(repoBranchCommitStr)
182182
if err != nil {
183183
return err
@@ -222,7 +222,7 @@ func MergeCommit(repoBranchCommitStr string, args []string) error {
222222
}
223223

224224
// DeployCommit deploys a commit to the database attached to the branch
225-
func DeployCommit(repoBranchCommitStr string, args []string) error {
225+
func DeployCommit(repoBranchCommitStr string, _ []string) error {
226226
repoName, branchName, commitCode, err := parseRepoBranchCommit(repoBranchCommitStr)
227227
if err != nil {
228228
return err

cmd/cli/internal/config/config.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Init(configFile string) error {
5656

5757
// Create config directory if it doesn't exist
5858
configDir := filepath.Dir(configFile)
59-
if err := os.MkdirAll(configDir, 0755); err != nil {
59+
if err := os.MkdirAll(configDir, 0o755); err != nil {
6060
return fmt.Errorf("failed to create config directory: %v", err)
6161
}
6262

@@ -77,7 +77,7 @@ func Init(configFile string) error {
7777
return fmt.Errorf("failed to marshal default config: %v", err)
7878
}
7979

80-
if err := os.WriteFile(configFile, data, 0644); err != nil {
80+
if err := os.WriteFile(configFile, data, 0o600); err != nil {
8181
return fmt.Errorf("failed to write default config file: %v", err)
8282
}
8383
}
@@ -158,13 +158,27 @@ func GetUsername() (string, error) {
158158

159159
func ClearCredentials(username string) error {
160160
// Clear all stored credentials for the user
161-
keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, TokenKey))
162-
keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, RefreshKey))
163-
keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, SessionKey))
164-
keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, HostnameKey))
165-
keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, WorkspaceKey))
166-
keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, TenantKey))
167-
keyringManager.Delete(ServiceName, "current_user")
161+
if err := keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, TokenKey)); err != nil {
162+
return fmt.Errorf("failed to delete token: %v", err)
163+
}
164+
if err := keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, RefreshKey)); err != nil {
165+
return fmt.Errorf("failed to delete refresh token: %v", err)
166+
}
167+
if err := keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, SessionKey)); err != nil {
168+
return fmt.Errorf("failed to delete session: %v", err)
169+
}
170+
if err := keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, HostnameKey)); err != nil {
171+
return fmt.Errorf("failed to delete hostname: %v", err)
172+
}
173+
if err := keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, WorkspaceKey)); err != nil {
174+
return fmt.Errorf("failed to delete workspace: %v", err)
175+
}
176+
if err := keyringManager.Delete(ServiceName, fmt.Sprintf("%s:%s", username, TenantKey)); err != nil {
177+
return fmt.Errorf("failed to delete tenant: %v", err)
178+
}
179+
if err := keyringManager.Delete(ServiceName, "current_user"); err != nil {
180+
return fmt.Errorf("failed to delete current user: %v", err)
181+
}
168182
return nil
169183
}
170184

cmd/cli/internal/databases/databases.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -831,56 +831,57 @@ func ModifyDatabase(databaseName string, args []string) error {
831831

832832
// Parse command line arguments or prompt for input
833833
for _, arg := range args {
834-
if strings.HasPrefix(arg, "--name=") {
834+
switch {
835+
case strings.HasPrefix(arg, "--name="):
835836
updateReq["database_name_new"] = strings.TrimPrefix(arg, "--name=")
836837
hasChanges = true
837-
} else if strings.HasPrefix(arg, "--description=") {
838+
case strings.HasPrefix(arg, "--description="):
838839
updateReq["database_description"] = strings.TrimPrefix(arg, "--description=")
839840
hasChanges = true
840-
} else if strings.HasPrefix(arg, "--type=") {
841+
case strings.HasPrefix(arg, "--type="):
841842
updateReq["database_type"] = strings.TrimPrefix(arg, "--type=")
842843
hasChanges = true
843-
} else if strings.HasPrefix(arg, "--vendor=") {
844+
case strings.HasPrefix(arg, "--vendor="):
844845
updateReq["database_vendor"] = strings.TrimPrefix(arg, "--vendor=")
845846
hasChanges = true
846-
} else if strings.HasPrefix(arg, "--host=") {
847+
case strings.HasPrefix(arg, "--host="):
847848
updateReq["host"] = strings.TrimPrefix(arg, "--host=")
848849
hasChanges = true
849-
} else if strings.HasPrefix(arg, "--port=") {
850+
case strings.HasPrefix(arg, "--port="):
850851
portInt, err := strconv.Atoi(strings.TrimPrefix(arg, "--port="))
851852
if err != nil {
852853
return fmt.Errorf("invalid port. Must be an integer")
853854
}
854855
updateReq["port"] = portInt
855856
hasChanges = true
856-
} else if strings.HasPrefix(arg, "--username=") {
857+
case strings.HasPrefix(arg, "--username="):
857858
updateReq["username"] = strings.TrimPrefix(arg, "--username=")
858859
hasChanges = true
859-
} else if strings.HasPrefix(arg, "--password=") {
860+
case strings.HasPrefix(arg, "--password="):
860861
updateReq["password"] = strings.TrimPrefix(arg, "--password=")
861862
hasChanges = true
862-
} else if strings.HasPrefix(arg, "--db-name=") {
863+
case strings.HasPrefix(arg, "--db-name="):
863864
updateReq["db_name"] = strings.TrimPrefix(arg, "--db-name=")
864865
hasChanges = true
865-
} else if strings.HasPrefix(arg, "--node-id=") {
866+
case strings.HasPrefix(arg, "--node-id="):
866867
updateReq["node_id"] = strings.TrimPrefix(arg, "--node-id=")
867868
hasChanges = true
868-
} else if strings.HasPrefix(arg, "--ssl=") {
869+
case strings.HasPrefix(arg, "--ssl="):
869870
updateReq["ssl"] = strings.TrimPrefix(arg, "--ssl=") == "true"
870871
hasChanges = true
871-
} else if strings.HasPrefix(arg, "--ssl-mode=") {
872+
case strings.HasPrefix(arg, "--ssl-mode="):
872873
updateReq["ssl_mode"] = strings.TrimPrefix(arg, "--ssl-mode=")
873874
hasChanges = true
874-
} else if strings.HasPrefix(arg, "--ssl-cert=") {
875+
case strings.HasPrefix(arg, "--ssl-cert="):
875876
updateReq["ssl_cert"] = strings.TrimPrefix(arg, "--ssl-cert=")
876877
hasChanges = true
877-
} else if strings.HasPrefix(arg, "--ssl-key=") {
878+
case strings.HasPrefix(arg, "--ssl-key="):
878879
updateReq["ssl_key"] = strings.TrimPrefix(arg, "--ssl-key=")
879880
hasChanges = true
880-
} else if strings.HasPrefix(arg, "--ssl-root-cert=") {
881+
case strings.HasPrefix(arg, "--ssl-root-cert="):
881882
updateReq["ssl_root_cert"] = strings.TrimPrefix(arg, "--ssl-root-cert=")
882883
hasChanges = true
883-
} else if strings.HasPrefix(arg, "--environment-id=") {
884+
case strings.HasPrefix(arg, "--environment-id="):
884885
updateReq["environment_id"] = strings.TrimPrefix(arg, "--environment-id=")
885886
hasChanges = true
886887
}
@@ -1072,11 +1073,12 @@ func DeleteDatabase(databaseName string, args []string) error {
10721073
deleteDatabaseObject := false
10731074
deleteRepo := false
10741075
for _, arg := range args {
1075-
if arg == "--force" || arg == "-f" {
1076+
switch {
1077+
case arg == "--force" || arg == "-f":
10761078
force = true
1077-
} else if strings.HasPrefix(arg, "--delete-database-object=") {
1079+
case strings.HasPrefix(arg, "--delete-database-object="):
10781080
deleteDatabaseObject = strings.TrimPrefix(arg, "--delete-database-object=") == "true"
1079-
} else if strings.HasPrefix(arg, "--delete-repo=") {
1081+
case strings.HasPrefix(arg, "--delete-repo="):
10801082
deleteRepo = strings.TrimPrefix(arg, "--delete-repo=") == "true"
10811083
}
10821084
}
@@ -1310,7 +1312,7 @@ func ConnectDatabase(databaseName string, args []string) error {
13101312
return nil
13111313
}
13121314

1313-
func ReconnectDatabase(databaseName string, args []string) error {
1315+
func ReconnectDatabase(databaseName string, _ []string) error {
13141316
databaseName = strings.TrimSpace(databaseName)
13151317
if databaseName == "" {
13161318
return fmt.Errorf("database name is required")
@@ -1355,7 +1357,7 @@ func DisconnectDatabase(databaseName string, args []string) error {
13551357
return DeleteDatabase(databaseName, args)
13561358
}
13571359

1358-
func WipeDatabase(databaseName string, args []string) error {
1360+
func WipeDatabase(databaseName string, _ []string) error {
13591361
databaseName = strings.TrimSpace(databaseName)
13601362
if databaseName == "" {
13611363
return fmt.Errorf("database name is required")
@@ -1394,7 +1396,7 @@ func WipeDatabase(databaseName string, args []string) error {
13941396
return nil
13951397
}
13961398

1397-
func DropDatabase(databaseName string, args []string) error {
1399+
func DropDatabase(databaseName string, _ []string) error {
13981400
databaseName = strings.TrimSpace(databaseName)
13991401
if databaseName == "" {
14001402
return fmt.Errorf("database name is required")
@@ -1433,7 +1435,7 @@ func DropDatabase(databaseName string, args []string) error {
14331435
return nil
14341436
}
14351437

1436-
func CloneTableData(mappingName string, args []string) error {
1438+
func CloneTableData(mappingName string, _ []string) error {
14371439
mappingName = strings.TrimSpace(mappingName)
14381440
if mappingName == "" {
14391441
return fmt.Errorf("mapping name is required")

cmd/cli/internal/environments/environments.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type Environment struct {
3030
Updated string `json:"updated"`
3131
}
3232

33-
// EnvironmentsResponse wraps the API response for listing environments
34-
type EnvironmentsResponse struct {
33+
// Response wraps the API response for listing environments
34+
type Response struct {
3535
Environments []Environment `json:"environments"`
3636
}
3737

@@ -95,7 +95,7 @@ func ListEnvironments() error {
9595
client := httpclient.GetClient()
9696
url := fmt.Sprintf("%s/api/v1/workspaces/%s/environments", tenantURL, workspaceName)
9797

98-
var response EnvironmentsResponse
98+
var response Response
9999
if err := client.Get(url, &response, true); err != nil {
100100
return fmt.Errorf("failed to get environments: %v", err)
101101
}
@@ -327,23 +327,24 @@ func ModifyEnvironment(environmentName string, args []string) error {
327327

328328
// Parse command line arguments or prompt for input
329329
for _, arg := range args {
330-
if strings.HasPrefix(arg, "--name=") {
330+
switch {
331+
case strings.HasPrefix(arg, "--name="):
331332
updateReq.Name = strings.TrimPrefix(arg, "--name=")
332333
hasChanges = true
333-
} else if strings.HasPrefix(arg, "--description=") {
334+
case strings.HasPrefix(arg, "--description="):
334335
updateReq.Description = strings.TrimPrefix(arg, "--description=")
335336
hasChanges = true
336-
} else if strings.HasPrefix(arg, "--production=") {
337+
case strings.HasPrefix(arg, "--production="):
337338
updateReq.Production = strings.TrimPrefix(arg, "--production=") == "true"
338339
hasChanges = true
339-
} else if strings.HasPrefix(arg, "--criticality=") {
340+
case strings.HasPrefix(arg, "--criticality="):
340341
criticalityInt, err := strconv.Atoi(strings.TrimPrefix(arg, "--criticality="))
341342
if err != nil {
342343
return fmt.Errorf("invalid criticality. Must be an integer")
343344
}
344345
updateReq.Criticality = criticalityInt
345346
hasChanges = true
346-
} else if strings.HasPrefix(arg, "--priority=") {
347+
case strings.HasPrefix(arg, "--priority="):
347348
priorityInt, err := strconv.Atoi(strings.TrimPrefix(arg, "--priority="))
348349
if err != nil {
349350
return fmt.Errorf("invalid priority. Must be an integer")

cmd/cli/internal/httpclient/httpclient.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ func (c *HTTPClient) makeRequest(method, url string, body interface{}, requireAu
5959

6060
// Add authentication if required and available
6161
if requireAuth {
62-
username, err := config.GetUsername()
63-
if err != nil {
64-
return nil, fmt.Errorf("authentication required but no user logged in: %v", err)
62+
username, authErr := config.GetUsername()
63+
if authErr != nil {
64+
return nil, fmt.Errorf("authentication required but no user logged in: %v", authErr)
6565
}
6666

67-
token, err := config.GetToken(username)
68-
if err != nil {
69-
return nil, fmt.Errorf("authentication required but no valid token found: %v", err)
67+
token, authErr := config.GetToken(username)
68+
if authErr != nil {
69+
return nil, fmt.Errorf("authentication required but no valid token found: %v", authErr)
7070
}
7171

7272
req.Header.Set("Authorization", "Bearer "+token)
@@ -118,7 +118,7 @@ func (c *HTTPClient) Get(url string, result interface{}, requireAuth bool) error
118118
}
119119

120120
// Post performs a POST request
121-
func (c *HTTPClient) Post(url string, body interface{}, result interface{}, requireAuth bool) error {
121+
func (c *HTTPClient) Post(url string, body, result interface{}, requireAuth bool) error {
122122
resp, err := c.makeRequest("POST", url, body, requireAuth)
123123
if err != nil {
124124
return err
@@ -127,7 +127,7 @@ func (c *HTTPClient) Post(url string, body interface{}, result interface{}, requ
127127
}
128128

129129
// Put performs a PUT request
130-
func (c *HTTPClient) Put(url string, body interface{}, result interface{}, requireAuth bool) error {
130+
func (c *HTTPClient) Put(url string, body, result interface{}, requireAuth bool) error {
131131
resp, err := c.makeRequest("PUT", url, body, requireAuth)
132132
if err != nil {
133133
return err

0 commit comments

Comments
 (0)