Output the created session on stdout

This commit is contained in:
Andrea Fazzi 2024-05-24 08:10:04 +02:00
parent ebd20b53ed
commit 875775e1e4
3 changed files with 58 additions and 24 deletions

View file

@ -42,7 +42,7 @@ func updateSession(cmd *cobra.Command, args []string) {
lipgloss.SetColorProfile(termenv.TrueColor) lipgloss.SetColorProfile(termenv.TrueColor)
_, err = tea.NewProgram( model, err := tea.NewProgram(
session.New(path, util.ReadStdin()), session.New(path, util.ReadStdin()),
tea.WithOutput(os.Stderr), tea.WithOutput(os.Stderr),
).Run() ).Run()
@ -51,4 +51,10 @@ func updateSession(cmd *cobra.Command, args []string) {
os.Exit(1) os.Exit(1)
} }
result := model.(*session.SessionModel)
if result.Result != "" {
fmt.Fprintf(os.Stdout, result.Result)
}
} }

View file

@ -56,6 +56,7 @@ type SessionModel struct {
// json // json
InputJson string InputJson string
Result string
// session // session
session *models.Session session *models.Session
@ -213,6 +214,19 @@ func (m *SessionModel) View() string {
return m.document.View() return m.document.View()
} }
func (m *SessionModel) marshalJSON() (string, error) {
session, err := m.createSession()
if err != nil {
return "", err
}
resultJson, err := json.Marshal(session)
if err != nil {
return "", err
}
return string(resultJson), nil
}
func (m *SessionModel) executeScript() tea.Cmd { func (m *SessionModel) executeScript() tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
if m.scriptFilePath == "" { if m.scriptFilePath == "" {
@ -226,22 +240,22 @@ func (m *SessionModel) executeScript() tea.Cmd {
script, err := os.ReadFile(m.scriptFilePath) script, err := os.ReadFile(m.scriptFilePath)
if err != nil { if err != nil {
return errorMsg{err} panic(err)
} }
s := tengo.NewScript(script) s := tengo.NewScript(script)
s.SetImports(stdlib.GetModuleMap("fmt", "json")) s.SetImports(stdlib.GetModuleMap("fmt", "json", "rand", "times"))
_ = s.Add("input", m.InputJson) _ = s.Add("input", m.InputJson)
_ = s.Add("output", string(sessionJson)) _ = s.Add("output", string(sessionJson))
c, err := s.Compile() c, err := s.Compile()
if err != nil { if err != nil {
return errorMsg{err} panic(err)
} }
if err := c.Run(); err != nil { if err := c.Run(); err != nil {
return errorMsg{err} panic(err)
} }
return scriptExecutedMsg{fmt.Sprintf("%s", c.Get("output"))} return scriptExecutedMsg{fmt.Sprintf("%s", c.Get("output"))}
@ -249,15 +263,15 @@ func (m *SessionModel) executeScript() tea.Cmd {
} }
} }
func (m *SessionModel) createSession() error { func (m *SessionModel) createSession() (*models.Session, error) {
m.session.Title = m.form.GetString("sessionTitle") m.session.Title = m.form.GetString("sessionTitle")
_, err := m.store.Create(m.session) session, err := m.store.Storer.Create(m.session)
if err != nil { if err != nil {
return err return nil, err
} }
return nil return session, err
} }
func (m *SessionModel) showErrorOnStatusBar(err error) { func (m *SessionModel) showErrorOnStatusBar(err error) {
@ -285,6 +299,10 @@ func (m *SessionModel) updateTableContent(session *models.Session) {
} }
func (m *SessionModel) updateViewportContent(session *models.Session) { func (m *SessionModel) updateViewportContent(session *models.Session) {
if len(m.table.Rows()) == 0 {
panic(errors.New("Session have not exams"))
}
currentToken := m.table.SelectedRow()[0] currentToken := m.table.SelectedRow()[0]
currentExam := session.Exams[currentToken] currentExam := session.Exams[currentToken]
@ -356,7 +374,7 @@ func (m *SessionModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
m.store = storeMsg.store m.store = storeMsg.store
m.lenStore = len(m.store.ReadAll()) m.lenStore = len(m.store.ReadAll())
return m.executeScript(m.scriptFilePath) return m.executeScript()
} }
@ -368,10 +386,13 @@ func (m *SessionModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
} }
if m.form.State == huh.StateCompleted { if m.form.State == huh.StateCompleted {
err := m.createSession() var err error
m.Result, err = m.marshalJSON()
if err != nil { if err != nil {
panic(err) panic(err)
} }
cmds = append(cmds, tea.Quit) cmds = append(cmds, tea.Quit)
} }

View file

@ -79,24 +79,31 @@ func DefaultIndexDirFunc[T FileStorable, K store.Storer[T]](s *FileStore[T, K])
filename := file.Name() filename := file.Name()
fullPath := filepath.Join(s.Dir, filename) fullPath := filepath.Join(s.Dir, filename)
content, err := os.ReadFile(fullPath) fileInfo, err := os.Stat(fullPath)
if err != nil { if err != nil {
return err panic(err)
} }
entity := s.CreateEntityFunc() if fileInfo.Size() > 0 {
content, err := os.ReadFile(fullPath)
if err != nil {
return err
}
err = entity.Unmarshal(content) entity := s.CreateEntityFunc()
if err != nil {
return fmt.Errorf("An error occurred unmarshalling %v: %v", filename, err) err = entity.Unmarshal(content)
if err != nil {
return fmt.Errorf("An error occurred unmarshalling %v: %v", filename, err)
}
mEntity, err := s.Create(entity, fullPath)
if err != nil {
return err
}
s.SetPath(mEntity, fullPath)
} }
mEntity, err := s.Create(entity, fullPath)
if err != nil {
return err
}
s.SetPath(mEntity, fullPath)
} }
return nil return nil