uffizzi-cli 2.0.35 → 2.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ce4e169f833a82e239af7385a2ad4c46f01d1150d19f85f3feedff4a98e4279
4
- data.tar.gz: a4e435c280b9b5658c1b0048f73f78c5b245e8e37a4d5709d15eea8862d99ca9
3
+ metadata.gz: 72e1e0754278f9e3fc7069a15d1f7963c9aa578a67fdb7495d52f9d633bf1f71
4
+ data.tar.gz: a1cd9e4a84cba3b3827dfe0bb547ed4f3e734dc8cb9640603f5818937d0abee3
5
5
  SHA512:
6
- metadata.gz: 9dbbf25f1500a9ad99a4a4ec27a9524d7a7c2ead0ca0231af6949f0dd67663cf687da3ff98336d5473f332d90abd8d55e4c11a0e92358e508e6943c5eefc097a
7
- data.tar.gz: b38e45251cb074ed709e0fb169108e23f758af7ba9e3473f01e2db7d67e0964181a8c3a4a2a3093cbd52e2ade39c0d3610da71ec1d42cddaa648075040955286
6
+ metadata.gz: f2c0a8c7a2c62cb5ae478f2a18c29a90ee4bdcc2c74b6177930510fc40f1032b2bb8d0e7f8e4947ee30760cc963776d32ac6dbb3e18710a784d2d3cbb4dff862
7
+ data.tar.gz: cbd873c5ef8ab6c8868ee518f34acbada4303f44ef0f30770c1d405ef13b443a9d5431f7593dee81522a6a9b2e1bc1722774f1cce6f20cd16797165391bb9534
@@ -9,6 +9,7 @@ require 'uffizzi/services/preview_service'
9
9
  require 'uffizzi/services/command_service'
10
10
  require 'uffizzi/services/cluster_service'
11
11
  require 'uffizzi/services/kubeconfig_service'
12
+ require 'uffizzi/services/cluster/disconnect_service'
12
13
 
13
14
  MANUAL = 'manual'
14
15
 
@@ -42,7 +43,7 @@ module Uffizzi
42
43
  end
43
44
 
44
45
  desc 'delete [NAME]', 'Delete a cluster'
45
- method_option :'delete-config', required: false, type: :boolean, aliases: '-dc'
46
+ method_option :'delete-config', required: false, type: :boolean, aliases: '-c'
46
47
  def delete(name)
47
48
  run('delete', cluster_name: name)
48
49
  end
@@ -55,6 +56,13 @@ module Uffizzi
55
56
  run('update-kubeconfig', cluster_name: name)
56
57
  end
57
58
 
59
+ method_option :kubeconfig, type: :string, aliases: '-k'
60
+ method_option :ask, type: :boolean
61
+ desc 'disconnect', 'Switch back to original kubeconfig current context'
62
+ def disconnect
63
+ run('disconnect')
64
+ end
65
+
58
66
  private
59
67
 
60
68
  def run(command, command_args = {})
@@ -75,6 +83,8 @@ module Uffizzi
75
83
  handle_delete_command(project_slug, command_args)
76
84
  when 'update-kubeconfig'
77
85
  handle_update_kubeconfig_command(project_slug, command_args)
86
+ when 'disconnect'
87
+ ClusterDisconnectService.handle(options)
78
88
  end
79
89
  end
80
90
 
@@ -191,8 +201,14 @@ module Uffizzi
191
201
 
192
202
  KubeconfigService.save_to_filepath(kubeconfig_path, parsed_kubeconfig) do |kubeconfig_by_path|
193
203
  merged_kubeconfig = KubeconfigService.merge(kubeconfig_by_path, parsed_kubeconfig)
194
- current_context = KubeconfigService.get_current_context(parsed_kubeconfig)
195
- KubeconfigService.update_current_context(merged_kubeconfig, current_context)
204
+ new_current_context = KubeconfigService.get_current_context(parsed_kubeconfig)
205
+ new_kubeconfig = KubeconfigService.update_current_context(merged_kubeconfig, new_current_context)
206
+
207
+ next new_kubeconfig if kubeconfig_by_path.nil?
208
+
209
+ previous_current_context = KubeconfigService.get_current_context(kubeconfig_by_path)
210
+ save_previous_current_context(kubeconfig_path, previous_current_context)
211
+ new_kubeconfig
196
212
  end
197
213
 
198
214
  update_clusters_config(cluster_data[:id], kubeconfig_path: kubeconfig_path)
@@ -319,8 +335,14 @@ module Uffizzi
319
335
  merged_kubeconfig = KubeconfigService.merge(kubeconfig_by_path, kubeconfig)
