From 4ab449a35622b538bc7a51f7189023fd0be2d512 Mon Sep 17 00:00:00 2001 From: Ayoub Faouzi Date: Mon, 3 Aug 2026 14:22:09 +0100 Subject: [PATCH] chore: remove dead code left over from the API-key migration The CLI talks exclusively to the web API, but internal/config still declared storage (S3/MinIO/local) and Couchbase sections that nothing reads, inviting users to put live credentials in a config file for no reason. Remove them along with the never-called users API client (which also hardcoded the production URL), its entity, the unused users endpoint on Service, the unused statusQueued/statusScanning constants, and the env switch in config.Load that was only ever called with the default. --- cmd/root.go | 2 +- cmd/scan.go | 2 - internal/config/config.go | 76 +++----------------------- internal/entity/user.go | 30 ----------- internal/webapi/service.go | 10 ++-- internal/webapi/users.go | 106 ------------------------------------- 6 files changed, 10 insertions(+), 216 deletions(-) delete mode 100644 internal/entity/user.go delete mode 100644 internal/webapi/users.go diff --git a/cmd/root.go b/cmd/root.go index e22f18a..bb6a7bd 100755 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,7 +43,7 @@ For more details see the github repo at https://github.com/saferwall return nil } cfgFilePath := filepath.Join(util.UserHomeDir(), ".config", "saferwall") - if err := config.Load(cfgFilePath, "", &cfg); err != nil { + if err := config.Load(cfgFilePath, &cfg); err != nil { return fmt.Errorf("failed loading CLI config: %v\nRun 'saferwall-cli init' to configure", err) } return nil diff --git a/cmd/scan.go b/cmd/scan.go index 65f4519..a0c29b4 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -17,8 +17,6 @@ import ( ) const ( - statusQueued = 1 - statusScanning = 2 statusCompleted = 3 pollInterval = 5 * time.Second diff --git a/internal/config/config.go b/internal/config/config.go index 3c42030..6d95c49 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -14,91 +14,27 @@ type CredentialsCfg struct { APIKey string `mapstructure:"api_key"` } -// AWSS3Cfg represents AWS S3 credentials. -type AWSS3Cfg struct { - Region string `mapstructure:"region"` - SecretKey string `mapstructure:"secret_key"` - AccessKey string `mapstructure:"access_key"` -} - -// MinIOCfg represents MinIO credentials. -type MinIOCfg struct { - Endpoint string `mapstructure:"endpoint"` - Region string `mapstructure:"region"` - SecretKey string `mapstructure:"secret_key"` - AccessKey string `mapstructure:"access_key"` -} - -// LocalFsCfg represents local file system storage data. -type LocalFsCfg struct { - RootDir string `mapstructure:"root_dir"` -} - -// StorageCfg represents the object storage config. -type StorageCfg struct { - // Deployment kind, possible values: aws, gcp, azure, local. - DeploymentKind string `mapstructure:"deployment_kind"` - SamplesBucket string `mapstructure:"samples_bucket"` - ArtifactsBucket string `mapstructure:"artifacts_bucket"` - S3 AWSS3Cfg `mapstructure:"s3"` - MinIO MinIOCfg `mapstructure:"minio"` - Local LocalFsCfg `mapstructure:"local"` -} - -// DatabaseCfg represents the database config. -type DatabaseCfg struct { - // the data source name (DSN) for connecting to the database. - Server string `mapstructure:"server"` - // Username used to access the db. - Username string `mapstructure:"username"` - // Password used to access the db. - Password string `mapstructure:"password"` - // Name of the couchbase bucket. - BucketName string `mapstructure:"bucket_name"` -} - // Config represents our CLI app config. type Config struct { Credentials CredentialsCfg `mapstructure:"credentials"` - Storage StorageCfg `mapstructure:"storage"` - DB DatabaseCfg `mapstructure:"db"` } // Load returns an application configuration which is populated -// from the given configuration file. -func Load(path, env string, c any) error { +// from the config file in the given directory. +func Load(path string, c any) error { // Adding our TOML config file. viper.AddConfigPath(path) - // Load the config type depending on env variable. - var name string - switch env { - case "local": - name = "local" - case "dev": - name = "dev" - case "prod": - name = "prod" - default: - name = "config" - } - - // Set the config name to choose from the config path + // Set the config name to choose from the config path. // Extension not needed. - viper.SetConfigName(name) + viper.SetConfigName("config") // Load the configuration from disk. - err := viper.ReadInConfig() - if err != nil { + if err := viper.ReadInConfig(); err != nil { return err } // Unmarshal the config into our interface. - err = viper.Unmarshal(&c) - if err != nil { - return err - } - - return err + return viper.Unmarshal(&c) } diff --git a/internal/entity/user.go b/internal/entity/user.go deleted file mode 100644 index a425646..0000000 --- a/internal/entity/user.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018 Saferwall. All rights reserved. -// Use of this source code is governed by Apache v2 license -// license that can be found in the LICENSE file. - -package entity - -// User represent a user. -type User struct { - Type string `json:"type"` - Email string `json:"email,omitempty"` - Username string `json:"username"` - Password string `json:"password,omitempty"` - FullName string `json:"name"` - Location string `json:"location"` - URL string `json:"url"` - Bio string `json:"bio"` - Confirmed bool `json:"confirmed"` - MemberSince int64 `json:"member_since"` - LastSeen int64 `json:"last_seen"` - Admin bool `json:"admin"` - HasAvatar bool `json:"has_avatar"` - Following []string `json:"following"` - FollowingCount int `json:"following_count"` - Followers []string `json:"followers"` - FollowersCount int `json:"followers_count"` - Likes []string `json:"likes"` - LikesCount int `json:"likes_count"` - SubmissionsCount int `json:"submissions_count"` - CommentsCount int `json:"comments_count"` -} diff --git a/internal/webapi/service.go b/internal/webapi/service.go index 022d816..30f9f19 100644 --- a/internal/webapi/service.go +++ b/internal/webapi/service.go @@ -10,7 +10,6 @@ import ( ) const ( - usersEndpoint = "/v1/users/" filesEndpoint = "/v1/files/" defaultTimeout = 5 * time.Minute @@ -18,16 +17,13 @@ const ( type Service struct { filesURL string - usersURL string client *http.Client } // New generates new web apis service object. func New(baseURL string) Service { - s := Service{ - client: &http.Client{Timeout: defaultTimeout}, + return Service{ + client: &http.Client{Timeout: defaultTimeout}, + filesURL: baseURL + filesEndpoint, } - s.usersURL = baseURL + usersEndpoint - s.filesURL = baseURL + filesEndpoint - return s } diff --git a/internal/webapi/users.go b/internal/webapi/users.go deleted file mode 100644 index 533b92f..0000000 --- a/internal/webapi/users.go +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright 2018 Saferwall. All rights reserved. -// Use of this source code is governed by Apache v2 license -// license that can be found in the LICENSE file. - -package webapi - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - - "github.com/saferwall/cli/internal/entity" -) - -const ( - usersURL = "https://api.saferwall.com/v1/users/" -) - -// ListUsers returns the list of users. -func ListUsers(authToken string) ([]entity.User, error) { - - request, err := http.NewRequest("GET", usersURL, nil) - if err != nil { - return nil, err - } - - request.Header.Set("X-Api-Key", authToken) - - // Perform the http post request. - client := &http.Client{} - resp, err := client.Do(request) - if err != nil { - return nil, err - } - - // Read the response. - body := &bytes.Buffer{} - _, err = body.ReadFrom(resp.Body) - if err != nil { - return nil, err - } - - pages := Pages{} - err = json.Unmarshal(body.Bytes(), &pages) - if err != nil { - return nil, err - } - - users := []entity.User{} - for page := 1; page <= pages.PageCount; page++ { - newUsers, err := ListUsersWithIndex(authToken, page, pages.PerPage) - if err != nil { - return nil, err - } - users = append(users, newUsers...) - - } - - return users, nil -} - -// ListUsers returns the list of users given a page and a per-page data. -func ListUsersWithIndex(authToken string, page, perPage int) ([]entity.User, error) { - - url := fmt.Sprintf("%s?page=%d&perPage=%d", usersURL, page, perPage) - request, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - - request.Header.Set("X-Api-Key", authToken) - - // Perform the http post request. - client := &http.Client{} - resp, err := client.Do(request) - if err != nil { - return nil, err - } - - // Read the response. - body := &bytes.Buffer{} - _, err = body.ReadFrom(resp.Body) - if err != nil { - return nil, err - } - - pages := Pages{} - err = json.Unmarshal(body.Bytes(), &pages) - if err != nil { - return nil, err - } - - usersMap := pages.Items.([]any) - - var users []entity.User - for _, u := range usersMap { - var user entity.User - data, _ := json.Marshal(u) - json.Unmarshal(data, &user) - users = append(users, user) - } - - resp.Body.Close() - return users, nil -}