Skip to content

Commit

Permalink
agent register
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Jul 30, 2024
1 parent 63ea045 commit def3ad5
Show file tree
Hide file tree
Showing 12 changed files with 841 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ linters:
# not relevant
- varnamelen
- wrapcheck
# - paralleltest
# - exhaustruct
linters-settings:
lll:
line-length: 140
Expand All @@ -33,6 +31,8 @@ issues:
linters:
- gochecknoglobals
- gochecknoinits
- exhaustruct
- mnd
- path: (.+)_test.go
linters:
- dupl
58 changes: 29 additions & 29 deletions cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,46 @@ import (
"github.com/bavix/vakeel/pkg/ctxid"
)

// cfg is the configuration for the Vakeel agent.
//
//nolint:exhaustruct
var cfg *config.Config = &config.Config{}

// agentCmd is the command for the Vakeel agent.
//
//nolint:exhaustruct
var agentCmd = &cobra.Command{
Use: "agent",
Short: "Run the Vakeel agent",
// RunE is the function that will be executed when the agent command is called.
// It creates a new builder with the configuration and calls the AgentApp method of the builder.
// The AgentApp method establishes a connection to the Vakeel server and starts sending update requests.
RunE: func(cmd *cobra.Command, _ []string) error {
// Create a new context with the ID value from the configuration.
ctx := ctxid.WithID(cmd.Context(), cfg.ID)

// Create a new builder with the configuration.
builder := build.New(cfg)

// Call the AgentApp method of the builder and pass the context of the command.
// The AgentApp method returns an error if the connection or the update service call fails.
return builder.AgentApp(builder.Logger(ctx))
},
}

// init registers the agent command to the root command.
//
//nolint:mnd
// The agent command is responsible for running the Vakeel agent. It establishes
// a connection to the Vakeel server and starts sending update requests.
func init() {
rootCmd.AddCommand(agentCmd)
// Create a new configuration object.
cfg := &config.Config{}

// Create a new agent command.
agentCmd := &cobra.Command{
Use: "agent",
Short: "Run the Vakeel agent",
// RunE is the function that will be executed when the agent command is called.
// It creates a new builder with the configuration and calls the AgentApp method of the builder.
// The AgentApp method establishes a connection to the Vakeel server and starts sending update requests.
RunE: func(cmd *cobra.Command, _ []string) error {
// Create a new context with the ID value from the configuration.
ctx := ctxid.WithID(cmd.Context(), cfg.ID)

// Create a new builder with the configuration.
builder := build.New(cfg)

// Call the AgentApp method of the builder and pass the context of the command.
// The AgentApp method returns an error if the connection or the update service call fails.
return builder.AgentApp(builder.Logger(ctx))
},
}

// Set the default value of the host flag to "127.0.0.1".
agentCmd.Flags().
StringVarP(&cfg.Host, "host", "H", "127.0.0.1", "Host for agent, i.e. the IP address of the Vakeel server.")

// Set the default value of the port flag to 4643.
agentCmd.Flags().
IntVarP(&cfg.Port, "port", "p", 4643, "Port for agent, i.e. the port number of the Vakeel server.")

// Set the default value of the id flag to uuid.Nil.String().
agentCmd.Flags().
StringVar(&cfg.ID, "id", uuid.Nil.String(), "ID of agent, i.e. the UUID of the Vakeel agent.")

// Add the agent command to the root command.
rootCmd.AddCommand(agentCmd)
}
45 changes: 45 additions & 0 deletions cmd/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build linux || darwin

package cmd

import (
"github.com/google/uuid"
"github.com/spf13/cobra"

"github.com/bavix/vakeel/internal/build"
"github.com/bavix/vakeel/internal/config"
"github.com/bavix/vakeel/pkg/ctxid"
)

func init() {
// Create a new configuration object.
cfg := &config.Config{}

registerCmd := &cobra.Command{
Use: "register",
Short: "todo short",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := ctxid.WithID(cmd.Context(), cfg.ID)

builder := build.New(cfg)

return builder.AgentRegisterApp(builder.Logger(ctx))
},
}

// Set the default value of the host flag to "127.0.0.1".
registerCmd.Flags().
StringVarP(&cfg.Host, "host", "H", "127.0.0.1", "Host for agent, i.e. the IP address of the Vakeel server.")
// Set the default value of the port flag to 4643.
registerCmd.Flags().
IntVarP(&cfg.Port, "port", "p", 4643, "Port for agent, i.e. the port number of the Vakeel server.")
// Set the default value of the id flag to a new UUID.
// The flag is used to set the ID of the agent, i.e. the UUID of the Vakeel agent.
// The UUID is generated using uuid.New() and converted to a string using uuid.String().
registerCmd.Flags().
StringVar(&cfg.ID, "id", uuid.New().String(), "ID of agent, i.e. the UUID of the Vakeel agent."+
"If not provided, a new UUID will be generated.")

rootCmd.AddCommand(registerCmd)
}
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
)

//nolint:exhaustruct
var rootCmd = &cobra.Command{
Use: "vakeel",
Short: "Agent for vakeel-way",
Expand Down
16 changes: 16 additions & 0 deletions internal/app/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package app

import (
"context"
)

type RegisterUseCase interface {
Register() error
}

func AgentRegister(
_ context.Context,
register RegisterUseCase,
) error {
return register.Register()
}
32 changes: 32 additions & 0 deletions internal/build/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/bavix/vakeel-way/pkg/api/vakeel_way"
"github.com/bavix/vakeel/internal/app"
"github.com/bavix/vakeel/internal/infra/templater"
"github.com/bavix/vakeel/pkg/ctxid"
)

// AgentApp creates a gRPC client and connects to the server's update service.
Expand All @@ -34,3 +36,33 @@ func (b *Builder) AgentApp(ctx context.Context) error {

return app.Agent(ctx, serviceClient)
}

// AgentRegisterApp is a method of the Builder struct.
//
// It registers the agent application with the server.
// It returns an error if the registration fails.
//
// ctx: The context.Context to use for the gRPC call.
//
// Returns:
// An error if the registration fails.
func (b *Builder) AgentRegisterApp(ctx context.Context) error {
// Create a new templater.New instance with the context ID, host, and port from the config.
// The templater.New instance generates the stub agent template.
generate, err := templater.New(ctxid.ID(ctx), b.config.Host, b.config.Port)
if err != nil {
return err
}

// Call the AgentRegister function of the app package.
// It creates a gRPC client insecure connection to the server.
// It registers the agent application with the server.
// It returns an error if the registration fails.
//
// ctx: The context.Context to use for the gRPC call.
// generate: The templater.New instance that generates the stub agent template.
//
// Returns:
// An error if the registration fails.
return app.AgentRegister(ctx, generate)
}
Loading

0 comments on commit def3ad5

Please sign in to comment.