sem 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d9b08c9b6f0e386eea1f0d6dd58d47d0b6fae07
4
- data.tar.gz: 0f3cc15c62d9f5463f1b491e242cbdf02b6caffa
3
+ metadata.gz: 6c386662fdc6f698a75d2a465bf2341995130638
4
+ data.tar.gz: 8042f7c3359a3ff026658784aed2950fd6b36d7d
5
5
  SHA512:
6
- metadata.gz: 22c21e733489290870522a64a7797654aeffcda6ab8c423e1be7ef9571b291b14a637a61823377559e51d481f941473d6d3ebf9c743191fcb0b4feadf71b6eac
7
- data.tar.gz: 6ca6ecdeff6b0083e39f390ca09853391f5ccaef084a915ac2fd9a8aafa3eb814bc22a340a11bc43ef5a2888002f738238b7b1a777ee4ddf241b5124bf7c73c0
6
+ metadata.gz: 9c29dda2661f328865b4671e5da73d5bfb50a5d68619f9201a72c14aad2fd07f2382c7ddb65ce4a96616e70d0952789055d7e95db1ba5a8547307d69afdfb1c7
7
+ data.tar.gz: 61246d6688a5618bdafb366d56855ad8553c05f8b037ad652b00d698a81db2e53dfc47e5269ef5cdd925509c390fa4a0a197bf6cb0145edbf048c89b39417159
data/.rubocop.yml CHANGED
@@ -30,7 +30,7 @@ Style/CollectionMethods:
30
30
  Enabled: true
31
31
 
32
32
  Metrics/LineLength:
33
- Max: 120
33
+ Max: 160
34
34
 
35
35
  Style/EmptyLinesAroundClassBody:
36
36
  Enabled: false
data/lib/sem/api/base.rb CHANGED
@@ -1,15 +1,13 @@
1
- module Sem
2
- module API
3
- class Base
4
- class << self
5
- def client
6
- @client ||= SemaphoreClient.new(
7
- Sem::Configuration.auth_token,
8
- :api_url => Sem::Configuration.api_url,
9
- :verbose => (Sem.log_level == Sem::LOG_LEVEL_TRACE)
10
- )
11
- end
12
- end
13
- end
1
+ module Sem::API::Base
2
+ module_function
3
+
4
+ def client
5
+ @client ||= SemaphoreClient.new(
6
+ Sem::Configuration.auth_token,
7
+ :api_url => Sem::Configuration.api_url,
8
+ :verbose => (Sem.log_level == Sem::LOG_LEVEL_TRACE),
9
+ :auto_paginate => true
10
+ )
14
11
  end
12
+
15
13
  end