320
336
 
321
337
  if is_update_current_context
322
- current_context = KubeconfigService.get_current_context(kubeconfig)
323
- KubeconfigService.update_current_context(merged_kubeconfig, current_context)
338
+ new_current_context = KubeconfigService.get_current_context(kubeconfig)
339
+ new_kubeconfig = KubeconfigService.update_current_context(merged_kubeconfig, new_current_context)
340
+
341
+ next new_kubeconfig if kubeconfig_by_path.nil?
342
+
343
+ previous_current_context = KubeconfigService.get_current_context(kubeconfig_by_path)
344
+ save_previous_current_context(kubeconfig_path, previous_current_context)
345
+ new_kubeconfig
324
346
  else
325
347
  merged_kubeconfig
326
348
  end
@@ -359,5 +381,12 @@ module Uffizzi
359
381
  ResponseHelper.handle_failed_response(response)
360
382
  end
361
383
  end
384
+
385
+ def save_previous_current_context(kubeconfig_path, current_context)
386
+ return if kubeconfig_path.nil? || ConfigHelper.previous_current_context_by_path(kubeconfig_path).present?
387
+
388
+ previous_current_contexts = Uffizzi::ConfigHelper.set_previous_current_context_by_path(kubeconfig_path, current_context)
389
+ ConfigFile.write_option(:previous_current_contexts, previous_current_contexts)
390
+ end
362
391
  end
363
392
  end
@@ -79,6 +79,8 @@ module Uffizzi
79
79
  else
80
80
  ResponseHelper.handle_invalid_compose_response(response)
81
81
  end
82
+ elsif ResponseHelper.not_found?(response)
83
+ Uffizzi.ui.say('The project does not have a compose file set.')
82
84
  else
83
85
  ResponseHelper.handle_failed_response(response)
84
86
  end
@@ -39,11 +39,28 @@ module Uffizzi
39
39
  clusters.detect { |c| c[:id] == id }
40
40
  end
41
41
 
42
+ def set_previous_current_context_by_path(path, current_context)
43
+ current_contexts = previous_current_contexts_without(path)
44
+ current_contexts << { current_context: current_context, kubeconfig_path: path }
45
+ end
46
+
47
+ def previous_current_contexts_without(path)
48
+ cluster_previous_current_contexts.reject { |c| c[:kubeconfig_path] == path }
49
+ end
50
+
51
+ def previous_current_context_by_path(path)
52
+ cluster_previous_current_contexts.detect { |c| c[:kubeconfig_path] == path }
53
+ end
54
+
42
55
  private
43
56
 
44
57
  def clusters
45
58
  read_option_from_config(:clusters) || []
46
59
  end
60
+
61
+ def cluster_previous_current_contexts
62
+ read_option_from_config(:previous_current_contexts) || []
63
+ end
47
64
  end
48
65
  end
