github.com/kubri/kubri@v0.5.1-0.20240317001612-bda2aaef967e/integrations/sparkle/rss.go (about)

     1  package sparkle
     2  
     3  import "encoding/xml"
     4  
     5  type RSS struct {
     6  	Channels []*Channel `xml:"channel"`
     7  }
     8  
     9  func (r *RSS) MarshalXML(enc *xml.Encoder, _ xml.StartElement) error {
    10  	err := enc.EncodeToken(xml.StartElement{
    11  		Name: xml.Name{Local: "rss"},
    12  		Attr: []xml.Attr{
    13  			{Name: xml.Name{Local: "version"}, Value: "2.0"},
    14  			{Name: xml.Name{Local: "xmlns:sparkle"}, Value: "http://www.andymatuschak.org/xml-namespaces/sparkle"},
    15  			{Name: xml.Name{Local: "xmlns:dc"}, Value: "http://purl.org/dc/elements/1.1/"},
    16  		},
    17  	})
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	err = enc.EncodeElement(r.Channels, xml.StartElement{Name: xml.Name{Local: "channel"}})
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	return enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "rss"}})
    28  }
    29  
    30  func (r *RSS) UnmarshalXML(dec *xml.Decoder, _ xml.StartElement) error {
    31  	var data []unmarshalRSS
    32  	if err := dec.Decode(&data); err != nil {
    33  		return err
    34  	}
    35  
    36  	r.Channels = make([]*Channel, 0, len(data))
    37  	for _, item := range data {
    38  		channel := &Channel{
    39  			Title:       item.Title,
    40  			Link:        item.Link,
    41  			Description: item.Description,
    42  			Language:    item.Language,
    43  			Items:       make([]*Item, 0, len(item.Items)),
    44  		}
    45  
    46  		for _, item := range item.Items {
    47  			channel.Items = append(channel.Items, &Item{
    48  				Title:                             item.Title,
    49  				PubDate:                           item.PubDate,
    50  				Description:                       item.Description,
    51  				Version:                           item.Version,
    52  				ReleaseNotesLink:                  item.ReleaseNotesLink,
    53  				CriticalUpdate:                    (*CriticalUpdate)(item.CriticalUpdate),
    54  				Tags:                              (*Tags)(item.Tags),
    55  				MinimumAutoupdateVersion:          item.MinimumAutoupdateVersion,
    56  				IgnoreSkippedUpgradesBelowVersion: item.IgnoreSkippedUpgradesBelowVersion,
    57  				Enclosure:                         (*Enclosure)(item.Enclosure),
    58  			})
    59  		}
    60  
    61  		r.Channels = append(r.Channels, channel)
    62  	}
    63  
    64  	return dec.Skip()
    65  }
    66  
    67  type Channel struct {
    68  	Title       string  `xml:"title"`
    69  	Link        string  `xml:"link,omitempty"`
    70  	Description string  `xml:"description,omitempty"`
    71  	Language    string  `xml:"language,omitempty"`
    72  	Items       []*Item `xml:"item"`
    73  }
    74  
    75  type Item struct {
    76  	Title                             string          `xml:"title"`
    77  	PubDate                           string          `xml:"pubDate"`
    78  	Description                       *CdataString    `xml:"description,omitempty"`
    79  	Version                           string          `xml:"sparkle:version,omitempty"`
    80  	ReleaseNotesLink                  string          `xml:"sparkle:releaseNotesLink,omitempty"`
    81  	CriticalUpdate                    *CriticalUpdate `xml:"sparkle:criticalUpdate,omitempty"`
    82  	Tags                              *Tags           `xml:"sparkle:tags,omitempty"`
    83  	MinimumAutoupdateVersion          string          `xml:"sparkle:minimumAutoupdateVersion,omitempty"`
    84  	IgnoreSkippedUpgradesBelowVersion string          `xml:"sparkle:ignoreSkippedUpgradesBelowVersion,omitempty"`
    85  	Enclosure                         *Enclosure      `xml:"enclosure,omitempty"`
    86  }
    87  
    88  // CdataString for XML CDATA
    89  // See issue: https://github.com/golang/go/issues/16198
    90  type CdataString struct {
    91  	Value string `xml:",cdata"`
    92  }
    93  
    94  type CriticalUpdate struct {
    95  	Version string `xml:"sparkle:version,attr,omitempty"`
    96  }
    97  
    98  type Tags struct {
    99  	CriticalUpdate Bool `xml:"sparkle:criticalUpdate,omitempty"`
   100  }
   101  
   102  type Bool bool
   103  
   104  func (xb *Bool) MarshalText() ([]byte, error) {
   105  	return nil, nil
   106  }
   107  
   108  func (xb *Bool) UnmarshalText([]byte) error {
   109  	*xb = true
   110  	return nil
   111  }
   112  
   113  type Enclosure struct {
   114  	URL                  string `xml:"url,attr"`
   115  	OS                   string `xml:"sparkle:os,attr"`
   116  	Version              string `xml:"sparkle:version,attr"`
   117  	DSASignature         string `xml:"sparkle:dsaSignature,attr,omitempty"`
   118  	EDSignature          string `xml:"sparkle:edSignature,attr,omitempty"`
   119  	InstallerArguments   string `xml:"sparkle:installerArguments,attr,omitempty"`
   120  	MinimumSystemVersion string `xml:"sparkle:minimumSystemVersion,attr,omitempty"`
   121  	Length               int    `xml:"length,attr,omitempty"`
   122  	Type                 string `xml:"type,attr"`
   123  }
   124  
   125  type unmarshalRSS struct {
   126  	XMLName     xml.Name `xml:"channel"`
   127  	Title       string   `xml:"title"`
   128  	Link        string   `xml:"link,omitempty"`
   129  	Description string   `xml:"description,omitempty"`
   130  	Language    string   `xml:"language,omitempty"`
   131  	Items       []struct {
   132  		Title            string       `xml:"title"`
   133  		PubDate          string       `xml:"pubDate"`
   134  		Description      *CdataString `xml:"description,omitempty"`
   135  		Version          string       `xml:"version,omitempty"`
   136  		ReleaseNotesLink string       `xml:"releaseNotesLink,omitempty"`
   137  		CriticalUpdate   *struct {
   138  			Version string `xml:"version,attr,omitempty"`
   139  		} `xml:"criticalUpdate,omitempty"`
   140  		Tags *struct {
   141  			CriticalUpdate Bool `xml:"criticalUpdate,omitempty"`
   142  		} `xml:"tags,omitempty"`
   143  		MinimumAutoupdateVersion          string `xml:"minimumAutoupdateVersion,omitempty"`
   144  		IgnoreSkippedUpgradesBelowVersion string `xml:"ignoreSkippedUpgradesBelowVersion,omitempty"`
   145  		Enclosure                         *struct {
   146  			URL                  string `xml:"url,attr"`
   147  			OS                   string `xml:"os,attr"`
   148  			Version              string `xml:"version,attr"`
   149  			DSASignature         string `xml:"dsaSignature,attr,omitempty"`
   150  			EDSignature          string `xml:"edSignature,attr,omitempty"`
   151  			InstallerArguments   string `xml:"installerArguments,attr,omitempty"`
   152  			MinimumSystemVersion string `xml:"minimumSystemVersion,attr,omitempty"`
   153  			Length               int    `xml:"length,attr,omitempty"`
   154  			Type                 string `xml:"type,attr"`
   155  		} `xml:"enclosure,omitempty"`
   156  	} `xml:"item"`
   157  }