edge-connect-client/internal/adapters/driving/cli/root.go
Stephan Lo 3e432aa8db refactor(arch): Relocate domain and ports packages
Moves the `domain` and `ports` packages from `internal/core` to `internal`.

This refactoring simplifies the directory structure by elevating the core architectural concepts of domain and ports to the top level of the `internal` directory. The `core` directory is now removed as its only purpose was to house these two packages.

All import paths across the project have been updated to reflect this change.
2025-10-09 01:16:31 +02:00

111 lines
No EOL
3.5 KiB
Go

package cli
import (
"fmt"
"os"
"edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/ports/driving"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
baseURL string
username string
password string
// Services injected via constructor - no global state
services *ServiceContainer
)
// ServiceContainer holds injected services (simple struct - no complex container)
type ServiceContainer struct {
AppService driving.AppService
InstanceService driving.AppInstanceService
CloudletService driving.CloudletService
OrganizationService driving.OrganizationService
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "edge-connect",
Short: "A CLI tool for managing Edge Connect applications",
Long: `edge-connect is a command line interface for managing Edge Connect applications
and their instances. It provides functionality to create, show, list, and delete
applications and application instances.`,
}
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// ExecuteWithServices executes CLI with dependency-injected services (simple parameter passing)
func ExecuteWithServices(appSvc driving.AppService, instanceSvc driving.AppInstanceService, cloudletSvc driving.CloudletService, orgSvc driving.OrganizationService) {
// Simple dependency injection - just store services in container
services = &ServiceContainer{
AppService: appSvc,
InstanceService: instanceSvc,
CloudletService: cloudletSvc,
OrganizationService: orgSvc,
}
Execute()
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(organizationCmd)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.edge-connect.yaml)")
rootCmd.PersistentFlags().StringVar(&baseURL, "base-url", "", "base URL for the Edge Connect API")
rootCmd.PersistentFlags().StringVar(&username, "username", "", "username for authentication")
rootCmd.PersistentFlags().StringVar(&password, "password", "", "password for authentication")
if err := viper.BindPFlag("base_url", rootCmd.PersistentFlags().Lookup("base-url")); err != nil {
panic(fmt.Sprintf("Failed to bind base-url flag: %v", err))
}
if err := viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username")); err != nil {
panic(fmt.Sprintf("Failed to bind username flag: %v", err))
}
if err := viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password")); err != nil {
panic(fmt.Sprintf("Failed to bind password flag: %v", err))
}
}
func initConfig() {
viper.AutomaticEnv()
viper.SetEnvPrefix("EDGE_CONNECT")
if err := viper.BindEnv("base_url", "EDGE_CONNECT_BASE_URL"); err != nil {
panic(fmt.Sprintf("Failed to bind base_url environment variable: %v", err))
}
if err := viper.BindEnv("username", "EDGE_CONNECT_USERNAME"); err != nil {
panic(fmt.Sprintf("Failed to bind username environment variable: %v", err))
}
if err := viper.BindEnv("password", "EDGE_CONNECT_PASSWORD"); err != nil {
panic(fmt.Sprintf("Failed to bind password environment variable: %v", err))
}
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".edge-connect")
}
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}