-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
177 lines (154 loc) · 6.82 KB
/
Copy pathmain.go
File metadata and controls
177 lines (154 loc) · 6.82 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
176
177
// Copyright 2026 Elementum Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
elementumcmd "github.com/elementumltd/elementum-cli/cmd"
"github.com/elementumltd/elementum-cli/logger"
"github.com/elementumltd/elementum-cli/ui"
"github.com/spf13/cobra"
)
var version = "dev"
var rootCmd = &cobra.Command{
Use: "ei",
Short: "Elementum Infinity - The power to create and destroy",
Long: `A CLI tool for discovering and exporting Elementum resources to Terraform.
Use this tool to generate import configurations for terraform plan -generate-config-out,
making it easy to manage your Elementum apps with Infrastructure as Code.`,
Version: version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Determine log level from flags and environment
level := getLogLevel(cmd)
logger.Init(level)
// Start background update check (non-blocking)
go checkForUpdateAsync(cmd)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// Show update notification if available (non-blocking check from cache)
if notification := elementumcmd.CheckForUpdateNotification(version); notification != "" {
fmt.Fprint(os.Stderr, notification)
}
},
Run: func(cmd *cobra.Command, args []string) {
// Show the banner when no subcommand is provided
fmt.Println(ui.RenderBanner(version))
_ = cmd.Help()
},
}
// checkForUpdateAsync performs a background version check without blocking.
// This populates the cache so the PersistentPostRun can show a notification.
func checkForUpdateAsync(cmd *cobra.Command) {
// Skip for dev builds or if disabled
if version == "" || version == "dev" {
return
}
if os.Getenv("EI_DISABLE_UPDATE_CHECK") == "1" {
return
}
// Skip for the update command itself
if cmd.Name() == "update" {
return
}
// The actual check happens in CheckForUpdateNotification via cache
// This goroutine just ensures the cache gets populated in the background
}
// getLogLevel determines the effective log level from flags and environment.
// Priority: --quiet > --log-level > --debug > ELEMENTUM_LOG_LEVEL > default (info)
func getLogLevel(cmd *cobra.Command) logger.Level {
// 1. Check --quiet flag (highest priority)
if quiet, _ := cmd.Flags().GetBool("quiet"); quiet {
return logger.LevelError
}
// 2. Check --log-level flag
if lvl, _ := cmd.Flags().GetString("log-level"); lvl != "" {
return logger.ParseLevel(lvl)
}
// 3. Check --debug flag (backwards compatibility)
if debug, _ := cmd.Flags().GetBool("debug"); debug {
return logger.LevelDebug
}
// 4. Check ELEMENTUM_LOG_LEVEL environment variable
if lvl := os.Getenv("ELEMENTUM_LOG_LEVEL"); lvl != "" {
return logger.ParseLevel(lvl)
}
// 5. Default to info
return logger.LevelInfo
}
func init() {
// Global flags
rootCmd.PersistentFlags().String("org", "", "Organization ID (overrides config)")
rootCmd.PersistentFlags().String("client-id", "", "Client ID (overrides config)")
rootCmd.PersistentFlags().String("client-secret", "", "Client Secret (overrides config)")
rootCmd.PersistentFlags().String("instance", "", "Instance/region: us, eu, stage, dev, custom (overrides config)")
rootCmd.PersistentFlags().String("environment", "", "Organization environment slug, e.g. 'staging' (overrides config)")
rootCmd.PersistentFlags().String("custom-base-url", "", "Custom base URL for custom instances (deprecated: use --custom-graphql-url and --custom-api-url)")
rootCmd.PersistentFlags().String("custom-graphql-url", "", "Custom GraphQL URL for custom instances (e.g., http://localhost:3000)")
rootCmd.PersistentFlags().String("custom-api-url", "", "Custom API/OAuth URL for custom instances (e.g., http://localhost:8080)")
rootCmd.PersistentFlags().String("profile", "", "Config profile to use")
// Logging flags
rootCmd.PersistentFlags().String("log-level", "", "Log level: trace, debug, info, warn, error, off (default: info)")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Suppress all non-error output (shortcut for --log-level error)")
rootCmd.PersistentFlags().Bool("debug", false, "Enable debug logging (shortcut for --log-level debug)")
// Output format flags
rootCmd.PersistentFlags().Bool("json", false, "Output in JSON format")
// Register commands
// Core authentication and operations
rootCmd.AddCommand(elementumcmd.GetAuthCmd())
rootCmd.AddCommand(elementumcmd.GetRefsCmd())
rootCmd.AddCommand(elementumcmd.GetPlanCmd())
rootCmd.AddCommand(elementumcmd.GetApplyCmd())
rootCmd.AddCommand(elementumcmd.GetDestroyCmd())
rootCmd.AddCommand(elementumcmd.GetImportCmd())
rootCmd.AddCommand(elementumcmd.GetChatCmd())
rootCmd.AddCommand(elementumcmd.GetConversationCmd())
rootCmd.AddCommand(elementumcmd.GetUpdateCmd())
// Resource commands (ei <resource> <verb> pattern)
rootCmd.AddCommand(elementumcmd.GetAppsCmd())
rootCmd.AddCommand(elementumcmd.GetElementsCmd())
rootCmd.AddCommand(elementumcmd.GetGroupsCmd())
rootCmd.AddCommand(elementumcmd.GetTablesCmd())
rootCmd.AddCommand(elementumcmd.GetTasksCmd())
rootCmd.AddCommand(elementumcmd.GetDataminesCmd())
rootCmd.AddCommand(elementumcmd.GetFieldsCmd())
rootCmd.AddCommand(elementumcmd.GetLayoutsCmd())
rootCmd.AddCommand(elementumcmd.GetRecordsCmd())
rootCmd.AddCommand(elementumcmd.GetAutomationsCmd())
rootCmd.AddCommand(elementumcmd.GetAgentsCmd())
rootCmd.AddCommand(elementumcmd.GetSkillsCmd())
rootCmd.AddCommand(elementumcmd.GetSkillToolsCmd())
rootCmd.AddCommand(elementumcmd.GetAgentToolsCmd())
rootCmd.AddCommand(elementumcmd.GetObjectsCmd())
rootCmd.AddCommand(elementumcmd.GetCategoriesCmd())
rootCmd.AddCommand(elementumcmd.GetCloudlinksCmd())
rootCmd.AddCommand(elementumcmd.GetUsersCmd())
rootCmd.AddCommand(elementumcmd.GetApprovalsCmd())
rootCmd.AddCommand(elementumcmd.GetInterventionsCmd())
rootCmd.AddCommand(elementumcmd.GetFileReadersCmd())
rootCmd.AddCommand(elementumcmd.GetTableCmd())
rootCmd.AddCommand(elementumcmd.GetFunctionsCmd())
rootCmd.AddCommand(elementumcmd.GetA2ASkillsCmd())
// Infrastructure/config commands
rootCmd.AddCommand(elementumcmd.GetAiProvidersCmd())
rootCmd.AddCommand(elementumcmd.GetAiServicesCmd())
rootCmd.AddCommand(elementumcmd.GetFeatureFlagsCmd())
rootCmd.AddCommand(elementumcmd.GetPhoneProvidersCmd())
rootCmd.AddCommand(elementumcmd.GetPhoneServicesCmd())
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}