@@ -0,0 +1,7 @@
1
+ class Sem::API::EnvVar < SimpleDelegator
2
+ extend Sem::API::Base
3
+
4
+ def encrypted?
5
+ encrypted == true
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Sem::API::File < SimpleDelegator
2
+ extend Sem::API::Base
3
+
4
+ def encrypted?
5
+ encrypted == true
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ class Sem::API::Org < SimpleDelegator
2
+ extend Sem::API::Base
3
+
4
+ def self.all
5
+ client.orgs.list.map { |org| new(org) }
6
+ end
7
+
8
+ def self.find!(org_name)
9
+ org = client.orgs.get(org_name)
10
+
11
+ raise Sem::Errors::ResourceNotFound.new("Organization", [org_name]) if org.nil?
12
+
13
+ new(org)
14
+ end
15
+
16
+ def users
17
+ Sem::API::Base.client.users.list_for_org(name).map { |user| Sem::API::User.new(user) }
18
+ end
19
+
20
+ def teams
21
+ Sem::API::Base.client.teams.list_for_org(name).map { |team| Sem::API::Team.new(team) }
22
+ end
23
+
24
+ def projects
25
+ Sem::API::Base.client.projects.list_for_org(name).map { |project| Sem::API::Project.new(project) }
26
+ end
27
+
28
+ end
@@ -0,0 +1,50 @@
1
+ class Sem::API::Project < SimpleDelegator
2
+ extend Sem::API::Base
3
+
4
+ def self.all
5
+ Sem::API::Org.all.map do |org|
6
+ client.projects.list_for_org(org.username).map { |project| new(org.username, project) }
7
+ end.flatten
8
+ end
9
+
10
+ def self.find!(project_srn)
11
+ org_name, project_name = Sem::SRN.parse_project(project_srn)
12
+
13
+ # TODO: fix .to_a bug in client
14
+
15
+ project = client.projects.list_for_org(org_name, :name => project_name).to_a.first
16
+
17
+ raise Sem::Errors::ResourceNotFound.new("Project", [org_name, project_name]) if project.nil?
18
+
19
+ new(org_name, project)
20
+ end
21
+
22
+ attr_reader :org_name
23
+
24
+ def initialize(org_name, project)
25
+ @org_name = org_name
26
+
27
+ super(project)
28
+ end
29
+
30
+ def full_name
31
+ "#{@org_name}/#{name}"
32
+ end
33
+
34
+ def teams
35
+ client.teams.list_for_project(id).map { |team| Sem::API::Team.new(org_name, team) }
36
+ end
37
+
38
+ def shared_configs
39
+ Sem::API::Base.client.shared_configs.list_for_project(id).map { |project| Sem::API::SharedConfig.new(org_name, project) }
40
+ end
41
+
42
+ def add_shared_config(shared_config)
43
+ Sem::API::Base.client.shared_configs.attach_to_project(shared_config.id, id)
44
+ end
45
+
46
+ def remove_shared_config(shared_config)
47
+ Sem::API::Base.client.shared_configs.detach_from_project(shared_config.id, id)
48
+ end
49
+
50
+ end
@@ -0,0 +1,109 @@
1
+ class Sem::API::SharedConfig < SimpleDelegator
2
+ extend Sem::API::Base
3
+
4
+ def self.all
5
+ Sem::API::Org.all.pmap do |org|
6
+ client.shared_configs.list_for_org(org.username).map { |config| new(org.username, config) }
7
+ end.flatten
8
+ end
9
+
10
+ def self.find!(shared_config_srn)
11
+ org_name, shared_config_name = Sem::SRN.parse_shared_config(shared_config_srn)
12
+
13
+ configs = client.shared_configs.list_for_org(org_name)
14
+ config = configs.find { |c| c.name == shared_config_name }
15
+
16
+ if config.nil?
17
+ raise Sem::Errors::ResourceNotFound.new("Shared Configuration", [org_name, shared_config_name])
18
+ end
19
+
20
+ new(org_name, config)
21
+ end
22
+
23
+ def self.create!(shared_config_srn, args = {})
24
+ org_name, shared_config_name = Sem::SRN.parse_shared_config(shared_config_srn)
25
+
26
+ shared_config = client.shared_configs.create_for_org(org_name, args.merge(:name => shared_config_name))
27
+
28
+ if shared_config.nil?
29
+ raise Sem::Errors::ResourceNotCreated.new("Shared Configuration", [org_name, shared_config_name])
30
+ end
31
+
32
+ new(org_name, shared_config)
33
+ end
34
+
35
+ attr_reader :org_name
36
+
37
+ def initialize(org_name, shared_config)
38
+ @org_name = org_name
39
+
40
+ super(shared_config)
41
+ end
42
+
43
+ def full_name
44
+ "#{org_name}/#{name}"
45
+ end
46
+
47
+ def add_config_file(args)
48
+ file = Sem::API::Base.client.config_files.create_for_shared_config(id, args)
49
+
50
+ if file.nil?
51
+ raise Sem::Errors::ResourceNotCreated.new("Configuration File", [org_name, path])
52
+ end
53
+
54
+ Sem::API::File.new(file)
55
+ end
56
+
57
+ def remove_config_file(path)
58
+ file = files.find { |f| f.path == path }
59
+
60
+ if file.nil?
61
+ raise Sem::Errors::ResourceNotCreated.new("Configuration File", [org_name, path])
62
+ end
63
+
64
+ Sem::API::Base.client.config_files.delete(file.id)
65
+ end
66
+
67
+ def add_env_var(args)
68
+ env_var = Sem::API::Base.client.env_vars.create_for_shared_config(id, args)
69
+
70
+ if env_var.nil?
71
+ raise Sem::Errors::ResourceNotCreated.new("Environment Variable", [org_name, args[:name]])
72
+ end
73
+
74
+ Sem::API::EnvVar.new(env_var)
75
+ end
76
+
77
+ def remove_env_var(name)
78
+ env_var = env_vars.find { |var| var.name == name }
79
+
80
+ if env_var.nil?
81
+ raise Sem::Errors::ResourceNotCreated.new("Environment Variable", [org_name, name])
82
+ end
83
+
84
+ Sem::API::Base.client.env_vars.delete(env_var.id)
85
+ end
86
+
87
+ def update!(args)
88
+ shared_config = Sem::API::Base.client.shared_configs.update(id, args)
89
+
90
+ if shared_config.nil?
91
+ raise Sem::Errors::ResourceNotUpdated.new("Shared Configuration", [org_name, name])
92
+ end
93
+
94
+ self.class.new(org_name, shared_config)
95
+ end
96
+
97
+ def delete!
98
+ Sem::API::Base.client.shared_configs.delete(id)
99
+ end
100
+
101
+ def files
102
+ Sem::API::Base.client.config_files.list_for_shared_config(id).map { |file| Sem::API::File.new(file) }
103
+ end
104
+
105
+ def env_vars
106
+ Sem::API::Base.client.env_vars.list_for_shared_config(id).map { |env_var| Sem::API::EnvVar.new(env_var) }
107
+ end
108
+
109
+ end
@@ -0,0 +1,90 @@
1
+ class Sem::API::Team < SimpleDelegator
2
+ extend Sem::API::Base
3
+
4
+ def self.all
5
+ Sem::API::Org.all.pmap do |org|
6
+ client.teams.list_for_org(org.username).map { |team| new(org.username, team) }
7
+ end.flatten
8
+ end
9
+
10
+ def self.find!(team_srn)
11
+ org_name, team_name = Sem::SRN.parse_team(team_srn)
12
+
13
+ team = client.teams.list_for_org(org_name).find { |t| t.name == team_name }
14
+
15
+ raise Sem::Errors::ResourceNotFound.new("Team", [org_name, team_name]) if team.nil?
16
+
17
+ new(org_name, team)
18
+ end
19
+
20
+ def self.create!(team_srn, args)
21
+ org_name, team_name = Sem::SRN.parse_team(team_srn)
22
+
23
+ team = client.teams.create_for_org(org_name, args.merge(:name => team_name))
24
+
25
+ raise Sem::Errors::ResourceNotCreated.new("Team", [org_name, team_name]) if team.nil?
26
+
27
+ new(org_name, team)
28
+ end
29
+
30
+ attr_reader :org_name
31
+
32
+ def initialize(org_name, team)
33
+ @org_name = org_name
34
+
35
+ super(team)
36
+ end
37
+
38
+ def full_name
39
+ "#{org_name}/#{name}"
40
+ end
41
+
42
+ def update(args)
43
+ new_team = Sem::API::Base.client.teams.update(id, args)
44
+
45
+ raise Sem::Errors::ResourceNotUpdated.new("Team", [@org_name, name]) if new_team.nil?
46
+
47
+ self.class.new(@org_name, new_team)
48
+ end
49
+
50
+ def add_user(username)
51
+ Sem::API::Base.client.users.attach_to_team(username, id)
52
+ end
53
+
54
+ def remove_user(username)
55
+ Sem::API::Base.client.users.detach_from_team(username, id)
56
+ end
57
+
58
+ def add_project(project)
59
+ Sem::API::Base.client.projects.attach_to_team(project.id, id)
60
+ end
61
+
62
+ def remove_project(project)
63
+ Sem::API::Base.client.projects.detach_from_team(project.id, id)
64
+ end
65
+
66
+ def add_shared_config(config)
67
+ Sem::API::Base.client.shared_configs.attach_to_team(config.id, id)
68
+ end
69
+
70
+ def remove_shared_config(config)
71
+ Sem::API::Base.client.shared_configs.detach_from_team(config.id, id)
72
+ end
73
+
74
+ def delete!
75
+ Sem::API::Base.client.teams.delete(id)
76
+ end
77
+
78
+ def users
79
+ Sem::API::Base.client.users.list_for_team(id).map { |user| Sem::API::User.new(user) }
80
+ end
81
+
82
+ def projects
83
+ Sem::API::Base.client.projects.list_for_team(id).map { |project| Sem::API::Project.new(org_name, project) }
84
+ end
85
+
86
+ def shared_configs
87
+ Sem::API::Base.client.shared_configs.list_for_team(id).map { |config| Sem::API::SharedConfig.new(org_name, config) }
88
+ end
89
+
90
+ end
@@ -0,0 +1,8 @@
1
+ class Sem::API::User < SimpleDelegator
2
+ extend Sem::API::Base
3
+
4
+ def self.all
5
+ client.users.list.map { |user| new(user) }
6
+ end
7
+
8
+ end
data/lib/sem/api.rb CHANGED
@@ -1,13 +1,12 @@
1
1
  module Sem
