]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-cfgdir/main.go
Unify copyright comment format
[nncp.git] / src / cmd / nncp-cfgdir / main.go
1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-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 // Convert NNCP Hjson configuration file to the directory layout.
17 package main
18
19 import (
20         "flag"
21         "fmt"
22         "log"
23         "os"
24
25         "github.com/hjson/hjson-go/v4"
26         "go.cypherpunks.ru/nncp/v8"
27 )
28
29 func usage() {
30         fmt.Fprint(os.Stderr, "nncp-cfgdir -- Convert configuration file to the directory layout.\n\n")
31         fmt.Fprintf(os.Stderr, "Usage: %s [options] [-cfg ...] -dump /path/to/dir\n", os.Args[0])
32         fmt.Fprintf(os.Stderr, "       %s [options] -load /path/to/dir > cfg.hjson\nOptions:\n", os.Args[0])
33         flag.PrintDefaults()
34 }
35
36 func main() {
37         var (
38                 doDump   = flag.Bool("dump", false, "Dump configuration file to the directory")
39                 doLoad   = flag.Bool("load", false, "Load directory to create configuration file")
40                 cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
41                 version  = flag.Bool("version", false, "Print version information")
42                 warranty = flag.Bool("warranty", false, "Print warranty information")
43         )
44         log.SetFlags(log.Lshortfile)
45         flag.Usage = usage
46         flag.Parse()
47         if *warranty {
48                 fmt.Println(nncp.Warranty)
49                 return
50         }
51         if *version {
52                 fmt.Println(nncp.VersionGet())
53                 return
54         }
55
56         if (!*doDump && !*doLoad) || flag.NArg() != 1 {
57                 usage()
58                 os.Exit(1)
59         }
60
61         if *doDump {
62                 cfgRaw, err := os.ReadFile(*cfgPath)
63                 if err != nil {
64                         log.Fatalln(err)
65                 }
66                 cfg, err := nncp.CfgParse(cfgRaw)
67                 if err != nil {
68                         log.Fatalln(err)
69                 }
70                 if err = nncp.CfgToDir(flag.Arg(0), cfg); err != nil {
71                         log.Fatalln(err)
72                 }
73         }
74         if *doLoad {
75                 cfg, err := nncp.DirToCfg(flag.Arg(0))
76                 if err != nil {
77                         log.Fatalln(err)
78                 }
79                 if _, err = nncp.Cfg2Ctx(cfg); err != nil {
80                         log.Fatalln(err)
81                 }
82                 marshaled, err := hjson.MarshalWithOptions(cfg, hjson.EncoderOptions{
83                         Eol:            "\n",
84                         BracesSameLine: true,
85                         QuoteAlways:    false,
86                         IndentBy:       "  ",
87                 })
88                 if err != nil {
89                         log.Fatalln(err)
90                 }
91                 os.Stdout.Write(marshaled)
92                 os.Stdout.WriteString("\n")
93         }
94 }