1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pelletier/go-toml/v2"
"github.com/philandstuff/dhall-golang/v6"
"github.com/tailscale/hujson"
"go.jolheiser.com/nixfig"
"gopkg.in/yaml.v3"
)
func maine() error {
var unmarshal func([]byte, any) error
var marshal func(any) ([]byte, error)
fs := flag.NewFlagSet("cfg", flag.ExitOnError)
fromFunc := func(s string) error {
switch strings.ToLower(s) {
case "json", "jsonc":
unmarshal = func(b []byte, a any) error {
b, err := hujson.Standardize(b)
if err != nil {
return err
}
return json.Unmarshal(b, a)
}
case "yaml":
unmarshal = yaml.Unmarshal
case "toml":
unmarshal = toml.Unmarshal
case "dhall":
unmarshal = dhall.Unmarshal
case "nix":
unmarshal = nixfig.Unmarshal
default:
return fmt.Errorf("unknown format %q", s)
}
return nil
}
fs.Func("from", "The format to convert from", fromFunc)
fs.Func("f", "--from", fromFunc)
toFunc := func(s string) error {
switch strings.ToLower(s) {
case "json", "jsonc":
marshal = func(a any) ([]byte, error) {
return json.MarshalIndent(a, "", "\t")
}
case "yaml":
marshal = yaml.Marshal
case "toml":
marshal = toml.Marshal
case "nix":
marshal = nixfig.Marshal
default:
return fmt.Errorf("unknown format %q", s)
}
return nil
}
fs.Func("to", "The format to convert to", toFunc)
fs.Func("t", "--to", toFunc)
if err := fs.Parse(os.Args[1:]); err != nil {
return err
}
if fs.NArg() < 1 {
return errors.New("cfg requires at least an input file")
}
in := fs.Arg(0)
if unmarshal == nil {
if err := fromFunc(strings.TrimPrefix(filepath.Ext(fs.Arg(0)), ".")); err != nil {
return err
}
}
out := os.Stdout
if fs.NArg() > 1 {
fi, err := os.Create(fs.Arg(1))
if err != nil {
return err
}
defer fi.Close()
out = fi
if marshal == nil {
if err := toFunc(strings.TrimPrefix(filepath.Ext(fs.Arg(1)), ".")); err != nil {
return err
}
}
} else if marshal == nil {
marshal = json.Marshal
}
var data any
inData, err := os.ReadFile(in)
if err != nil {
return err
}
if err := unmarshal(inData, &data); err != nil {
return err
}
outData, err := marshal(data)
if err != nil {
return err
}
if _, err := out.Write(outData); err != nil {
return err
}
return nil
}
func main() {
if err := maine(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
|