2
2
  module API
3
- require_relative "api/traits"
4
3
  require_relative "api/base"
5
- require_relative "api/orgs"
6
- require_relative "api/users"
7
- require_relative "api/projects"
8
- require_relative "api/env_vars"
9
- require_relative "api/files"
10
- require_relative "api/shared_configs"
11
- require_relative "api/teams"
4
+ require_relative "api/org"
5
+ require_relative "api/user"
6
+ require_relative "api/project"
7
+ require_relative "api/env_var"
8
+ require_relative "api/file"
9
+ require_relative "api/shared_config"
10
+ require_relative "api/team"
12
11
  end
13
12
  end
data/lib/sem/cli/orgs.rb CHANGED
@@ -2,27 +2,27 @@ class Sem::CLI::Orgs < Dracula
2
2
 
3
3
  desc "list", "list organizations"
4
4
  def list
5
- orgs = Sem::API::Orgs.list
5
+ orgs = Sem::API::Org.all
6
6
 
7
- Sem::Views::Orgs.list(orgs)
7
+ if !orgs.empty?
8
+ Sem::Views::Orgs.list(orgs)
9
+ else
10
+ Sem::Views::Orgs.create_first_org
11
+ end
8
12
  end
9
13
 
10
14
  desc "info", "shows detailed information about an organization"
