s.go

  1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  2//
  3// SPDX-License-Identifier: AGPL-3.0-or-later
  4
  5package g
  6
  7import (
  8	"fmt"
  9	"strings"
 10
 11	"git.secluded.site/np/cmd/shared"
 12	"git.secluded.site/np/internal/db"
 13	"git.secluded.site/np/internal/event"
 14	"git.secluded.site/np/internal/goal"
 15	"github.com/spf13/cobra"
 16)
 17
 18var sCmd = &cobra.Command{
 19	Use:   "s",
 20	Short: "Set goal",
 21	Long:  `Set the session goal with title and description`,
 22	RunE:  runSetGoal,
 23}
 24
 25func init() {
 26	GCmd.AddCommand(sCmd)
 27
 28	sCmd.Flags().StringP("title", "t", "", "Goal title (required)")
 29	sCmd.Flags().StringP("description", "d", "", "Goal description (required)")
 30	_ = sCmd.MarkFlagRequired("title")
 31	_ = sCmd.MarkFlagRequired("description")
 32}
 33
 34func runSetGoal(cmd *cobra.Command, _ []string) error {
 35	env, err := shared.Environment(cmd)
 36	if err != nil {
 37		return err
 38	}
 39
 40	sessionDoc, found, err := shared.ActiveSession(cmd, env)
 41	if err != nil {
 42		return err
 43	}
 44	if !found {
 45		return nil
 46	}
 47
 48	title, err := cmd.Flags().GetString("title")
 49	if err != nil {
 50		return err
 51	}
 52	description, err := cmd.Flags().GetString("description")
 53	if err != nil {
 54		return err
 55	}
 56
 57	title = strings.TrimSpace(title)
 58	description = strings.TrimSpace(description)
 59
 60	if title == "" {
 61		_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Goal title is required.")
 62		return nil
 63	}
 64	if description == "" {
 65		_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Goal description is required.")
 66		return nil
 67	}
 68
 69	exists, err := env.GoalStore.Exists(cmd.Context(), sessionDoc.SID)
 70	if err != nil {
 71		return err
 72	}
 73	if exists {
 74		_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Goal already set. Use 'np g u' to update it (requires -r/--reason flag).")
 75		return nil
 76	}
 77
 78	var saved goal.Document
 79	err = env.DB.Update(cmd.Context(), func(txn *db.Txn) error {
 80		goalTxn := env.GoalStore.WithTxn(txn)
 81		var err error
 82		saved, err = goalTxn.Set(sessionDoc.SID, title, description)
 83		if err != nil {
 84			return err
 85		}
 86
 87		sessionTxn := env.SessionStore.WithTxn(txn)
 88		if _, err := sessionTxn.TouchAt(sessionDoc.SID, saved.UpdatedAt); err != nil {
 89			return err
 90		}
 91
 92		eventTxn := env.EventStore.WithTxn(txn)
 93		_, err = eventTxn.Append(sessionDoc.SID, event.BuildGoalSet(shared.CommandString(), "", saved))
 94		return err
 95	})
 96	if err != nil {
 97		return err
 98	}
 99
100	if _, err := shared.PrintPlan(cmd, env, sessionDoc.SID); err != nil {
101		return err
102	}
103
104	out := cmd.OutOrStdout()
105	_, _ = fmt.Fprintln(out, "")
106	_, _ = fmt.Fprintln(out, "Study everything above carefully, the reference content, the source code, the documentation, etc. Once you've a solid understanding of how to approach resolving the request, fill out your task list. Prefer adding/updating in batch.")
107
108	return nil
109}