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