11
- def info(org)
12
- org_name = Sem::SRN.parse_org(org).first
15
+ def info(org_name)
16
+ org = Sem::API::Org.find!(org_name)
13
17
 
14
- org_instance = Sem::API::Orgs.info(org_name).to_h
15
-
16
- Sem::Views::Orgs.info(org_instance)
18
+ Sem::Views::Orgs.info(org)
17
19
  end
18
20
 
19
21
  desc "members", "list members of an organization"
20
- def members(org)
21
- org_name = Sem::SRN.parse_org(org).first
22
-
23
- users = Sem::API::Users.list_for_org(org_name)
22
+ def members(org_name)
23
+ org = Sem::API::Org.find!(org_name)
24
24
 
25
- Sem::Views::Users.list(users)
25
+ Sem::Views::Users.list(org.users)
26
26
  end
27
27
 
28
28
  end
@@ -2,59 +2,57 @@ class Sem::CLI::Projects < Dracula
2
2
 
3
3
  desc "list", "list projects"
4
4
  def list
5
- projects = Sem::API::Projects.list
5
+ projects = Sem::API::Project.all
6
6
 
7
- Sem::Views::Projects.list(projects)
7
+ if !projects.empty?
8
+ Sem::Views::Projects.list(projects)
9
+ else
10
+ Sem::Views::Projects.setup_first_project
11
+ end
8
12
  end
