Skip to content

Commit

Permalink
Merge pull request #43 from kripsy/write-docs
Browse files Browse the repository at this point in the history
Write docs
  • Loading branch information
kripsy authored Nov 13, 2023
2 parents a077d9b + 52e0189 commit 106c58a
Show file tree
Hide file tree
Showing 48 changed files with 521 additions and 90 deletions.
7 changes: 7 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package main is the entry point for the GophKeeper client application.
// It initializes and starts the client-side application,
// handling configuration, logging, and application setup.
package main

import (
Expand All @@ -9,6 +12,10 @@ import (
"github.com/kripsy/GophKeeper/internal/client/log"
)

// main is the entry point of the application. It initializes the
// necessary configurations, logger, and the application context.
// It handles the startup sequence including error handling and
// initiates the application's run process.
func main() {
bi := getBuildInfo()
cfg, err := config.GetConfig()
Expand Down
8 changes: 8 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package main is the entry point for the GophKeeper server application.
// It sets up and starts the server, managing configurations, logging,
// database connections, and server lifecycle.
package main

import (
Expand All @@ -21,6 +24,11 @@ import (
"go.uber.org/zap"
)

// main is the entry point of the GophKeeper server application.
// It initializes the server configuration, logging, database repositories,
// use cases, and gRPC server. It handles the application's lifecycle,
// including graceful shutdown and resource cleanup.
//
//nolint:cyclop
func main() {
grcpPort := ":50051"
Expand Down
13 changes: 11 additions & 2 deletions internal/client/app/about.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// Package app provides core functionalities for the GophKeeper application,
// including build information and utility functions.
package app

import "fmt"

// aboutMsg is a constant template for displaying information about the
// GophKeeper application. It includes placeholders for the build version
// and build date.
const aboutMsg = `
「 GophKeeper 」
This is an Application with the ability to store your secrets locally,
as well as synchronize between your clients when registering through the server.
Build version: %s Build date: %s
`

// BuildInfo holds information about the build version and date.
// It is used to display the current version of the application.
type BuildInfo struct {
BuildVersion string
BuildDate string
BuildVersion string // BuildVersion is the current version of the application.
BuildDate string // BuildDate is the date when the current version was built.
}

// about returns a formatted string containing the application's build version
// and date, providing users with details about the version of GophKeeper they are using.
func about(info BuildInfo) string {
return fmt.Sprintf(aboutMsg, info.BuildVersion, info.BuildDate)
}
10 changes: 10 additions & 0 deletions internal/client/app/app.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package app contains the application logic for the GophKeeper client.
// It includes the main application structure, its initialization, and runtime behavior.
package app

import (
Expand All @@ -9,11 +11,16 @@ import (
"github.com/rs/zerolog"
)

// Application is the main structure of the client application.
// It holds the usecase logic and logger, orchestrating the overall behavior of the client.
type Application struct {
usecase *usecase.ClientUsecase
log zerolog.Logger
}

// NewApplication creates and returns a new Application instance.
// It initializes the application with the provided configuration, build information, and logger.
// Returns an error if the initialization fails.
func NewApplication(cfg config.Config, bi BuildInfo, log zerolog.Logger) (*Application, error) {
return &Application{
usecase: usecase.NewUsecase(
Expand All @@ -28,6 +35,8 @@ func NewApplication(cfg config.Config, bi BuildInfo, log zerolog.Logger) (*Appli
}, nil
}

// PrepareApp prepares the application for running by setting up the user and file manager.
// Returns an error if any of the setup processes fail.
func (a *Application) PrepareApp() error {
if err := a.usecase.SetUser(); err != nil {
return fmt.Errorf("SetUser: %w", err)
Expand All @@ -40,6 +49,7 @@ func (a *Application) PrepareApp() error {
return nil
}

// Run starts the main loop of the application, allowing the user to interact with the CLI interface.
func (a *Application) Run() {
a.usecase.InMenu()
}
14 changes: 10 additions & 4 deletions internal/client/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package config manages the configuration settings for the GophKeeper application.
// It provides functionality to parse and apply configuration from a YAML file
// and command-line flags.
package config

import (
Expand All @@ -22,12 +25,13 @@ const (
//nolint:gochecknoglobals
var flags Flags

// Sync.Once ensures that configuration flags are parsed only once.
// once ensures that configuration flags are parsed only once.
//
//nolint:gochecknoglobals
var once sync.Once

// GetConfig returns the configuration based on the provided flags or defaults.
// GetConfig parses the configuration flags (if not already done) and then loads
// and applies the configuration settings from the YAML file or uses the defaults.
func GetConfig() (Config, error) {
var fileCfg Config

Expand All @@ -54,7 +58,8 @@ func GetConfig() (Config, error) {
return setConfig(fileCfg, flags), nil
}

// setConfig updates the fileCfg with values from flagCfg if flags are provided.
// setConfig updates the fileCfg with values from flagCfg if flags are provided,
// otherwise, it uses default values.
func setConfig(fileCfg Config, flagCfg Flags) Config {
if flagCfg.StoragePath != "" {
fileCfg.StoragePath = flagCfg.StoragePath
Expand All @@ -80,13 +85,14 @@ func setConfig(fileCfg Config, flagCfg Flags) Config {
}

// Config represents the configuration structure with YAML tags for unmarshaling.
// It includes settings for storage paths and server address.
type Config struct {
StoragePath string `yaml:"storage_path"` // Path to store user data and secrets
UploadPath string `yaml:"upload_path"` // Path to upload files from the secrets vault
ServerAddress string `yaml:"server_address"` // Server address for sync
}

// parseConfig reads and parses the YAML configuration file.
// parseConfig reads and parses the YAML configuration file, populating the cfg struct.
func parseConfig(filePath string, cfg any) error {
filename, err := filepath.Abs(filePath)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/client/config/flags.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Package config manages the configuration settings for the GophKeeper application.
// This part includes the definition and parsing of command-line flags.
package config

import "flag"

// Flags defines the structure to hold command-line arguments.
// It includes options for specifying custom paths for storage, upload, server address,
// and the path to the configuration file.
type Flags struct {
StoragePath string
UploadPath string
ServerAddress string
ConfigPath string
}

// parseFlags parses command-line arguments into a Flags struct.
// It uses the flag package to define and parse command-line options, providing
// users with the flexibility to override default configuration settings.
func parseFlags() Flags {
var f Flags
flag.StringVar(&f.ConfigPath, "cfg-path", "",
Expand Down
26 changes: 26 additions & 0 deletions internal/client/grpc/client.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Package grpc provides the implementation of the gRPC client for the GophKeeper application.
// It includes methods for user registration, login, file uploading and downloading, and other
// client-server interactions over gRPC.
//
//nolint:gosec
package grpc

Expand All @@ -16,6 +20,8 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
)

// Client interface defines the set of methods that the gRPC client must implement.
// It includes functionalities for user authentication, file operations, and server availability checks.
type Client interface {
Register(login, password string) error
Login(login, password string) error
Expand All @@ -31,6 +37,8 @@ type Client interface {
TryToConnect() bool
}

// Grpc struct implements the Client interface and provides methods to interact with the gRPC server.
// It manages server communication for user authentication, file operations, and maintaining the connection state.
type Grpc struct {
client pb.GophKeeperServiceClient
token string
Expand All @@ -40,13 +48,17 @@ type Grpc struct {
isAvailable bool
}

// NewClient creates a new Grpc client instance.
// It initializes the gRPC client with the given server address and logger.
func NewClient(serverAddress string, log zerolog.Logger) *Grpc {
return &Grpc{
serverAddress: serverAddress,
log: log,
}
}

// Register handles user registration through the gRPC server.
// It sends a registration request and stores the returned token.
func (c *Grpc) Register(login, password string) error {
resp, err := c.client.Register(context.Background(), &pb.AuthRequest{
Username: login,
Expand All @@ -60,6 +72,8 @@ func (c *Grpc) Register(login, password string) error {
return nil
}

// Login handles user login through the gRPC server.
// It sends a login request and stores the returned token.
func (c *Grpc) Login(login, password string) error {
resp, err := c.client.Login(context.Background(), &pb.AuthRequest{
Username: login,
Expand All @@ -73,6 +87,8 @@ func (c *Grpc) Login(login, password string) error {
return nil
}

// BlockStore initiates a block store operation on the server.
// It sends the sync key and receives a GUID in response.
func (c *Grpc) BlockStore(ctx context.Context, syncKey string, guidChan chan string) error {
stream, err := c.client.BlockStore(c.getCtx(ctx, c.token))
if err != nil {
Expand All @@ -96,6 +112,8 @@ func (c *Grpc) BlockStore(ctx context.Context, syncKey string, guidChan chan str
return nil
}

// DownloadFile handles the file download process from the server.
// It sends a request and streams the file content back to the client.
func (c *Grpc) DownloadFile(ctx context.Context,
fileName string,
fileHash string,
Expand Down Expand Up @@ -132,6 +150,8 @@ func (c *Grpc) DownloadFile(ctx context.Context,
return data, nil
}

// UploadFile handles the file upload process to the server.
// It streams the file content from the client to the server.
func (c *Grpc) UploadFile(ctx context.Context, fileName string, hash string, syncKey string, data chan []byte) error {
stream, err := c.client.MultipartUploadFile(c.getCtx(ctx, c.token))
if err != nil {
Expand Down Expand Up @@ -161,6 +181,7 @@ func (c *Grpc) UploadFile(ctx context.Context, fileName string, hash string, syn
return nil
}

// ApplyChanges sends a request to apply changes on the server for the given ID.
func (c *Grpc) ApplyChanges(ctx context.Context, id string) error {
_, err := c.client.ApplyChanges(c.getCtx(ctx, c.token), &pb.ApplyChangesRequest{Guid: id})
if err != nil {
Expand All @@ -170,14 +191,18 @@ func (c *Grpc) ApplyChanges(ctx context.Context, id string) error {
return nil
}

// IsNotAvailable checks if the server is not available.
func (c *Grpc) IsNotAvailable() bool {
return !c.isAvailable
}

// IsAvailable checks if the server is available.
func (c *Grpc) IsAvailable() bool {
return c.isAvailable
}

// TryToConnect attempts to establish a connection with the gRPC server.
// It sets up the connection and checks the server's availability.
func (c *Grpc) TryToConnect() bool {
conn, err := grpc.Dial(c.serverAddress,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})))
Expand All @@ -202,6 +227,7 @@ func (c *Grpc) TryToConnect() bool {
return true
}

// getCtx generates a new context with metadata containing the JWT for authorization.
func (c *Grpc) getCtx(ctx context.Context, jwt string) context.Context {
md := metadata.New(map[string]string{
"authorization": jwt,
Expand Down
10 changes: 9 additions & 1 deletion internal/client/infrastrucrure/filemanager/data.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Package filemanager provides structures and functionality to manage different types
// of data in the GophKeeper application, including notes, basic authentication data,
// card data, and files. It supports encryption, hashing, and conversion to string formats.
//
//nolint:gochecknoglobals
package filemanager

Expand All @@ -9,6 +13,7 @@ import (
"github.com/kripsy/GophKeeper/internal/utils"
)

// Data type identifiers.
const (
NoteType = iota
BasicAuthType
Expand All @@ -24,7 +29,8 @@ const (
NameFileType = "File"
)

// Data is an interface implemented by various data types to support encryption, hashing, and displaying.
// Data is an interface that defines methods for encryption, hashing, and string representation.
// It is implemented by various data types (Note, BasicAuth, CardData).
type Data interface {
// EncryptedData methods for perform encryption using a provided key.
EncryptedData(key []byte) ([]byte, error)
Expand Down Expand Up @@ -64,6 +70,8 @@ type Note struct {
Text string `json:"text"`
}

// Implementation of String(), EncryptedData(), and GetHash() for each data type.
// These methods provide string representation, encryption, and hashing functionalities.
func (c CardData) String() string {
return fmt.Sprintf("Number: %q, Date: %q, CVV: %q", c.Number, c.Date, c.CVV)
}
Expand Down
Loading

0 comments on commit 106c58a

Please sign in to comment.