-
Notifications
You must be signed in to change notification settings - Fork 70
/
io.go
72 lines (65 loc) · 2.08 KB
/
io.go
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
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cli
import (
"fmt"
"io/fs"
"cogentcore.org/core/base/dirs"
"cogentcore.org/core/base/iox/tomlx"
"cogentcore.org/core/base/reflectx"
)
// OpenWithIncludes reads the config struct from the given config file
// using the given options, looking on [Options.IncludePaths] for the file.
// It opens any Includes specified in the given config file in the natural
// include order so that includers overwrite included settings.
// Is equivalent to Open if there are no Includes. It returns an error if
// any of the include files cannot be found on [Options.IncludePaths].
func OpenWithIncludes(opts *Options, cfg any, file string) error {
files := dirs.FindFilesOnPaths(opts.IncludePaths, file)
if len(files) == 0 {
return fmt.Errorf("OpenWithIncludes: no files found for %q", file)
}
err := tomlx.OpenFiles(cfg, files)
if err != nil {
return err
}
incfg, ok := cfg.(Includer)
if !ok {
return err
}
incs, err := IncludeStack(opts, incfg)
ni := len(incs)
if ni == 0 {
return err
}
for i := ni - 1; i >= 0; i-- {
inc := incs[i]
err = tomlx.OpenFiles(cfg, dirs.FindFilesOnPaths(opts.IncludePaths, inc))
if err != nil {
fmt.Println(err)
}
}
// reopen original
err = tomlx.OpenFiles(cfg, dirs.FindFilesOnPaths(opts.IncludePaths, file))
if err != nil {
return err
}
*incfg.IncludesPtr() = incs
return err
}
// OpenFS reads the given config object from the given file.
func Open(cfg any, file string) error {
return tomlx.Open(cfg, file)
}
// OpenFS reads the given config object from given file, using
// the given [fs.FS] filesystem (e.g., for embed files).
func OpenFS(cfg any, fsys fs.FS, file string) error {
return tomlx.OpenFS(cfg, fsys, file)
}
// Save writes the given config object to the given file.
// It only saves the non-default fields of the given object,
// as specified by [reflectx.NonDefaultFields].
func Save(cfg any, file string) error {
return tomlx.Save(reflectx.NonDefaultFields(cfg), file)
}