49
66
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uffizzi/helpers/config_helper'
4
+ require 'uffizzi/services/cluster_service'
5
+ require 'uffizzi/services/kubeconfig_service'
6
+
7
+ class ClusterDisconnectService
8
+ class << self
9
+ def handle(options)
10
+ kubeconfig_path = options[:kubeconfig] || KubeconfigService.default_path
11
+ is_ask_origin_current_context = options[:ask]
12
+
13
+ prev_current_context = Uffizzi::ConfigHelper.previous_current_context_by_path(kubeconfig_path)&.fetch(:current_context, nil)
14
+ kubeconfig = KubeconfigService.read_kubeconfig(kubeconfig_path)
15
+
16
+ if kubeconfig.nil?
17
+ return Uffizzi.ui.say("Kubeconfig does not exist by path #{kubeconfig_path}")
18
+ end
19
+
20
+ contexts = KubeconfigService.get_cluster_contexts(kubeconfig)
21
+ current_context = KubeconfigService.get_current_context(kubeconfig)
22
+
23
+ if contexts.empty?
24
+ return Uffizzi.ui.say("No contexts by kubeconfig path #{kubeconfig_path}")
25
+ end
26
+
27
+ if KubeconfigService.find_cluster_contexts_by_name(kubeconfig, prev_current_context).present? &&
28
+ prev_current_context != current_context &&
29
+ !is_ask_origin_current_context
30
+ update_current_context_by_filepath(kubeconfig_path, prev_current_context)
31
+ say_success(prev_current_context, kubeconfig_path)
32
+ return
33
+ end
34
+
35
+ new_current_context = ask_context(contexts, current_context)
36
+ update_current_context_by_filepath(kubeconfig_path, new_current_context)
37
+ set_previous_current_context_to_config(kubeconfig_path, new_current_context)
38
+ say_success(new_current_context, kubeconfig_path)
39
+ end
40
+
41
+ private
42
+
43
+ def say_success(current_context, kubeconfig_path)
44
+ Uffizzi.ui.say("Now your current context is '#{current_context}' for kubeconfig path '#{kubeconfig_path}'")
45
+ end
46
+
47
+ def update_current_context_by_filepath(filepath, current_context)
48
+ KubeconfigService.save_to_filepath(filepath) do |kubeconfig|
49
+ KubeconfigService.update_current_context(kubeconfig, current_context)
50
+ end
51
+ end
52
+
53
+ def set_previous_current_context_to_config(kubeconfig_path, current_context)
54
+ previous_current_contexts = Uffizzi::ConfigHelper.set_previous_current_context_by_path(kubeconfig_path, current_context)
55
+ Uffizzi::ConfigFile.write_option(:previous_current_contexts, previous_current_contexts)
56
+ end
57
+
58
+ def ask_context(contexts, current_context)
59
+ context_names = contexts
60
+ .map { |c| { name: c['name'], value: c['name'] } }
61
+ .reject { |c| c[:value] == current_context }
62
+
63
+ if context_names.empty?
64
+ return Uffizzi.ui.say_error_and_exit('No other contexts')
65
+ end
66
+
67
+ question = 'Select origin context to switch on:'
68
+ Uffizzi.prompt.select(question, context_names)
69
+ end
70
+ end
71
+ end
@@ -41,15 +41,25 @@ class KubeconfigService
41
41
  end
42
42
 
43
43
  def get_first_context(kubeconfig)
44
- kubeconfig.fetch('contexts', [])[0]
44
+ get_cluster_contexts(kubeconfig)[0]
45
45
  end
46
46
 
47
47
  def get_current_cluster_name(kubeconfig)
48
- kubeconfig['contexts']
48
+ get_cluster_contexts(kubeconfig)
49
49
  .detect { |c| c['name'] == get_current_context(kubeconfig) }
50
50
  .dig('context', 'cluster')
51
51
  end
52
52
 
53
+ def get_cluster_contexts(kubeconfig)
54
+ kubeconfig.fetch('contexts', [])
55
+ end
56
+
57
+ def find_cluster_contexts_by_name(kubeconfig, context_name)
58
+ return if context_name.nil?
59
+
60
+ get_cluster_contexts(kubeconfig).detect { |c| c['name'] == context_name }
61
+ end
62
+
53
63
  def update_current_context(kubeconfig, current_context)
54
64
  new_kubeconfig = kubeconfig.deep_dup
55
65
  new_kubeconfig['current-context'] = current_context
@@ -57,9 +67,8 @@ class KubeconfigService
57
67
  new_kubeconfig
58
68
  end
59
69
 
60
- def save_to_filepath(filepath, kubeconfig)
61
- real_file_path = File.expand_path(filepath)
62
- target_kubeconfig = File.exist?(real_file_path) ? Psych.safe_load(File.read(real_file_path)) : nil
70
+ def save_to_filepath(filepath, kubeconfig = nil)
71
+ target_kubeconfig = read_kubeconfig(filepath)
63
72
 
64
73
  if target_kubeconfig.present? && !valid_kubeconfig?(target_kubeconfig)
65
74
  raise InvalidKubeconfigError.new(filepath)
@@ -68,15 +77,31 @@ class KubeconfigService
68
77
  new_kubeconfig = block_given? ? yield(target_kubeconfig) : kubeconfig
69
78
  return if new_kubeconfig.nil?
70
79
 
71
- dir_path = File.dirname(real_file_path)
72
- FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
73
- File.write(real_file_path, new_kubeconfig.to_yaml)
80
+ write_kubeconfig(filepath, new_kubeconfig)
74
81
  end
75
82
 
76
83
  def default_path
77
84
  kubeconfig_env_path || Uffizzi.configuration.default_kubeconfig_path
78
85
  end
79
86
 
87
+ def read_kubeconfig(filepath)
88
+ real_file_path = File.expand_path(filepath)
89
+ kubeconfig = File.exist?(real_file_path) ? Psych.safe_load(File.read(real_file_path)) : nil
90
+
91
+ if kubeconfig.present? && !valid_kubeconfig?(kubeconfig)
92
+ raise InvalidKubeconfigError.new(filepath)
93
+ end
94
+
95
+ kubeconfig
96
+ end
97
+
98
+ def write_kubeconfig(filepath, kubeconfig)
99
+ real_file_path = File.expand_path(filepath)
100
+ dir_path = File.dirname(real_file_path)
101
+ FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
102
+ File.write(real_file_path, kubeconfig.to_yaml)
103
+ end
104
+
80
105
  private
