]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-create/main.go
Ability to skip Published/Generator fields inclusion
[meta4ra.git] / cmd / meta4-create / main.go
1 // meta4ra -- Metalink 4.0 utilities
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 // Metalink 4.0 creator
17 package main
18
19 import (
20         "bufio"
21         "encoding/xml"
22         "flag"
23         "fmt"
24         "io"
25         "log"
26         "os"
27         "path"
28         "strings"
29         "time"
30
31         "go.stargrave.org/meta4ra"
32 )
33
34 func main() {
35         fn := flag.String("fn", "", "Filename")
36         mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
37         desc := flag.String("desc", "", "Description")
38         sigPGP := flag.String("sig-pgp", "",
39                 "Path to OpenPGP .asc signature file for inclusion")
40         sigSSH := flag.String("sig-ssh", "",
41                 "Path to OpenSSH .sig signature file for inclusion")
42         hashes := flag.String("hashes",
43                 strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
44         noPublished := flag.Bool("no-published", false,
45                 "Do not include Published field")
46         noGenerator := flag.Bool("no-generator", false,
47                 "Do not include Generator field")
48         torrent := flag.String("torrent", "", "Torrent URL")
49         flag.Usage = func() {
50                 fmt.Fprintf(flag.CommandLine.Output(),
51                         "Usage: %s [options] [URL ...] < DATA > XXX.meta4\n", os.Args[0])
52                 flag.PrintDefaults()
53         }
54         flag.Parse()
55         log.SetFlags(log.Lshortfile)
56         if *fn == "" {
57                 log.Fatalln("empty -fn")
58         }
59         urls := make([]meta4ra.URL, 0, len(flag.Args()))
60         for _, u := range flag.Args() {
61                 urls = append(urls, meta4ra.URL{URL: u})
62         }
63         h := meta4ra.NewHasher(*hashes)
64         h.Start()
65         br := bufio.NewReaderSize(os.Stdin, 1<<20)
66         buf := make([]byte, 1<<20)
67         size, err := io.CopyBuffer(h, br, buf)
68         if err != nil {
69                 log.Fatalln(err)
70         }
71         f := meta4ra.File{
72                 Name:        path.Base(*fn),
73                 Description: *desc,
74                 Size:        uint64(size),
75                 URLs:        urls,
76                 Hashes:      h.Sums(),
77         }
78         if *sigPGP != "" {
79                 sigData, err := os.ReadFile(*sigPGP)
80                 if err != nil {
81                         log.Fatalln(err)
82                 }
83                 f.Signature = append(f.Signature, meta4ra.Signature{
84                         MediaType: meta4ra.SigMediaTypePGP,
85                         Signature: "\n" + string(sigData),
86                 })
87         }
88         if *sigSSH != "" {
89                 sigData, err := os.ReadFile(*sigSSH)
90                 if err != nil {
91                         log.Fatalln(err)
92                 }
93                 f.Signature = append(f.Signature, meta4ra.Signature{
94                         MediaType: meta4ra.SigMediaTypeSSH,
95                         Signature: "\n" + string(sigData),
96                 })
97         }
98         if *torrent != "" {
99                 f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
100         }
101         var published time.Time
102         if *mtime == "" {
103                 published = time.Now()
104         } else {
105                 fi, err := os.Stat(*mtime)
106                 if err != nil {
107                         log.Fatalln(err)
108                 }
109                 published = fi.ModTime()
110         }
111         m := meta4ra.Metalink{Files: []meta4ra.File{f}}
112         if !*noPublished {
113                 t := published.UTC().Truncate(time.Second)
114                 m.Published = &t
115         }
116         if !*noGenerator {
117                 m.Generator = meta4ra.Generator
118         }
119         out, err := xml.MarshalIndent(&m, "", "  ")
120         if err != nil {
121                 log.Fatalln(err)
122         }
123         os.Stdout.Write([]byte(xml.Header))
124         os.Stdout.Write(out)
125         os.Stdout.Write([]byte("\n"))
126 }