mirror of
https://github.com/catppuccin/cli.git
synced 2026-02-05 20:17:58 +08:00
Co-authored-by: bashbunni <bashbunni.io@gmail.com> Co-authored-by: ghostx31 <68803793+ghostx31@users.noreply.github.com> Co-authored-by: Dukk <48651837+DakshG07@users.noreply.github.com> Co-authored-by: jolheiser <john.olheiser@gmail.com>
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
/*
|
|
NETWORK.GO
|
|
Contains bigger functions
|
|
that have to do with Git
|
|
or networking.
|
|
*/
|
|
package utils
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path"
|
|
|
|
// "path/filepath"
|
|
|
|
"github.com/caarlos0/log"
|
|
"github.com/catppuccin/cli/internal/pkg/structs"
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/google/go-github/v47/github"
|
|
)
|
|
|
|
// PullUpdates opens a git repo and pulls the latest changes.
|
|
func PullUpdates(repo string) {
|
|
// Repo should be a valid folder, so now we'll open the .git
|
|
r, err := git.PlainOpen(repo) // Open new repo targeting the .git folder
|
|
if err != nil {
|
|
// fmt.Printf("Error opening repo folder: %s\n", err)
|
|
log.Errorf("Error opening repo folder.")
|
|
} else {
|
|
// Get working directory
|
|
w, err := r.Worktree()
|
|
if err != nil {
|
|
// fmt.Printf("Error getting working directory: %s\n", err)
|
|
log.Fatalf("Error getting working directory")
|
|
} else {
|
|
// Pull the latest changes from origin
|
|
// fmt.Printf("Pulling latest changes for %s...\n", repo)
|
|
log.Infof("Pulling latest changes for %s...", repo)
|
|
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
|
|
if err != nil {
|
|
// fmt.Printf("Failed to pull updates: %s\n", err)
|
|
log.Fatalf("Failed to pull updates")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// UpdateJSON makes a search request for all Catppuccin repos and caches them.
|
|
func UpdateJSON() {
|
|
dir := path.Join(ShareDir(), "repos.json") // Set the staging directory plus the file name
|
|
org := GetEnv("ORG_OVERRIDE", "catppuccin")
|
|
client := github.NewClient(nil)
|
|
|
|
// Get all the Catppuccin repositories
|
|
opt := &github.RepositoryListByOrgOptions{Type: "public"} // Get all the repositories
|
|
repos, _, err := client.Repositories.ListByOrg(context.Background(), org, opt)
|
|
|
|
// Handle errors
|
|
if err != nil {
|
|
// fmt.Println("Failed to get repositories.")
|
|
log.Error("Failed to get repositories")
|
|
} else {
|
|
log.Info("Received repositories. Caching!")
|
|
themes := []structs.SearchEntry{}
|
|
for i := 0; i < len(repos); i++ {
|
|
repo := repos[i]
|
|
if !ListContains(repo.Topics, "catppuccin-meta") { // Repo does not contain catppuccin-meta topic
|
|
// Append search result
|
|
theme := structs.SearchEntry{
|
|
Name: repo.GetName(),
|
|
Stars: repo.GetStargazersCount(),
|
|
Topics: repo.Topics,
|
|
}
|
|
themes = append(themes, theme)
|
|
}
|
|
}
|
|
body, err := json.Marshal(themes)
|
|
if err != nil {
|
|
// fmt.Printf("Failed to marshal cache: %s\nPlease try again.\n", err)
|
|
log.Error("Failed to marshal cache. Please try again!")
|
|
} else {
|
|
os.WriteFile(dir, body, 0o644)
|
|
}
|
|
}
|
|
}
|