forked from tfquery/tfquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
175 lines (151 loc) · 4.15 KB
/
Copy pathmain.go
File metadata and controls
175 lines (151 loc) · 4.15 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2026 Steve Taranto <staranto@gmail.com>.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/tfctl/tfctl/internal/cacheutil"
"github.com/tfctl/tfctl/internal/command"
"github.com/tfctl/tfctl/internal/config"
"github.com/tfctl/tfctl/internal/log"
"github.com/tfctl/tfctl/internal/util"
"github.com/tfctl/tfctl/internal/version"
)
var ctx = context.Background()
func main() {
os.Exit(realMain())
}
// handleVersion checks for --version/-v and returns whether it was handled.
func handleVersion(args []string) bool {
for _, a := range args {
if a == "--version" || a == "-v" {
fmt.Println(version.Version)
return true
}
}
return false
}
// handleNakedCommand appends --help if no command is provided.
func handleNakedCommand(args []string) []string {
if len(args) <= 1 {
return append(args, "--help")
}
return args
}
// processCommandArgs handles command-specific argument processing.
func processCommandArgs(args []string) []string {
switch {
case len(args) > 1 && args[1] == "completion":
// Short-circuit completion: pass args directly.
return args
default:
// For ps and other commands, process @set first.
args = processSetOnly(args)
log.Debugf("args after set processing: args=%v", args)
if len(args) > 1 && args[1] == "ps" {
args = processPsArgs(args)
} else {
args = processOtherArgs(args)
}
return args
}
}
// processPsArgs handles argument processing for the ps command.
func processPsArgs(args []string) []string {
// Ensure the argument immediately following "ps" is "-" or an existing file.
if len(args) == 2 || (args[2] != "-" && !isExistingFile(args[2])) {
args = append(args[:2], append([]string{"-"}, args[2:]...)...)
}
return args
}
// processOtherArgs handles argument processing for other commands.
func processOtherArgs(args []string) []string {
rootDir, _ := os.Getwd()
if len(args) > 2 {
if _, _, err := util.ParseRootDir(args[2]); err == nil {
rootDir = args[2]
}
}
if len(args) == 2 {
args = append(args, rootDir)
} else if args[2] != rootDir {
args = append(args[:2], append([]string{rootDir}, args[2:]...)...)
}
return args
}
// initAndRunApp initializes the app and runs it, returning the exit code.
func initAndRunApp(args []string) int {
// Pre-create cache directory when caching is enabled.
if _, ok, err := cacheutil.EnsureBaseDir(); err != nil && ok {
fmt.Fprintln(os.Stderr, err)
log.Debugf("cache ensure err: err=%v", err)
}
app, err := command.InitApp(ctx, args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
log.Debugf("app init err: err=%v", err)
return 1
}
if err := app.Run(ctx, args); err != nil {
fmt.Fprintln(os.Stderr, err)
log.Debugf("app run err: err=%v", err)
return 2
}
return 0
}
func realMain() int {
log.InitLogger()
args := os.Args
log.Debugf("args captured: args=%v", args)
if handleVersion(args) {
return 0
}
args = handleNakedCommand(args)
// If --help appears anywhere, skip command processing and let the CLI handle it.
helpFound := false
for _, a := range args {
if a == "--help" || a == "-h" {
helpFound = true
break
}
}
if !helpFound {
args = processCommandArgs(args)
}
return initAndRunApp(args)
}
// isExistingFile checks if the given path exists and is a file.
func isExistingFile(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
// processSetOnly handles the @set logic for all commands, expanding set arguments at the @set position.
func processSetOnly(args []string) []string {
// Look for an explicit @set argument starting from index 2.
idx := 2
set := "defaults"
removeIdx := -1
for i, a := range args[idx:] {
if strings.HasPrefix(a, "@") {
set = a[1:]
removeIdx = idx + i
break
}
}
if removeIdx != -1 {
// Remove the @set argument.
args = append(args[:removeIdx], args[removeIdx+1:]...)
// Expand the set arguments at the removeIdx position.
setArgs, _ := config.GetStringSlice(args[1] + "." + set)
for _, arg := range setArgs {
parts := strings.Fields(arg)
args = append(args[:removeIdx], append(parts, args[removeIdx:]...)...)
removeIdx += len(parts)
}
}
return args
}