mirror of
https://github.com/GlueOps/autoglue.git
synced 2026-02-13 12:50:05 +01:00
initial rebuild
This commit is contained in:
71
cmd/cli/init-config.go
Normal file
71
cmd/cli/init-config.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/glueops/autoglue/internal/config"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var initConfigCmd = &cobra.Command{
|
||||
Use: "init-config",
|
||||
Short: "Initialize config",
|
||||
Long: "Initialize configuration file",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
file := "config.yaml"
|
||||
if _, err := os.Stat(file); err == nil {
|
||||
fmt.Println("config.yaml already exists")
|
||||
return
|
||||
}
|
||||
|
||||
defaultSecret := config.GenerateSecureSecret()
|
||||
|
||||
defaultConfig := map[string]interface{}{
|
||||
"bind_address": "127.0.0.1",
|
||||
"bind_port": "8080",
|
||||
"database": map[string]string{
|
||||
"dsn": "postgres://user:pass@localhost:5432/db?sslmode=disable",
|
||||
},
|
||||
"authentication": map[string]string{
|
||||
"secret": defaultSecret,
|
||||
},
|
||||
"smtp": map[string]interface{}{
|
||||
"enabled": false,
|
||||
"host": "smtp.example.com",
|
||||
"port": 587,
|
||||
"username": "",
|
||||
"password": "",
|
||||
"from": "no-reply@example.com",
|
||||
},
|
||||
"frontend": map[string]string{
|
||||
"base_url": "http://localhost:5173",
|
||||
},
|
||||
"ui": map[string]string{
|
||||
"dev": "false",
|
||||
},
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(defaultConfig)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshalling YAML:", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.WriteFile(file, data, 0644)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing config.yaml:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("config.yaml written")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(initConfigCmd)
|
||||
|
||||
_ = viper.BindPFlag("database.dsn", rootCmd.PersistentFlags().Lookup("dsn"))
|
||||
_ = viper.BindPFlag("authentication.secret", rootCmd.PersistentFlags().Lookup("authentication-secret"))
|
||||
}
|
||||
34
cmd/cli/root.go
Normal file
34
cmd/cli/root.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "autoglue [command]",
|
||||
Short: "autoglue is used to manage the lifecycle of kubernetes clusters on GlueOps supported cloud providers",
|
||||
Long: "autoglue is used to manage the lifecycle of kubernetes clusters on GlueOps supported cloud providers",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
serveCmd.Run(cmd, []string{})
|
||||
} else {
|
||||
_ = cmd.Help()
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func checkNilErr(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
83
cmd/cli/serve.go
Normal file
83
cmd/cli/serve.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/glueops/autoglue/internal/api"
|
||||
"github.com/glueops/autoglue/internal/db"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
bindPort string
|
||||
bindAddress string
|
||||
)
|
||||
|
||||
var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "Start the server",
|
||||
Long: "Start the server",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db.Connect()
|
||||
|
||||
// Resolve bind address/port from viper (flags/env/config/defaults)
|
||||
addr := fmt.Sprintf("%s:%s", viper.GetString("bind_address"), viper.GetString("bind_port"))
|
||||
|
||||
// Build server (uses Chi router inside)
|
||||
srv := api.NewServer(addr)
|
||||
|
||||
// Start server
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
log.Printf("HTTP server listening on http://%s (ui.dev=%v)", addr, viper.GetBool("ui.dev"))
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
errCh <- err
|
||||
}
|
||||
close(errCh)
|
||||
}()
|
||||
|
||||
// Handle OS signals for graceful shutdown
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
select {
|
||||
case sig := <-stop:
|
||||
log.Printf("Received signal: %s — shutting down...", sig)
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
log.Fatalf("Server error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Printf("Graceful shutdown failed: %v; forcing close", err)
|
||||
_ = srv.Close()
|
||||
} else {
|
||||
log.Println("Server stopped cleanly.")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Flags to override bind address/port
|
||||
serveCmd.Flags().StringVar(&bindAddress, "bind-address", "", "Address to bind the HTTP server (default 127.0.0.1)")
|
||||
serveCmd.Flags().StringVar(&bindPort, "bind-port", "", "Port to bind the HTTP server (default 8080)")
|
||||
|
||||
// Bind flags to viper keys
|
||||
_ = viper.BindPFlag("bind_address", serveCmd.Flags().Lookup("bind-address"))
|
||||
_ = viper.BindPFlag("bind_port", serveCmd.Flags().Lookup("bind-port"))
|
||||
|
||||
// Register command
|
||||
rootCmd.AddCommand(serveCmd)
|
||||
}
|
||||
43
cmd/cli/set-admin.go
Normal file
43
cmd/cli/set-admin.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/glueops/autoglue/internal/db"
|
||||
"github.com/glueops/autoglue/internal/db/models"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
userEmail string
|
||||
)
|
||||
|
||||
var setAdminCmd = &cobra.Command{
|
||||
Use: "set-admin",
|
||||
Short: "Set an existing user to admin role",
|
||||
Long: "Set an existing user to admin role, looked up by email address",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if userEmail == "" {
|
||||
log.Fatal("email is required (use --email)")
|
||||
}
|
||||
|
||||
db.Connect()
|
||||
|
||||
var user models.User
|
||||
if err := db.DB.Where("email = ?", userEmail).First(&user).Error; err != nil {
|
||||
log.Fatalf("could not find user with email %s: %v", userEmail, err)
|
||||
}
|
||||
|
||||
if err := db.DB.Model(&user).Update("role", models.RoleAdmin).Error; err != nil {
|
||||
log.Fatalf("failed to update user role: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("User %s (%s) set to admin role\n", user.Name, user.Email)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
setAdminCmd.Flags().StringVarP(&userEmail, "email", "e", "", "Email of the user to promote to admin")
|
||||
rootCmd.AddCommand(setAdminCmd)
|
||||
}
|
||||
19
cmd/cli/show-config.go
Normal file
19
cmd/cli/show-config.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"github.com/glueops/autoglue/internal/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var showConfigCmd = &cobra.Command{
|
||||
Use: "show-config",
|
||||
Short: "Show the current configuration",
|
||||
Long: "Show the current configuration",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
config.DebugPrintConfig()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(showConfigCmd)
|
||||
}
|
||||
24
cmd/cli/version.go
Normal file
24
cmd/cli/version.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/earthboundkid/versioninfo/v2"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number of the application",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Version:", versioninfo.Version)
|
||||
fmt.Println("Revision:", versioninfo.Revision)
|
||||
fmt.Println("DirtyBuild:", versioninfo.DirtyBuild)
|
||||
fmt.Println("LastCommit:", versioninfo.LastCommit)
|
||||
fmt.Printf("Version: %s\n", versioninfo.Short())
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
}
|
||||
Reference in New Issue
Block a user