81
106
 
82
107
  def cluster_exists_in_kubeconfig?(kubeconfig, cluster_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uffizzi
4
- VERSION = '2.0.35'
4
+ VERSION = '2.0.36'
5
5
  end
@@ -0,0 +1,40 @@
1
+ .\" generated with Ronn-NG/v0.9.1
2
+ .\" http://github.com/apjanke/ronn-ng/tree/0.9.1
3
+ .TH "UFFIZZI\-CLUSTER\-DISCONNECT" "" "September 2023" ""
4
+ .SH "NAME"
5
+ \fBuffizzi\-cluster\-disconnect\fR \- disconnect from current cluster
6
+ .SH "SYNOPSIS"
7
+ .nf
8
+ uffizzi cluster disconnect
9
+ .fi
10
+ .SH "DESCRIPTION"
11
+ .nf
12
+ Switch current context to origin\.
13
+
14
+ For more information on Uffizzi clusters, see:
15
+ https://docs\.uffizzi\.com/references/cli/
16
+ .fi
17
+ .SH "FLAGS"
18
+ .nf
19
+ \-\-ask
20
+ Show list available contexts for kubeconfig
21
+
22
+ \-\-kubeconfig="/path/to/your/kubeconfig"
23
+ Path to kubeconfig file
24
+ .fi
25
+ .SH "EXAMPLES"
26
+ .nf
27
+ To disconnect from current cluster context to origin context, run:
28
+
29
+ $ uffizzi cluster disconnect
30
+
31
+ To disconnect from current cluster context to origin context
32
+ and set new origin current context, run:
33
+
34
+ $ uffizzi cluster disconnect \-\-ask
35
+
36
+ To disconnect from current cluster context to origin context for current kubeconfig, run:
37
+
38
+ $ uffizzi cluster disconnect \-\-kubeconfig="/path/to/your/kubeconfig"
39
+ .fi
40
+
@@ -0,0 +1,32 @@
1
+ uffizzi-cluster-disconnect - disconnect from current cluster context
2
+ ====================================================================
3
+
4
+ ## SYNOPSIS
5
+ uffizzi cluster disconnect
6
+
7
+ ## DESCRIPTION
8
+ Switch current context to origin.
9
+
10
+ For more information on Uffizzi clusters, see:
11
+ https://docs.uffizzi.com/references/cli/
12
+
13
+ ## FLAGS
14
+ --ask
15
+ Show list available contexts for kubeconfig and set new origin current context
16
+
17
+ --kubeconfig="/path/to/your/kubeconfig"
18
+ Path to kubeconfig file
19
+
20
+ ## EXAMPLES
21
+ To disconnect from current cluster context to origin context, run:
22
+
23
+ $ uffizzi cluster disconnect
24
+
25
+ To disconnect from current cluster context to origin context
26
+ and set new origin current context, run:
27
+
28
+ $ uffizzi cluster disconnect --ask
29
+
30
+ To disconnect from current cluster context to origin context for current kubeconfig, run:
31
+
32
+ $ uffizzi cluster disconnect --kubeconfig="/path/to/your/kubeconfig"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uffizzi-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.35
4
+ version: 2.0.36
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Thurman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-09-08 00:00:00.000000000 Z
12
+ date: 2023-09-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -445,6 +445,7 @@ files:
445
445
  - lib/uffizzi/helpers/project_helper.rb
446
446
  - lib/uffizzi/promt.rb
447
447
  - lib/uffizzi/response_helper.rb
448
+ - lib/uffizzi/services/cluster/disconnect_service.rb
448
449
  - lib/uffizzi/services/cluster_service.rb
449
450
  - lib/uffizzi/services/command_service.rb
450
451
  - lib/uffizzi/services/compose_file_service.rb
@@ -474,6 +475,8 @@ files:
474
475
  - man/uffizzi-cluster-delete.ronn
475
476
  - man/uffizzi-cluster-describe
476
477
  - man/uffizzi-cluster-describe.ronn
478
+ - man/uffizzi-cluster-disconnect
479
+ - man/uffizzi-cluster-disconnect.ronn
477
480
  - man/uffizzi-cluster-list
478
481
  - man/uffizzi-cluster-list.ronn
479
482
  - man/uffizzi-cluster-update-kubeconfig