feat(cmd/a): implement archive session command

Amolith and Crush created

Co-authored-by: Crush <crush@charm.land>

Change summary

cmd/a.go | 37 +++++++++++++++++++++++++------------
1 file changed, 25 insertions(+), 12 deletions(-)

Detailed changes

cmd/a.go 🔗

@@ -7,6 +7,7 @@ package cmd
 import (
 	"fmt"
 
+	"git.secluded.site/np/cmd/shared"
 	"github.com/spf13/cobra"
 )
 
@@ -14,21 +15,33 @@ var aCmd = &cobra.Command{
 	Use:   "a",
 	Short: "Archive session",
 	Long:  `Archive the current session`,
-	Run: func(cmd *cobra.Command, args []string) {
-		fmt.Println("[STUB] Archive the current session to database")
-	},
+	RunE:  runArchiveSession,
 }
 
 func init() {
 	rootCmd.AddCommand(aCmd)
+}
 
-	// Here you will define your flags and configuration settings.
-
-	// Cobra supports Persistent Flags which will work for this command
-	// and all subcommands, e.g.:
-	// aCmd.PersistentFlags().String("foo", "", "A help for foo")
-
-	// Cobra supports local flags which will only run when this command
-	// is called directly, e.g.:
-	// aCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+func runArchiveSession(cmd *cobra.Command, args []string) error {
+	env, err := shared.Environment(cmd)
+	if err != nil {
+		return err
+	}
+
+	sessionDoc, found, err := shared.ActiveSession(cmd, env)
+	if err != nil {
+		return err
+	}
+	if !found {
+		return nil
+	}
+
+	archived, err := env.SessionStore.Archive(cmd.Context(), sessionDoc.SID)
+	if err != nil {
+		return err
+	}
+
+	out := cmd.OutOrStdout()
+	fmt.Fprintf(out, "Session %s archived.\n", archived.SID)
+	return nil
 }