1// Copyright 2019 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cmd
16
17import (
18	"github.com/spf13/cobra"
19	"github.com/spf13/pflag"
20)
21
22// Common flags
23const (
24	flagAll        flagName = "all"
25	flagDryrun     flagName = "dryrun"
26	flagVerbose    flagName = "verbose"
27	flagAllErrors  flagName = "all-errors"
28	flagTrace      flagName = "trace"
29	flagForce      flagName = "force"
30	flagIgnore     flagName = "ignore"
31	flagStrict     flagName = "strict"
32	flagSimplify   flagName = "simplify"
33	flagPackage    flagName = "package"
34	flagInject     flagName = "inject"
35	flagInjectVars flagName = "inject-vars"
36
37	flagExpression  flagName = "expression"
38	flagSchema      flagName = "schema"
39	flagEscape      flagName = "escape"
40	flagGlob        flagName = "name"
41	flagRecursive   flagName = "recursive"
42	flagMerge       flagName = "merge"
43	flagList        flagName = "list"
44	flagPath        flagName = "path"
45	flagFiles       flagName = "files"
46	flagProtoPath   flagName = "proto_path"
47	flagProtoEnum   flagName = "proto_enum"
48	flagExt         flagName = "ext"
49	flagWithContext flagName = "with-context"
50	flagOut         flagName = "out"
51	flagOutFile     flagName = "outfile"
52)
53
54func addOutFlags(f *pflag.FlagSet, allowNonCUE bool) {
55	if allowNonCUE {
56		f.String(string(flagOut), "",
57			`output format (run 'cue filetypes' for more info)`)
58	}
59	f.StringP(string(flagOutFile), "o", "",
60		`filename or - for stdout with optional file prefix (run 'cue filetypes' for more info)`)
61	f.BoolP(string(flagForce), "f", false, "force overwriting existing files")
62}
63
64func addGlobalFlags(f *pflag.FlagSet) {
65	f.Bool(string(flagTrace), false,
66		"trace computation")
67	f.BoolP(string(flagSimplify), "s", false,
68		"simplify output")
69	f.BoolP(string(flagIgnore), "i", false,
70		"proceed in the presence of errors")
71	f.Bool(string(flagStrict), false,
72		"report errors for lossy mappings")
73	f.BoolP(string(flagVerbose), "v", false,
74		"print information about progress")
75	f.BoolP(string(flagAllErrors), "E", false, "print all available errors")
76}
77
78func addOrphanFlags(f *pflag.FlagSet) {
79	f.StringP(string(flagPackage), "p", "", "package name for non-CUE files")
80	f.StringP(string(flagSchema), "d", "",
81		"expression to select schema for evaluating values in non-CUE files")
82	f.StringArrayP(string(flagPath), "l", nil, "CUE expression for single path component")
83	f.Bool(string(flagList), false, "concatenate multiple objects into a list")
84	f.Bool(string(flagWithContext), false, "import as object with contextual data")
85	f.StringArrayP(string(flagProtoPath), "I", nil, "paths in which to search for imports")
86	f.String(string(flagProtoEnum), "int", "mode for rendering enums (int|json)")
87	f.StringP(string(flagGlob), "n", "", "glob filter for non-CUE file names in directories")
88	f.Bool(string(flagMerge), true, "merge non-CUE files")
89}
90
91func addInjectionFlags(f *pflag.FlagSet, auto bool) {
92	f.StringArrayP(string(flagInject), "t", nil,
93		"set the value of a tagged field")
94	f.BoolP(string(flagInjectVars), "T", auto,
95		"inject system variables in tags")
96}
97
98type flagName string
99
100func (f flagName) Bool(cmd *Command) bool {
101	v, _ := cmd.Flags().GetBool(string(f))
102	return v
103}
104
105func (f flagName) String(cmd *Command) string {
106	v, _ := cmd.Flags().GetString(string(f))
107	return v
108}
109
110func (f flagName) StringArray(cmd *Command) []string {
111	v, _ := cmd.Flags().GetStringArray(string(f))
112	return v
113}
114
115type stringFlag struct {
116	name  string
117	short string
118	text  string
119	def   string
120}
121
122func (f *stringFlag) Add(cmd *cobra.Command) {
123	cmd.Flags().StringP(f.name, f.short, f.def, f.text)
124}
125
126func (f *stringFlag) String(cmd *Command) string {
127	v, err := cmd.Flags().GetString(f.name)
128	if err != nil {
129		return f.def
130	}
131	return v
132}
133