You've already forked panic-cli
mirror of
https://github.com/Print-and-Panic/panic-cli.git
synced 2026-01-21 10:17:41 -08:00
feat(cli): add 'software' command group
This commit is contained in:
35
cmd/software/launch.go
Normal file
35
cmd/software/launch.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package software
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var launchCmd = &cobra.Command{
|
||||
Use: "launch",
|
||||
Short: "Launch a software package",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
fmt.Println("No software specified.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
sw, exists := softwareMap[args[0]]
|
||||
if !exists {
|
||||
fmt.Printf("Software '%s' not found.\n", args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := sw.Launch(); err != nil {
|
||||
fmt.Printf("Failed to launch '%s': %v\n", args[0], err)
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
SoftwareCmd.AddCommand(launchCmd)
|
||||
|
||||
}
|
||||
57
cmd/software/root.go
Normal file
57
cmd/software/root.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package software
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/Print-and-Panic/panic-cli/cmd"
|
||||
"github.com/Print-and-Panic/panic-cli/internal/ai"
|
||||
"github.com/Print-and-Panic/panic-cli/internal/config"
|
||||
"github.com/Print-and-Panic/panic-cli/internal/github"
|
||||
"github.com/Print-and-Panic/panic-cli/internal/software"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/genai"
|
||||
)
|
||||
|
||||
var SoftwareCmd = &cobra.Command{
|
||||
Use: "software",
|
||||
Short: "Software management",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
initSoftwareMap()
|
||||
},
|
||||
}
|
||||
|
||||
var softwareMap map[string]software.Launchable
|
||||
|
||||
func initSoftwareMap() {
|
||||
|
||||
geminiConfig := &genai.GenerateContentConfig{
|
||||
SystemInstruction: genai.NewContentFromText(software.BambuReleaseSummarizer, genai.RoleUser),
|
||||
}
|
||||
geminiProvider, err := ai.NewGeminiProvider(context.Background(), config.AppConfig.AI.Gemini.APIKey, ai.GeminiModel2p5Flash, geminiConfig)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
softwareList := []software.Launchable{
|
||||
&software.BambuStudio{
|
||||
AppImage: software.AppImage{
|
||||
Path: config.AppConfig.Software["bambu"].Path,
|
||||
},
|
||||
GitHubProject: github.GitHubProject{
|
||||
Owner: "bambulab",
|
||||
Repo: "BambuStudio",
|
||||
},
|
||||
GeminiProvider: *geminiProvider,
|
||||
},
|
||||
}
|
||||
|
||||
softwareMap = make(map[string]software.Launchable)
|
||||
for _, s := range softwareList {
|
||||
softwareMap[s.ID()] = s
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmd.RootCmd.AddCommand(SoftwareCmd)
|
||||
}
|
||||
86
cmd/software/update.go
Normal file
86
cmd/software/update.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package software
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/Print-and-Panic/panic-cli/internal/software"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "Update a software package",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if len(args) == 0 {
|
||||
fmt.Println("No software specified.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
sw, exists := softwareMap[args[0]]
|
||||
if !exists {
|
||||
fmt.Printf("Software '%s' not found.\n", args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if updater, ok := sw.(software.Updateable); ok {
|
||||
newestVersion, err := checkForUpdates(updater)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to check for updates: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if newestVersion != "" {
|
||||
if summarizer, ok := sw.(software.Summarizer); ok {
|
||||
notes, err := updater.GetReleaseNotes(newestVersion)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to get release notes: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
summary, err := summarizeReleaseNotes(notes, summarizer)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to summarize release notes: %v\n", err)
|
||||
}
|
||||
fmt.Println(summary)
|
||||
}
|
||||
if err := updater.Install(newestVersion); err != nil {
|
||||
fmt.Printf("Failed to update '%s': %v\n", sw.Name(), err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Software '%s' updated successfully.\n", sw.Name())
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
SoftwareCmd.AddCommand(updateCmd)
|
||||
|
||||
}
|
||||
|
||||
func checkForUpdates(updater software.Updateable) (string, error) {
|
||||
currentVersion, err := updater.GetVersion()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
remoteVersion, err := updater.GetRemoteVersion()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if currentVersion != remoteVersion {
|
||||
return remoteVersion, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func summarizeReleaseNotes(notes string, summarizer software.Summarizer) (string, error) {
|
||||
var summary string
|
||||
buffer := bytes.NewBufferString(summary)
|
||||
summarizer.Summarize(context.Background(), strings.NewReader(notes), buffer)
|
||||
return buffer.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user