]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-create/main.go
1df7143258c9b1766d8b52c25a0d9f78f3e1d065
[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         "io"
24         "log"
25         "os"
26         "path"
27         "strings"
28         "time"
29
30         "go.stargrave.org/meta4ra"
31 )
32
33 func main() {
34         fn := flag.String("fn", "", "Filename")
35         mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
36         desc := flag.String("desc", "", "Description")
37         sigPGP := flag.String("sig-pgp", "", "Path to OpenPGP .asc signature file")
38         sigSSH := flag.String("sig-ssh", "", "Path to OpenSSH .sig signature file")
39         hashes := flag.String("hashes", strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
40         torrent := flag.String("torrent", "", "Torrent URL")
41         log.SetFlags(log.Lshortfile)
42         flag.Parse()
43         if *fn == "" {
44                 log.Fatalln("empty -fn")
45         }
46         urls := make([]meta4ra.URL, 0, len(flag.Args()))
47         for _, u := range flag.Args() {
48                 urls = append(urls, meta4ra.URL{URL: u})
49         }
50         h := meta4ra.NewHasher(*hashes)
51         h.Start()
52         br := bufio.NewReaderSize(os.Stdin, 1<<20)
53         buf := make([]byte, 1<<20)
54         size, err := io.CopyBuffer(h, br, buf)
55         if err != nil {
56                 log.Fatalln(err)
57         }
58         f := meta4ra.File{
59                 Name:        path.Base(*fn),
60                 Description: *desc,
61                 Size:        uint64(size),
62                 URLs:        urls,
63                 Hashes:      h.Sums(),
64         }
65         if *sigPGP != "" {
66                 sigData, err := os.ReadFile(*sigPGP)
67                 if err != nil {
68                         log.Fatalln(err)
69                 }
70                 f.Signature = append(f.Signature, meta4ra.Signature{
71                         MediaType: meta4ra.SigMediaTypePGP,
72                         Signature: "\n" + string(sigData),
73                 })
74         }
75         if *sigSSH != "" {
76                 sigData, err := os.ReadFile(*sigSSH)
77                 if err != nil {
78                         log.Fatalln(err)
79                 }
80                 f.Signature = append(f.Signature, meta4ra.Signature{
81                         MediaType: meta4ra.SigMediaTypeSSH,
82                         Signature: "\n" + string(sigData),
83                 })
84         }
85         if *torrent != "" {
86                 f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
87         }
88         var published time.Time
89         if *mtime == "" {
90                 published = time.Now()
91         } else {
92                 fi, err := os.Stat(*mtime)
93                 if err != nil {
94                         log.Fatalln(err)
95                 }
96                 published = fi.ModTime()
97         }
98         published = published.UTC().Truncate(time.Second)
99         m := meta4ra.Metalink{
100                 Files:     []meta4ra.File{f},
101                 Generator: meta4ra.Generator,
102                 Published: published,
103         }
104         out, err := xml.MarshalIndent(&m, "", "  ")
105         if err != nil {
106                 log.Fatalln(err)
107         }
108         os.Stdout.Write([]byte(xml.Header))
109         os.Stdout.Write(out)
110         os.Stdout.Write([]byte("\n"))
111 }