edge-connect-client/cmd/app.go
Daniel Sy c539eb2210
feat(cli): Implement Edge Connect CLI tool
Creates a new command-line interface for managing Edge Connect applications and instances with the following features:

- Configuration management via YAML files and environment variables
- Application lifecycle commands (create, show, list, delete)
- Instance management with cloudlet support
- Improved error handling and authentication flow
- Comprehensive documentation with usage examples

The CLI provides a user-friendly interface for managing Edge Connect resources while following best practices for command-line tool development using Cobra and Viper.
2025-09-18 13:51:09 +02:00

145 lines
3.5 KiB
Go

package cmd
import (
"context"
"fmt"
"net/http"
"os"
"edp.buildth.ing/DevFW-CICD/edge-connect-client/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
organization string
appName string
appVersion string
region string
)
func newClient() *client.EdgeConnect {
return &client.EdgeConnect{
BaseURL: viper.GetString("base_url"),
HttpClient: &http.Client{},
Credentials: client.Credentials{
Username: viper.GetString("username"),
Password: viper.GetString("password"),
},
}
}
var appCmd = &cobra.Command{
Use: "app",
Short: "Manage Edge Connect applications",
Long: `Create, show, list, and delete Edge Connect applications.`,
}
var createAppCmd = &cobra.Command{
Use: "create",
Short: "Create a new Edge Connect application",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
input := client.NewAppInput{
Region: region,
App: client.App{
Key: client.AppKey{
Organization: organization,
Name: appName,
Version: appVersion,
},
},
}
err := c.CreateApp(context.Background(), input)
if err != nil {
fmt.Printf("Error creating app: %v\n", err)
os.Exit(1)
}
fmt.Println("Application created successfully")
},
}
var showAppCmd = &cobra.Command{
Use: "show",
Short: "Show details of an Edge Connect application",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
appKey := client.AppKey{
Organization: organization,
Name: appName,
Version: appVersion,
}
app, err := c.ShowApp(context.Background(), appKey, region)
if err != nil {
fmt.Printf("Error showing app: %v\n", err)
os.Exit(1)
}
fmt.Printf("Application details:\n%+v\n", app)
},
}
var listAppsCmd = &cobra.Command{
Use: "list",
Short: "List Edge Connect applications",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
appKey := client.AppKey{
Organization: organization,
Name: appName,
Version: appVersion,
}
apps, err := c.ShowApps(context.Background(), appKey, region)
if err != nil {
fmt.Printf("Error listing apps: %v\n", err)
os.Exit(1)
}
fmt.Println("Applications:")
for _, app := range apps {
fmt.Printf("%+v\n", app)
}
},
}
var deleteAppCmd = &cobra.Command{
Use: "delete",
Short: "Delete an Edge Connect application",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
appKey := client.AppKey{
Organization: organization,
Name: appName,
Version: appVersion,
}
err := c.DeleteApp(context.Background(), appKey, region)
if err != nil {
fmt.Printf("Error deleting app: %v\n", err)
os.Exit(1)
}
fmt.Println("Application deleted successfully")
},
}
func init() {
rootCmd.AddCommand(appCmd)
appCmd.AddCommand(createAppCmd, showAppCmd, listAppsCmd, deleteAppCmd)
// Add common flags to all app commands
appCmds := []*cobra.Command{createAppCmd, showAppCmd, listAppsCmd, deleteAppCmd}
for _, cmd := range appCmds {
cmd.Flags().StringVarP(&organization, "org", "o", "", "organization name (required)")
cmd.Flags().StringVarP(&appName, "name", "n", "", "application name")
cmd.Flags().StringVarP(&appVersion, "version", "v", "", "application version")
cmd.Flags().StringVarP(&region, "region", "r", "", "region (required)")
cmd.MarkFlagRequired("org")
cmd.MarkFlagRequired("region")
}
// Add required name flag for specific commands
for _, cmd := range []*cobra.Command{createAppCmd, showAppCmd, deleteAppCmd} {
cmd.MarkFlagRequired("name")
}
}