9
13
 
10
14
  desc "info", "shows detailed information about a project"
11
- def info(project)
12
- org_name, project_name = Sem::SRN.parse_project(project)
13
-
14
- project_instance = Sem::API::Projects.info(org_name, project_name).to_h
15
+ def info(project_name)
16
+ project = Sem::API::Project.find!(project_name)
15
17
 
16
- Sem::Views::Projects.info(project_instance)
18
+ Sem::Views::Projects.info(project)
17
19
  end
18
20
 
19
21
  class SharedConfigs < Dracula
20
- desc "list", "list shared configurations on a project"
21
- def list(project)
22
- org_name, project_name = Sem::SRN.parse_project(project)
23
22
 
24
- configs = Sem::API::SharedConfigs.list_for_project(org_name, project_name)
25
-
26
- Sem::Views::SharedConfigs.list(configs)
23
+ desc "list", "list shared configurations on a project"
24
+ def list(project_name)
25
+ project = Sem::API::Project.find!(project_name)
26
+ shared_configs = project.shared_configs
27
+
28
+ if !shared_configs.empty?
29
+ Sem::Views::SharedConfigs.list(shared_configs)
30
+ else
31
+ Sem::Views::Projects.attach_first_shared_config(project)
32
+ end
27
33
  end
28
34
 
29
35
  desc "add", "attach a shared configuration to a project"
30
- def add(project, shared_config)
31
- project_org_name, project_name = Sem::SRN.parse_project(project)
32
- shared_config_org_name, shared_config_name = Sem::SRN.parse_shared_config(shared_config)
36
+ def add(project_name, shared_config_name)
37
+ project = Sem::API::Project.find!(project_name)
38
+ shared_config = Sem::API::SharedConfig.find!(shared_config_name)
33
39
 
34
- if project_org_name != shared_config_org_name
35
- abort Sem::Views::Projects.org_names_not_matching("project", "shared configuration", project, shared_config)
36
- end
37
-
38
- Sem::API::SharedConfigs.add_to_project(project_org_name, project_name, shared_config_name)
40
+ project.add_shared_config(shared_config)
39
41
 
40
- puts "Shared Configuration #{project_org_name}/#{shared_config_name} added to the project."
42
+ puts "Shared Configuration #{shared_config_name} added to the project."
41
43
  end
42
44
 
43
45
  desc "remove", "removes a shared configuration from the project"
44
- def remove(project, shared_config)
45
- project_org_name, project_name = Sem::SRN.parse_project(project)
46
- shared_config_org_name, shared_config_name = Sem::SRN.parse_shared_config(shared_config)
47
-
48
- if project_org_name != shared_config_org_name
49
- abort Sem::Views::Projects.org_names_not_matching("project", "shared configuration", project, shared_config)
50
- end
46
+ def remove(project_name, shared_config_name)
47
+ project = Sem::API::Project.find!(project_name)
48
+ shared_config = Sem::API::SharedConfig.find!(shared_config_name)
51
49
 
52
- Sem::API::SharedConfigs.remove_from_project(project_org_name, project_name, shared_config_name)
50
+ project.remove_shared_config(shared_config)
53
51
 
54
- puts "Shared Configuration #{project_org_name}/#{shared_config_name} removed from the project."
52
+ puts "Shared Configuration #{shared_config_name} removed from the project."
55
53
  end
54
+
56
55
  end
57
56
 
58
57
  register "shared-configs", "manage shared configurations", SharedConfigs
59
-
60
58
  end