Debian Codegraph @

internal/cli/query.go

raw symbols SCIP
1 package cli
2
3 import "fmt"
4
5 type QueryCmd struct {
6 Vars variables `flag:"" name:"var" help:"Variables to pass to the query. E.g. --var foo=\"bar\" --var baz=json:file:./some/file.json"`
7 ExtReadWriteFlags extReadWriteFlags `flag:"" name:"rw-flag" help:"Read/Write flag to customise parsing/output. Applies to read + write E.g. --rw-flag csv-delimiter=;"`
8 ExtReadFlags extReadWriteFlags `flag:"" name:"read-flag" help:"Reader flag to customise parsing. E.g. --read-flag xml-mode=structured"`
9 ExtWriteFlags extReadWriteFlags `flag:"" name:"write-flag" help:"Writer flag to customise output. E.g. --write-flag csv-delimiter=;"`
10 InFormat string `flag:"" name:"in" short:"i" help:"The format of the input data."`
11 OutFormat string `flag:"" name:"out" short:"o" help:"The format of the output data."`
12 ReturnRoot bool `flag:"" name:"root" help:"Return the root value."`
13 Compact bool `flag:"" name:"compact" help:"Output in compact mode (no indentation/newlines)."`
14 Unstable bool `flag:"" name:"unstable" help:"Allow access to potentially unstable features."`
15 Interactive bool `flag:"" name:"it" help:"Run in interactive mode (alpha)."`
16
17 ConfigPath string `name:"config" short:"c" help:"Path to config file" default:"~/dasel.yaml"`
18
19 Query string `arg:"" help:"The query to execute." optional:"" default:""`
20 }
21
22 func (c *QueryCmd) Run(ctx *Globals) error {
23 cfg, err := LoadConfig(c.ConfigPath)
24 if err != nil {
25 return err
26 }
27
28 if c.InFormat == "" && c.OutFormat == "" {
29 c.InFormat = cfg.DefaultFormat
30 c.OutFormat = cfg.DefaultFormat
31 }
32
33 if c.Query == "" && c.InFormat == "" && ctx.Stdin == nil {
34 return ErrNoArgsGiven
35 }
36
37 if c.Interactive {
38 return NewInteractiveCmd(c).Run(ctx)
39 }
40
41 o := runOpts{
42 Vars: c.Vars,
43 ExtReadWriteFlags: c.ExtReadWriteFlags,
44 ExtReadFlags: c.ExtReadFlags,
45 ExtWriteFlags: c.ExtWriteFlags,
46 InFormat: c.InFormat,
47 OutFormat: c.OutFormat,
48 Compact: c.Compact,
49 ReturnRoot: c.ReturnRoot,
50 Unstable: c.Unstable,
51 Query: c.Query,
52
53 ConfigPath: c.ConfigPath,
54
55 Stdin: ctx.Stdin,
56 }
57 outBytes, err := run(o)
58 if err != nil {
59 return err
60 }
61
62 _, err = ctx.Stdout.Write(outBytes)
63 if err != nil {
64 return fmt.Errorf("error writing output: %w", err)
65 }
66
67 return nil
68 }