6
6
"crypto/tls"
7
7
"encoding/json"
8
8
"fmt"
9
- "google.golang.org/grpc/credentials/insecure"
10
9
"log"
11
10
"net/http"
12
11
"net/url"
@@ -15,6 +14,8 @@ import (
15
14
"regexp"
16
15
"strings"
17
16
17
+ "google.golang.org/grpc/credentials/insecure"
18
+
18
19
"gopkg.in/yaml.v3"
19
20
20
21
"github.com/jhump/protoreflect/dynamic"
@@ -53,16 +54,21 @@ var settingInitCmd = &cobra.Command{
53
54
cfctl setting init endpoint http://localhost:8080 --app
54
55
cfctl setting init endpoint http://localhost:8080 --user
55
56
or
56
- cfctl setting init local` ,
57
+ cfctl setting init static grpc://localhost:50051
58
+ cfctl setting init static grpc+ssl://inventory.-` ,
57
59
}
58
60
59
- // settingInitLocalCmd represents the setting init local command
60
- var settingInitLocalCmd = & cobra.Command {
61
- Use : "local" ,
62
- Short : "Initialize local environment setting" ,
63
- Long : `Initialize a local environment setting with default configuration.` ,
64
- Args : cobra .NoArgs ,
61
+ // settingInitStaticCmd represents the setting init direct command
62
+ var settingInitStaticCmd = & cobra.Command {
63
+ Use : "static [endpoint]" ,
64
+ Short : "Initialize static connection to a local or service endpoint" ,
65
+ Long : `Initialize configuration with a static service endpoint.
66
+ This is useful for development or when connecting directly to specific service endpoints.` ,
67
+ Example : ` cfctl setting init static grpc://localhost:50051
68
+ cfctl setting init static grpc+ssl://inventory-` ,
69
+ Args : cobra .ExactArgs (1 ),
65
70
Run : func (cmd * cobra.Command , args []string ) {
71
+ endpoint := args [0 ]
66
72
settingDir := GetSettingDir ()
67
73
if err := os .MkdirAll (settingDir , 0755 ); err != nil {
68
74
pterm .Error .Printf ("Failed to create setting directory: %v\n " , err )
@@ -74,14 +80,20 @@ var settingInitLocalCmd = &cobra.Command{
74
80
v .SetConfigFile (mainSettingPath )
75
81
v .SetConfigType ("yaml" )
76
82
77
- // Check if local environment already exists
83
+ envName , err := parseEnvNameFromURL (endpoint )
84
+ if err != nil {
85
+ pterm .Error .Printf ("Failed to parse environment name: %v\n " , err )
86
+ return
87
+ }
88
+
89
+ // Check if environment already exists
78
90
if err := v .ReadInConfig (); err == nil {
79
91
environments := v .GetStringMap ("environments" )
80
- if existingEnv , exists := environments ["local" ]; exists {
92
+ if existingEnv , exists := environments [envName ]; exists {
81
93
currentConfig , _ := yaml .Marshal (map [string ]interface {}{
82
- "environment" : "local" ,
94
+ "environment" : envName ,
83
95
"environments" : map [string ]interface {}{
84
- "local" : existingEnv ,
96
+ envName : existingEnv ,
85
97
},
86
98
})
87
99
@@ -91,7 +103,7 @@ var settingInitLocalCmd = &cobra.Command{
91
103
WithLeftPadding (4 ).
92
104
WithBoxStyle (pterm .NewStyle (pterm .FgYellow ))
93
105
94
- confirmBox .Println ("Environment 'local ' already exists.\n Do you want to overwrite it?" )
106
+ confirmBox .Println (fmt . Sprintf ( "Environment '%s ' already exists.\n Do you want to overwrite it?" , envName ) )
95
107
96
108
pterm .Info .Println ("Current configuration:" )
97
109
fmt .Println (string (currentConfig ))
@@ -102,13 +114,14 @@ var settingInitLocalCmd = &cobra.Command{
102
114
response = strings .ToLower (strings .TrimSpace (response ))
103
115
104
116
if response != "y" {
105
- pterm .Info .Println ("Operation cancelled. Environment 'local ' remains unchanged." )
117
+ pterm .Info .Printf ("Operation cancelled. Environment '%s ' remains unchanged.\n " , envName )
106
118
return
107
119
}
108
120
}
109
121
}
110
122
111
- updateSetting ("local" , "grpc://localhost:50051" , "" )
123
+ updateSetting (envName , endpoint , "" )
124
+ pterm .Success .Printf ("Successfully initialized direct connection to %s\n " , endpoint )
112
125
},
113
126
}
114
127
@@ -480,6 +493,17 @@ You can either specify a new endpoint URL directly or use the service-based endp
480
493
if listFlag {
481
494
token , err := getToken (appV )
482
495
if err != nil {
496
+ if strings .HasSuffix (currentEnv , "-user" ) {
497
+ pterm .DefaultBox .WithTitle ("Authentication Required" ).
498
+ WithTitleTopCenter ().
499
+ WithBoxStyle (pterm .NewStyle (pterm .FgLightCyan )).
500
+ WithRightPadding (4 ).
501
+ WithLeftPadding (4 ).
502
+ Println ("Please login to SpaceONE Console first.\n " +
503
+ "Run the following command to authenticate:\n \n " +
504
+ "$ cfctl login" )
505
+ return
506
+ }
483
507
pterm .Error .Println ("Error retrieving token:" , err )
484
508
return
485
509
}
@@ -1201,11 +1225,17 @@ func updateGlobalSetting() {
1201
1225
}
1202
1226
1203
1227
func parseEnvNameFromURL (urlStr string ) (string , error ) {
1228
+ isGRPC := strings .HasPrefix (urlStr , "grpc://" ) || strings .HasPrefix (urlStr , "grpc+ssl://" )
1229
+
1204
1230
urlStr = strings .TrimPrefix (urlStr , "https://" )
1205
1231
urlStr = strings .TrimPrefix (urlStr , "http://" )
1206
1232
urlStr = strings .TrimPrefix (urlStr , "grpc://" )
1207
1233
urlStr = strings .TrimPrefix (urlStr , "grpc+ssl://" )
1208
1234
1235
+ if isGRPC {
1236
+ return "local" , nil
1237
+ }
1238
+
1209
1239
if strings .Contains (urlStr , "localhost" ) {
1210
1240
return "local" , nil
1211
1241
}
@@ -1271,6 +1301,19 @@ func updateSetting(envName, endpoint string, envSuffix string) {
1271
1301
envKey := fmt .Sprintf ("environments.%s.endpoint" , fullEnvName )
1272
1302
v .Set (envKey , endpoint )
1273
1303
1304
+ // Set proxy based on endpoint type
1305
+ proxyKey := fmt .Sprintf ("environments.%s.proxy" , fullEnvName )
1306
+ if strings .HasPrefix (endpoint , "grpc://" ) || strings .HasPrefix (endpoint , "grpc+ssl://" ) {
1307
+ // Check if endpoint contains 'identity'
1308
+ if strings .Contains (strings .ToLower (endpoint ), "identity" ) {
1309
+ v .Set (proxyKey , true )
1310
+ } else {
1311
+ v .Set (proxyKey , false )
1312
+ }
1313
+ } else {
1314
+ v .Set (proxyKey , true )
1315
+ }
1316
+
1274
1317
// Set additional configurations based on environment type
1275
1318
if envName == "local" {
1276
1319
// Local environment settings (only for pure 'local', not 'local-user' or 'local-app')
@@ -1378,7 +1421,7 @@ func init() {
1378
1421
SettingCmd .AddCommand (settingEndpointCmd )
1379
1422
SettingCmd .AddCommand (showCmd )
1380
1423
settingInitCmd .AddCommand (settingInitEndpointCmd )
1381
- settingInitCmd .AddCommand (settingInitLocalCmd )
1424
+ settingInitCmd .AddCommand (settingInitStaticCmd )
1382
1425
1383
1426
settingInitEndpointCmd .Flags ().Bool ("app" , false , "Initialize as application configuration" )
1384
1427
settingInitEndpointCmd .Flags ().Bool ("user" , false , "Initialize as user-specific configuration" )
0 commit comments