infctl-cli/main.go

115 lines
2.9 KiB
Go

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"fmt"
"log"
"log/slog"
"os"
"headshed/infctl-cli/app"
"headshed/infctl-cli/config"
)
type customMessageOnlyHandler struct {
output *os.File
}
func (h *customMessageOnlyHandler) Enabled(_ context.Context, _ slog.Level) bool {
return true
}
func (h *customMessageOnlyHandler) Handle(_ context.Context, r slog.Record) error {
// Directly retrieve the message from the record
msg := r.Message
if msg != "" {
_, err := fmt.Fprintln(h.output, msg)
return err
}
return nil
}
func (h *customMessageOnlyHandler) WithAttrs(_ []slog.Attr) slog.Handler {
return h
}
func (h *customMessageOnlyHandler) WithGroup(_ string) slog.Handler {
return h
}
func main() {
var levelVar slog.LevelVar
levelVar.Set(slog.LevelDebug)
var logger *slog.Logger
if os.Getenv("LOG_FORMAT") == "basic" {
logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}))
} else if os.Getenv("LOG_FORMAT") == "none" {
logger = slog.New(&customMessageOnlyHandler{output: os.Stdout})
} else {
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: &levelVar}))
}
slog.SetDefault(logger)
if err := run(); err != nil {
log.Fatalf("Application error: %v", err)
}
}
func run() error {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
baseConfigPath := wd + string(os.PathSeparator) + "base.json"
configPath := wd + string(os.PathSeparator) + "config.json"
baseConfig, err := config.ReadBaseConfig(baseConfigPath)
if err != nil {
return fmt.Errorf("error reading base config file: %w", err)
}
customerConfig, err := config.ReadCustomerConfig(configPath)
if err != nil {
return fmt.Errorf("error reading customer config file: %w", err)
}
appState, err := app.NewAppState(customerConfig, baseConfig, "app.db")
if err != nil {
return fmt.Errorf("failed to initialize app state: %w", err)
}
defer func() {
if err := appState.DB.Close(); err != nil {
log.Printf("Error closing database: %v", err)
}
}()
if err := appState.CreatePipeline(); err != nil {
return fmt.Errorf("failed to create customer project: %w", err)
}
return nil
}