fabric/cli/cli.go

238 lines
5.7 KiB
Go
Raw Normal View History

2024-08-16 15:43:27 -04:00
package cli
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
2024-08-16 15:43:27 -04:00
"github.com/danielmiessler/fabric/core"
"github.com/danielmiessler/fabric/db"
)
// Cli Controls the cli. It takes in the flags and runs the appropriate functions
2024-09-26 20:57:37 +02:00
func Cli(version string) (message string, err error) {
2024-08-16 15:43:27 -04:00
var currentFlags *Flags
if currentFlags, err = Init(); err != nil {
return
}
2024-09-26 20:57:37 +02:00
if currentFlags.Version {
fmt.Println(version)
return
}
2024-08-16 15:43:27 -04:00
var homedir string
if homedir, err = os.UserHomeDir(); err != nil {
return
}
2024-08-22 21:45:36 +02:00
fabricDb := db.NewDb(filepath.Join(homedir, ".config/fabric"))
2024-08-16 15:43:27 -04:00
// if the setup flag is set, run the setup function
if currentFlags.Setup {
2024-08-22 21:45:36 +02:00
_ = fabricDb.Configure()
_, err = Setup(fabricDb, currentFlags.SetupSkipUpdatePatterns)
2024-08-16 15:43:27 -04:00
return
}
var fabric *core.Fabric
2024-08-22 21:45:36 +02:00
if err = fabricDb.Configure(); err != nil {
2024-08-16 15:43:27 -04:00
fmt.Println("init is failed, run start the setup procedure", err)
2024-08-22 21:45:36 +02:00
if fabric, err = Setup(fabricDb, currentFlags.SetupSkipUpdatePatterns); err != nil {
2024-08-16 15:43:27 -04:00
return
}
} else {
2024-08-22 21:45:36 +02:00
if fabric, err = core.NewFabric(fabricDb); err != nil {
2024-08-16 15:43:27 -04:00
fmt.Println("fabric can't initialize, please run the --setup procedure", err)
return
}
}
// if the update patterns flag is set, run the update patterns function
if currentFlags.UpdatePatterns {
err = fabric.PopulateDB()
return
}
if currentFlags.ChangeDefaultModel {
err = fabric.SetupDefaultModel()
return
}
// if the latest patterns flag is set, run the latest patterns function
if currentFlags.LatestPatterns != "0" {
var parsedToInt int
if parsedToInt, err = strconv.Atoi(currentFlags.LatestPatterns); err != nil {
return
}
2024-08-22 21:45:36 +02:00
if err = fabricDb.Patterns.PrintLatestPatterns(parsedToInt); err != nil {
2024-08-16 15:43:27 -04:00
return
}
return
}
// if the list patterns flag is set, run the list all patterns function
if currentFlags.ListPatterns {
2024-08-22 21:45:36 +02:00
err = fabricDb.Patterns.ListNames()
2024-08-16 15:43:27 -04:00
return
}
// if the list all models flag is set, run the list all models function
if currentFlags.ListAllModels {
fabric.GetModels().Print()
return
}
// if the list all contexts flag is set, run the list all contexts function
if currentFlags.ListAllContexts {
2024-08-22 21:45:36 +02:00
err = fabricDb.Contexts.ListNames()
2024-08-16 15:43:27 -04:00
return
}
// if the list all sessions flag is set, run the list all sessions function
if currentFlags.ListAllSessions {
2024-08-22 21:45:36 +02:00
err = fabricDb.Sessions.ListNames()
2024-08-16 15:43:27 -04:00
return
}
2024-09-27 07:43:08 +08:00
// if the wipe context flag is set, run the wipe context function
if currentFlags.WipeContext != "" {
err = fabricDb.Contexts.Delete(currentFlags.WipeContext)
return
}
// if the wipe session flag is set, run the wipe session function
if currentFlags.WipeSession != "" {
err = fabricDb.Sessions.Delete(currentFlags.WipeSession)
return
}
2024-08-16 15:43:27 -04:00
// if the interactive flag is set, run the interactive function
// if currentFlags.Interactive {
// interactive.Interactive()
// }
// if none of the above currentFlags are set, run the initiate chat function
if currentFlags.YouTube != "" {
if fabric.YouTube.IsConfigured() == false {
err = fmt.Errorf("YouTube is not configured, please run the setup procedure")
return
}
var videoId string
if videoId, err = fabric.YouTube.GetVideoId(currentFlags.YouTube); err != nil {
return
}
if !currentFlags.YouTubeComments || currentFlags.YouTubeTranscript {
var transcript string
var language = "en"
if currentFlags.Language != "" {
language = currentFlags.Language
}
if transcript, err = fabric.YouTube.GrabTranscript(videoId, language); err != nil {
return
}
// fmt.Println(transcript)
2024-09-16 22:06:32 +02:00
currentFlags.AppendMessage(transcript)
}
if currentFlags.YouTubeComments {
var comments []string
if comments, err = fabric.YouTube.GrabComments(videoId); err != nil {
return
}
commentsString := strings.Join(comments, "\n")
// fmt.Println(commentsString)
2024-09-16 22:06:32 +02:00
currentFlags.AppendMessage(commentsString)
}
if currentFlags.Pattern == "" {
// if the pattern flag is not set, we wanted only to grab the transcript or comments
fmt.Println(currentFlags.Message)
return
}
}
2024-09-16 22:06:32 +02:00
if (currentFlags.ScrapeURL != "" || currentFlags.ScrapeQuestion != "") && fabric.Jina.IsConfigured() {
// Check if the scrape_url flag is set and call ScrapeURL
if currentFlags.ScrapeURL != "" {
if message, err = fabric.Jina.ScrapeURL(currentFlags.ScrapeURL); err != nil {
return
}
//fmt.Println(message)
2024-09-16 22:06:32 +02:00
currentFlags.AppendMessage(message)
}
// Check if the scrape_question flag is set and call ScrapeQuestion
if currentFlags.ScrapeQuestion != "" {
if message, err = fabric.Jina.ScrapeQuestion(currentFlags.ScrapeQuestion); err != nil {
return
}
//fmt.Println(message)
2024-09-16 22:06:32 +02:00
currentFlags.AppendMessage(message)
}
if currentFlags.Pattern == "" {
// if the pattern flag is not set, we wanted only to grab the url or get the answer to the question
fmt.Println(currentFlags.Message)
2024-09-16 22:06:32 +02:00
return
}
}
2024-08-16 15:43:27 -04:00
var chatter *core.Chatter
2024-09-01 13:44:56 +07:00
if chatter, err = fabric.GetChatter(currentFlags.Model, currentFlags.Stream, currentFlags.DryRun); err != nil {
2024-08-16 15:43:27 -04:00
return
}
if message, err = chatter.Send(currentFlags.BuildChatRequest(), currentFlags.BuildChatOptions()); err != nil {
return
}
if !currentFlags.Stream {
fmt.Println(message)
}
// if the copy flag is set, copy the message to the clipboard
if currentFlags.Copy {
if err = fabric.CopyToClipboard(message); err != nil {
return
}
}
// if the output flag is set, create an output file
if currentFlags.Output != "" {
err = fabric.CreateOutputFile(message, currentFlags.Output)
}
return
}
func Setup(db *db.Db, skipUpdatePatterns bool) (ret *core.Fabric, err error) {
2024-08-22 21:45:36 +02:00
instance := core.NewFabricForSetup(db)
2024-08-16 15:43:27 -04:00
2024-08-22 21:45:36 +02:00
if err = instance.Setup(); err != nil {
2024-08-16 15:43:27 -04:00
return
}
if !skipUpdatePatterns {
2024-08-22 21:45:36 +02:00
if err = instance.PopulateDB(); err != nil {
2024-08-16 15:43:27 -04:00
return
}
}
2024-08-22 21:45:36 +02:00
ret = instance
2024-08-16 15:43:27 -04:00
return
}