From 875775e1e4461daed3f4c45f5b48fa0d8dd0f2e1 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 24 May 2024 08:10:04 +0200 Subject: [PATCH] Output the created session on stdout --- cmd/session.go | 8 +++++++- cmd/session/session.go | 41 +++++++++++++++++++++++++++++++---------- pkg/store/file/file.go | 33 ++++++++++++++++++++------------- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/cmd/session.go b/cmd/session.go index 3b30bd8..c3f9f52 100644 --- a/cmd/session.go +++ b/cmd/session.go @@ -42,7 +42,7 @@ func updateSession(cmd *cobra.Command, args []string) { lipgloss.SetColorProfile(termenv.TrueColor) - _, err = tea.NewProgram( + model, err := tea.NewProgram( session.New(path, util.ReadStdin()), tea.WithOutput(os.Stderr), ).Run() @@ -51,4 +51,10 @@ func updateSession(cmd *cobra.Command, args []string) { os.Exit(1) } + result := model.(*session.SessionModel) + + if result.Result != "" { + fmt.Fprintf(os.Stdout, result.Result) + } + } diff --git a/cmd/session/session.go b/cmd/session/session.go index 4179e9d..a49d00c 100644 --- a/cmd/session/session.go +++ b/cmd/session/session.go @@ -56,6 +56,7 @@ type SessionModel struct { // json InputJson string + Result string // session session *models.Session @@ -213,6 +214,19 @@ func (m *SessionModel) View() string { 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 { return func() tea.Msg { if m.scriptFilePath == "" { @@ -226,22 +240,22 @@ func (m *SessionModel) executeScript() tea.Cmd { script, err := os.ReadFile(m.scriptFilePath) if err != nil { - return errorMsg{err} + panic(err) } 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("output", string(sessionJson)) c, err := s.Compile() if err != nil { - return errorMsg{err} + panic(err) } if err := c.Run(); err != nil { - return errorMsg{err} + panic(err) } 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") - _, err := m.store.Create(m.session) + session, err := m.store.Storer.Create(m.session) if err != nil { - return err + return nil, err } - return nil + return session, err } func (m *SessionModel) showErrorOnStatusBar(err error) { @@ -285,6 +299,10 @@ func (m *SessionModel) updateTableContent(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] currentExam := session.Exams[currentToken] @@ -356,7 +374,7 @@ func (m *SessionModel) handleStoreLoaded(msg tea.Msg) tea.Cmd { m.store = storeMsg.store 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 { - err := m.createSession() + var err error + + m.Result, err = m.marshalJSON() if err != nil { panic(err) } + cmds = append(cmds, tea.Quit) } diff --git a/pkg/store/file/file.go b/pkg/store/file/file.go index 76cdca1..57e7ecc 100644 --- a/pkg/store/file/file.go +++ b/pkg/store/file/file.go @@ -79,24 +79,31 @@ func DefaultIndexDirFunc[T FileStorable, K store.Storer[T]](s *FileStore[T, K]) filename := file.Name() fullPath := filepath.Join(s.Dir, filename) - content, err := os.ReadFile(fullPath) + fileInfo, err := os.Stat(fullPath) 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) - if err != nil { - return fmt.Errorf("An error occurred unmarshalling %v: %v", filename, err) + entity := s.CreateEntityFunc() + + 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