You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.1 KiB

1 week ago
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
1 week ago
"gopkg.in/yaml.v3"
"github.com/snapcore/snapd/store"
1 week ago
)
type seed struct {
Snaps []struct {
Name string `yaml:"name"`
Channel string `yaml:"channel"`
File string `yaml:"file"`
} `yaml:"snaps"`
}
// getChannelName returns the channel name for a specific snap name
func getChannelName(snapName string) (string, error) {
file, err := ioutil.ReadFile(seedYaml)
if err != nil {
return "", fmt.Errorf("failed to read seed.yaml: %w", err)
}
var seedData seed
if err := yaml.Unmarshal(file, &seedData); err != nil {
return "", fmt.Errorf("failed to parse seed.yaml: %w", err)
}
for _, snap := range seedData.Snaps {
if snap.Name == snapName {
return snap.Channel, nil
}
}
return "", fmt.Errorf("snap %s not found in seed.yaml", snapName)
}
1 week ago
// initializeSeedYaml ensures that seed.yaml exists; if not, creates it.
func initializeSeedYaml() {
if _, err := os.Stat(seedYaml); os.IsNotExist(err) {
file, err := os.Create(seedYaml)
if err != nil {
log.Fatalf("Failed to create seed.yaml: %v", err)
}
defer file.Close()
file.WriteString("snaps:\n")
}
1 week ago
}
// loadSeedData loads seed data from seed.yaml
func loadSeedData() seed {
file, err := ioutil.ReadFile(seedYaml)
if err != nil {
log.Fatalf("Failed to read seed.yaml: %v", err)
}
1 week ago
var seedData seed
if err := yaml.Unmarshal(file, &seedData); err != nil {
log.Fatalf("Failed to parse seed.yaml: %v", err)
}
1 week ago
return seedData
1 week ago
}
// loadExistingSnaps loads snaps from seed.yaml into a map
func loadExistingSnaps() map[string]bool {
file, err := ioutil.ReadFile(seedYaml)
if err != nil {
log.Fatalf("Failed to read seed.yaml: %v", err)
}
var seedData seed
if err := yaml.Unmarshal(file, &seedData); err != nil {
log.Fatalf("Failed to parse seed.yaml: %v", err)
}
existing := make(map[string]bool)
for _, snap := range seedData.Snaps {
existing[snap.Name] = true
verboseLog("Found %s in seed.yaml\n", snap.Name)
}
return existing
1 week ago
}
// updateSeedYaml updates the seed.yaml file with the current required snaps
func updateSeedYaml(snapsDir string, currentSnaps []*store.CurrentSnap) error {
// Log the snaps to be written
verboseLog("CurrentSnaps to be written to seed.yaml:")
for _, snapInfo := range currentSnaps {
verboseLog("- %s_%d.snap", snapInfo.InstanceName, snapInfo.Revision.N)
}
// Load existing seed data
file, err := ioutil.ReadFile(seedYaml)
if err != nil {
return fmt.Errorf("failed to read seed.yaml: %w", err)
}
var seedData seed
if err := yaml.Unmarshal(file, &seedData); err != nil {
return fmt.Errorf("failed to parse seed.yaml: %w", err)
}
// Clear existing snaps
seedData.Snaps = []struct {
Name string `yaml:"name"`
Channel string `yaml:"channel"`
File string `yaml:"file"`
}{}
// Populate seedData with currentSnaps
for _, snapInfo := range currentSnaps {
snapFileName := fmt.Sprintf("%s_%d.snap", snapInfo.InstanceName, snapInfo.Revision.N)
snapData := struct {
Name string `yaml:"name"`
Channel string `yaml:"channel"`
File string `yaml:"file"`
}{
Name: snapInfo.InstanceName,
Channel: strings.Replace(snapInfo.TrackingChannel, "latest/", "", -1),
File: snapFileName,
}
seedData.Snaps = append(seedData.Snaps, snapData)
}
// Marshal the updated seedData back to YAML
updatedYAML, err := yaml.Marshal(&seedData)
if err != nil {
return fmt.Errorf("failed to marshal updated seed data: %w", err)
}
// Write the updated YAML back to seed.yaml
if err := ioutil.WriteFile(seedYaml, updatedYAML, 0644); err != nil {
return fmt.Errorf("failed to write updated seed.yaml: %w", err)
}
verboseLog("Updated seed.yaml with current snaps.")
return nil
1 week ago
}