probo/cmd/session.go

60 lines
1.2 KiB
Go

/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"os"
"git.andreafazzi.eu/andrea/probo/cmd/session"
"git.andreafazzi.eu/andrea/probo/cmd/util"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)
// sessionCmd represents the session command
var sessionCmd = &cobra.Command{
Use: "session",
Short: "Create a new session or update a given one",
Long: "Create a new session or update a given one.",
Run: updateSession,
}
func init() {
updateCmd.AddCommand(sessionCmd)
sessionCmd.Flags().StringP("script", "s", "", "Execute a tengo script to initiate a session")
}
func updateSession(cmd *cobra.Command, args []string) {
f := util.LogToFile()
if f != nil {
defer f.Close()
}
path, err := cmd.Flags().GetString("script")
if err != nil {
panic(err)
}
lipgloss.SetColorProfile(termenv.TrueColor)
model, err := tea.NewProgram(
session.New(path, util.ReadStdin()),
tea.WithOutput(os.Stderr),
).Run()
if err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
result := model.(*session.SessionModel)
if result.Result != "" {
fmt.Fprintf(os.Stdout, result.Result)
}
}