66 "crypto/tls"
77 "encoding/json"
88 "fmt"
9- "google.golang.org/grpc/credentials/insecure"
109 "log"
1110 "net/http"
1211 "net/url"
@@ -15,6 +14,8 @@ import (
1514 "regexp"
1615 "strings"
1716
17+ "google.golang.org/grpc/credentials/insecure"
18+
1819 "gopkg.in/yaml.v3"
1920
2021 "github.com/jhump/protoreflect/dynamic"
@@ -53,16 +54,21 @@ var settingInitCmd = &cobra.Command{
5354 cfctl setting init endpoint http://localhost:8080 --app
5455 cfctl setting init endpoint http://localhost:8080 --user
5556 or
56- cfctl setting init local` ,
57+ cfctl setting init static grpc://localhost:50051
58+ cfctl setting init static grpc+ssl://inventory.-` ,
5759}
5860
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 ),
6570 Run : func (cmd * cobra.Command , args []string ) {
71+ endpoint := args [0 ]
6672 settingDir := GetSettingDir ()
6773 if err := os .MkdirAll (settingDir , 0755 ); err != nil {
6874 pterm .Error .Printf ("Failed to create setting directory: %v\n " , err )
@@ -74,14 +80,20 @@ var settingInitLocalCmd = &cobra.Command{
7480 v .SetConfigFile (mainSettingPath )
7581 v .SetConfigType ("yaml" )
7682
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
7890 if err := v .ReadInConfig (); err == nil {
7991 environments := v .GetStringMap ("environments" )
80- if existingEnv , exists := environments ["local" ]; exists {
92+ if existingEnv , exists := environments [envName ]; exists {
8193 currentConfig , _ := yaml .Marshal (map [string ]interface {}{
82- "environment" : "local" ,
94+ "environment" : envName ,
8395 "environments" : map [string ]interface {}{
84- "local" : existingEnv ,
96+ envName : existingEnv ,
8597 },
8698 })
8799
@@ -91,7 +103,7 @@ var settingInitLocalCmd = &cobra.Command{
91103 WithLeftPadding (4 ).
92104 WithBoxStyle (pterm .NewStyle (pterm .FgYellow ))
93105
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 ) )
95107
96108 pterm .Info .Println ("Current configuration:" )
97109 fmt .Println (string (currentConfig ))
@@ -102,13 +114,14 @@ var settingInitLocalCmd = &cobra.Command{
102114 response = strings .ToLower (strings .TrimSpace (response ))
103115
104116 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 )
106118 return
107119 }
108120 }
109121 }
110122
111- updateSetting ("local" , "grpc://localhost:50051" , "" )
123+ updateSetting (envName , endpoint , "" )
124+ pterm .Success .Printf ("Successfully initialized direct connection to %s\n " , endpoint )
112125 },
113126}
114127
@@ -480,6 +493,17 @@ You can either specify a new endpoint URL directly or use the service-based endp
480493 if listFlag {
481494 token , err := getToken (appV )
482495 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+ }
483507 pterm .Error .Println ("Error retrieving token:" , err )
484508 return
485509 }
@@ -1201,11 +1225,17 @@ func updateGlobalSetting() {
12011225}
12021226
12031227func parseEnvNameFromURL (urlStr string ) (string , error ) {
1228+ isGRPC := strings .HasPrefix (urlStr , "grpc://" ) || strings .HasPrefix (urlStr , "grpc+ssl://" )
1229+
12041230 urlStr = strings .TrimPrefix (urlStr , "https://" )
12051231 urlStr = strings .TrimPrefix (urlStr , "http://" )
12061232 urlStr = strings .TrimPrefix (urlStr , "grpc://" )
12071233 urlStr = strings .TrimPrefix (urlStr , "grpc+ssl://" )
12081234
1235+ if isGRPC {
1236+ return "local" , nil
1237+ }
1238+
12091239 if strings .Contains (urlStr , "localhost" ) {
12101240 return "local" , nil
12111241 }
@@ -1271,6 +1301,19 @@ func updateSetting(envName, endpoint string, envSuffix string) {
12711301 envKey := fmt .Sprintf ("environments.%s.endpoint" , fullEnvName )
12721302 v .Set (envKey , endpoint )
12731303
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+
12741317 // Set additional configurations based on environment type
12751318 if envName == "local" {
12761319 // Local environment settings (only for pure 'local', not 'local-user' or 'local-app')
@@ -1378,7 +1421,7 @@ func init() {
13781421 SettingCmd .AddCommand (settingEndpointCmd )
13791422 SettingCmd .AddCommand (showCmd )
13801423 settingInitCmd .AddCommand (settingInitEndpointCmd )
1381- settingInitCmd .AddCommand (settingInitLocalCmd )
1424+ settingInitCmd .AddCommand (settingInitStaticCmd )
13821425
13831426 settingInitEndpointCmd .Flags ().Bool ("app" , false , "Initialize as application configuration" )
13841427 settingInitEndpointCmd .Flags ().Bool ("user" , false , "Initialize as user-specific configuration" )
0 commit comments