sf_cli 0.0.2 → 0.0.3

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
  SHA256:
3
- metadata.gz: ea26ff0c7b396c91b431caf812ccc8e8cc702433205082ed68891b67bd82a910
4
- data.tar.gz: 920c35bc989df4230a76f52256c01a98c13c8c9105e9ea7ea6819dd32ffa49b5
3
+ metadata.gz: 3d070d17a818ef2dd2030a026e1e3b14a284eb1e4ce1bab9ed46c8f5811c2169
4
+ data.tar.gz: aa7aafc17f57d6d9df71e6969caad6c756965cfce76ca5583acd73799366b34c
5
5
  SHA512:
6
- metadata.gz: 995c401c9b9eb0d4fb076b70b6e6e74dde07272dbdb85117a767e746cf3f3995322d27b789ecf2df8a2b077ef7c78f9dac0bc5cf8d2ad5112fe50d4c07a5c7d3
7
- data.tar.gz: 82f9bf6f26330c1219de3a9f7660236e8d3db61393957bb476413d86dc0d79d5627d847e0e5d6ed2605ef311efc9961cb6b99e39dac9b0d7c5bb7d4c7c7837a1
6
+ metadata.gz: ddfd91e3ddd1573938d9dbb9c27749eb8054475cee62d5565222f75ad2867fe1abf3dc3c5cc48a452a79991971532f89b233ddf1db491a83b9f28348fdd53eb0
7
+ data.tar.gz: 9df1154ec04b5acefddc469429163feed1ee432abd517048b0915d046c29d9d30a5e097153aa50a5774085720f91c68f86cda484ada9d667a6140cd15bcbe6bb
data/README.rdoc ADDED
@@ -0,0 +1,50 @@
1
+ = Salesforce CLI library
2
+ https://badge.fury.io/rb/sf_cli.png
3
+
4
+ This is a class library for introducing {Salesforce CLI}[https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_top.htm] to Ruby scripting.<br>
5
+ It is designed to be similar usability to the command line interface.<br>
6
+ Currently only *sf* command is the target of development.
7
+
8
+ == prerequisite
9
+ Salesforce CLI must be installed.<br>
10
+ As of as of August in 2024, ver.2.54.6 is the development target.
11
+
12
+ == install
13
+ === Rubygem
14
+ $ gem install sf_cli
15
+
16
+ === Bundler
17
+ in Gemfile:
18
+ gem 'sf_cli'
19
+
20
+ then,
21
+ $ bundle install
22
+
23
+ == Examples
24
+ require 'sf_cli/sf'
25
+
26
+ sf = SfCli::Sf.new
27
+
28
+ # login to org
29
+ sf.org.login_web
30
+ sf.org.login_web target_org: :dev # name an alias to the org, which you're going to log in, for later use. This is needed when you don't use the default org.
31
+
32
+ # get Account records
33
+ sf.data.query 'SELECT Id, Name FROM Account LIMIT 3' # => returns an array containing 3 records
34
+
35
+ Account = Struct.new(:Id, :Name)
36
+ sf.data.query('SELECT Id, Name From Account Limit 3', model_class: Account) # returns an array of Account struct object
37
+
38
+ # generate a Salesforce DX project
39
+ sf.project.generate 'MyProject'
40
+
41
+
42
+ For more command details, see {Salesforce CLI Command Reference}[https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_top.htm]
43
+
44
+ == Documents
45
+ $ git clone https://github.com/tmkw/sf_cli.git
46
+ $ cd sf_cli
47
+ $ bundle install
48
+ $ bundle exec rake rdoc
49
+
50
+ This generates the documents in doc directory.
@@ -1,17 +1,34 @@
1
- require_relative '../helper_methods'
2
-
3
- module SfCli
4
- class Sf
5
- class Base
6
- include ::SfCli::HelperMethods
7
-
8
- attr_reader :target_org
9
-
10
- # *target_org* --- an org alias name, which is used for sf command operations (default is nil). If it is nil, the command uses the default org.
11
- #
12
- def initialize(target_org = nil)
13
- @target_org = target_org
14
- end
15
- end
16
- end
17
- end
1
+ module SfCli
2
+ class Sf
3
+ class Base
4
+ def initialize(_sf)
5
+ @sf = _sf
6
+ end
7
+
8
+ private
9
+
10
+ def exec(action, flags: {}, switches: {}, redirection: nil)
11
+ sf.exec(category, action, flags: flags, switches: switches, redirection: redirection)
12
+ end
13
+
14
+ def category
15
+ self.class.name.split('::').last.downcase
16
+ end
17
+
18
+ def field_value_pairs(hash)
19
+ return nil if hash.nil?
20
+ return nil if hash.empty?
21
+
22
+ hash.each_with_object([]) do|(k,v), arr|
23
+ value = v.instance_of?(String) ? %|'#{v}'| : v
24
+ arr << %(#{k}=#{value})
25
+ end
26
+ .join(' ')
27
+ end
28
+
29
+ def sf
30
+ @sf
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,38 +1,148 @@
1
- require 'json'
2
- require_relative './base'
3
-
4
- module SfCli
5
- class Sf
6
- #
7
- # ==== description
8
- # The class representing *sf* *data*
9
- #
10
- # https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_data_commands_unified.htm
11
- #
12
- class Data < Base
13
-
14
- # get the object records. It's eqivalent to use *sf* *data* *query*
15
- #
16
- # *soql* --- SOQL
17
- #
18
- # *model_class* --- the data model class representing the record object.
19
- #
20
- # ==== examples
21
- # sf.data.query('SELECT Id, Name From Account Limit 3') # returns an array of Hash object
22
- #
23
- # Account = Struct.new(:Id, :Name)
24
- # sf.data.query('SELECT Id, Name From Account Limit 3', model_class: Account) # returns an array of Account struct object
25
- #
26
- def query(soql, model_class: nil)
27
- json = JSON.parse `sf data query --json --query "#{soql}" #{flag :"target-org", target_org} #{null_stderr_redirection}`
28
- raise StandardError.new(%|sf data query: failed. (query: "#{soql}")|) if json['status'] != 0
29
-
30
- json['result']['records'].each_with_object([]) do |h, a|
31
- h.delete "attributes"
32
- a << (model_class ? model_class.new(**h) : h)
33
- end
34
- end
35
- end
36
- end
37
- end
38
-
1
+ require_relative './base'
2
+
3
+ module SfCli
4
+ class Sf
5
+ #
6
+ # ==== description
7
+ # The class representing *sf* *data*
8
+ #
9
+ # https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_data_commands_unified.htm
10
+ #
11
+ class Data < Base
12
+
13
+ # get object records using SQOL. (eqivalent to *sf* *data* *query*)
14
+ #
15
+ # *soql* --- SOQL<br>
16
+ # *target_org* --- an alias of paticular org, not default one<br>
17
+ # *model_class* --- the data model class representing the record object.<br>
18
+ #
19
+ # ==== examples
20
+ # sf.data.query('SELECT Id, Name From Account Limit 3') # returns an array of Hash object
21
+ #
22
+ # Account = Struct.new(:Id, :Name)
23
+ # sf.data.query('SELECT Id, Name From Account Limit 3', model_class: Account) # returns an array of Account struct object
24
+ #
25
+ def query(soql, target_org: nil, model_class: nil)
26
+ flags = {
27
+ :"query" => %("#{soql}"),
28
+ :"target-org" => target_org,
29
+ }
30
+ json = exec(__method__, flags: flags, redirection: :null_stderr)
31
+
32
+ json['result']['records'].each_with_object([]) do |h, a|
33
+ h.delete "attributes"
34
+ a << (model_class ? model_class.new(**h) : h)
35
+ end
36
+ end
37
+
38
+ # get a object record. (eqivalent to *sf* *data* *get* *record*)
39
+ #
40
+ # *object_type* --- Object Type (ex. Account)<br>
41
+ # *record_id* --- id of the object<br>
42
+ # *where* --- hash object that is used to identify a record<br>
43
+ # *target_org* --- an alias of paticular org, not default one<br>
44
+ # *model_class* --- the data model class representing the record object.<br>
45
+ #
46
+ # ==== examples
47
+ # sf.data.get_record :Account, record_id: 'xxxxxxx'
48
+ # sf.data.get_record :Account, where: {Name: 'Jonny B.Good', Country: 'USA'}
49
+ #
50
+ # CustomObject = Struct.new(:Id, :Name)
51
+ # sf.data.get_record :TheCustomObject__c, record_id: 'xxxxx', model_class: CustomObject # returns a CustomObject struct object
52
+ #
53
+ def get_record(object_type, record_id: nil, where: nil, target_org: nil, model_class: nil)
54
+ where_conditions = field_value_pairs(where)
55
+ flags = {
56
+ :"sobject" => object_type,
57
+ :"record-id" => record_id,
58
+ :"where" => (where_conditions.nil? ? nil : %|"#{where_conditions}"|),
59
+ :"target-org" => target_org,
60
+ }
61
+ action = __method__.to_s.tr('_', ' ')
62
+ json = exec(action, flags: flags, redirection: :null_stderr)
63
+
64
+ result = json['result']
65
+ result.delete 'attributes'
66
+
67
+ model_class ? model_class.new(**result) : result
68
+ end
69
+
70
+ # update a object record. (eqivalent to *sf* *data* *update* *record*)
71
+ #
72
+ # *object_type* --- Object Type (ex. Account)<br>
73
+ # *record_id* --- id of the object<br>
74
+ # *where* --- field values that is used to identify a record<br>
75
+ # *values* --- field values for update<br>
76
+ # *target_org* --- an alias of paticular org, not default one<br>
77
+ #
78
+ # ==== examples
79
+ # sf.data.update_record :Account, record_id: 'xxxxxxx', values: {Name: 'New Account Name'}
80
+ # sf.data.update_record :Hoge__c, where: {Name: 'Jonny B.Good', Country: 'USA'}, values: {Phone: 'xxxxx', bar: 2000}
81
+ #
82
+ def update_record(object_type, record_id: nil, where: nil, values: nil, target_org: nil)
83
+ where_conditions = field_value_pairs(where)
84
+ field_values = field_value_pairs(values)
85
+ flags = {
86
+ :"sobject" => object_type,
87
+ :"record-id" => record_id,
88
+ :"where" => (where_conditions.nil? ? nil : %|"#{where_conditions}"|),
89
+ :"values" => (field_values.nil? ? nil : %|"#{field_values}"|),
90
+ :"target-org" => target_org,
91
+ }
92
+ action = __method__.to_s.tr('_', ' ')
93
+ json = exec(action, flags: flags, redirection: :null_stderr)
94
+
95
+ json['result']['id']
96
+ end
97
+
98
+ # create a object record. (eqivalent to *sf* *data* *create* *record*)
99
+ #
100
+ # *object_type* --- Object Type (ex. Account)<br>
101
+ # *values* --- field values to be assigned<br>
102
+ # *target_org* --- an alias of paticular org, not default one<br>
103
+ #
104
+ # ==== examples
105
+ #
106
+ # sf.data.create_record :TheCustomObject__c, values: {Name: "John Smith", Age: 33} # creating a TheCustomObject record with name and age
107
+ #
108
+ def create_record(object_type, values: {}, target_org: nil)
109
+ field_values = field_value_pairs(values)
110
+ flags = {
111
+ :"sobject" => object_type,
112
+ :"values" => (field_values.nil? ? nil : %|"#{field_values}"|),
113
+ :"target-org" => target_org,
114
+ }
115
+ action = __method__.to_s.tr('_', ' ')
116
+ json = exec(action, flags: flags, redirection: :null_stderr)
117
+
118
+ json['result']['id']
119
+ end
120
+
121
+ # delete a object record. (eqivalent to *sf* *data* *delete* *record*)
122
+ #
123
+ # *object_type* --- Object Type (ex. Account)<br>
124
+ # *record_id* --- id of the object<br>
125
+ # *where* --- hash object that is used to identify a record<br>
126
+ # *target_org* --- an alias of paticular org, not default one<br>
127
+ #
128
+ # ==== examples
129
+ # sf.data.delete_record :Hoge__c, record_id: 'xxxxxxx'
130
+ # sf.data.delete_record :Hoge__c, where: {Name: 'Jonny B.Good', Country: 'USA'}
131
+ #
132
+ #
133
+ def delete_record(object_type, record_id: nil, where: nil, target_org: nil)
134
+ where_conditions = field_value_pairs(where)
135
+ flags = {
136
+ :"sobject" => object_type,
137
+ :"record-id" => record_id,
138
+ :"where" => (where_conditions.nil? ? nil : %|"#{where_conditions}"|),
139
+ :"target-org" => target_org,
140
+ }
141
+ action = __method__.to_s.tr('_', ' ')
142
+ json = exec(action, flags: flags, redirection: :null_stderr)
143
+
144
+ json['result']['id']
145
+ end
146
+ end
147
+ end
148
+ end
data/lib/sf_cli/sf/org.rb CHANGED
@@ -1,56 +1,49 @@
1
- require_relative './base'
2
-
3
- module SfCli
4
- class Sf
5
- #
6
- # ==== description
7
- # The class representing *sf* *org*.
8
- #
9
- # https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_org_commands_unified.htm
10
- #
11
- class Org < Base
12
- ConnectionInfo = Struct.new(:access_token, :alias, :instance_url, :api_version, :status)
13
-
14
- # login to the org by the browser. It's equivalent to use *sf* *org* *login* *web*
15
- #
16
- # *instance_url* --- custom login url.
17
- #
18
- def login_web(instance_url: nil)
19
- `sf org login web #{flag :alias, target_org} #{flag :"instance-url", instance_url}`
20
- end
21
-
22
- #
23
- # returns the org's connection information. It's equivalent to use *sf* *org* *display*.
24
- #
25
- def display
26
- lines = StringIO.new(`sf org display #{flag :"target-org", target_org} #{null_stderr_redirection}`).readlines
27
-
28
- connection_info = ConnectionInfo.new(
29
- access_token: nil,
30
- alias: nil,
31
- instance_url: nil,
32
- api_version: nil,
33
- status: nil
34
- )
35
-
36
- lines.each do |line|
37
- case line
38
- when /Access Token/
39
- connection_info.access_token = line.split(' ')[2]
40
- when /Alias/
41
- connection_info.alias = line.split(' ')[1]
42
- when /Instance Url/
43
- connection_info.instance_url = line.split(' ')[2]
44
- when /Api Version/
45
- connection_info.api_version = line.split(' ')[2]
46
- when /Connected Status/
47
- connection_info.status = line.split(' ')[2]
48
- end
49
- end
50
-
51
- connection_info
52
- end
53
- end
54
- end
55
- end
56
-
1
+ require_relative './base'
2
+
3
+ module SfCli
4
+ class Sf
5
+ #
6
+ # ==== description
7
+ # The class representing *sf* *org*.
8
+ #
9
+ # https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_org_commands_unified.htm
10
+ #
11
+ class Org < Base
12
+ ConnectionInfo = Struct.new(:id, :access_token, :alias, :instance_url, :user_name, :api_version, :status)
13
+
14
+ # login to the org by the browser. (equivalent to *sf* *org* *login* *web*)
15
+ #
16
+ # *target_org* --- an alias of paticular org, not default one<br>
17
+ # *instance_url* --- custom login url.
18
+ #
19
+ def login_web(target_org: nil, instance_url: nil)
20
+ flags = {
21
+ :"alias" => target_org,
22
+ :"instance-url" => instance_url,
23
+ }
24
+ action = __method__.to_s.tr('_', ' ')
25
+ exec(action, flags: flags)
26
+ end
27
+
28
+ #
29
+ # returns the org's connection information. (equivalent to *sf* *org* *display*)
30
+ #
31
+ # *target_org* --- an alias of paticular org, not default one<br>
32
+ #
33
+ def display(target_org: nil)
34
+ flags = {:"target-org" => target_org}
35
+ json = exec(__method__, flags: flags, redirection: :null_stderr)
36
+
37
+ ConnectionInfo.new(
38
+ id: json['result']['id'],
39
+ access_token: json['result']['accessToken'],
40
+ alias: json['result']['alias'],
41
+ instance_url: json['result']['instanceUrl'],
42
+ user_name: json['result']['username'],
43
+ api_version: json['result']['apiVersion'],
44
+ status: json['result']['connectedStatus']
45
+ )
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,74 +1,68 @@
1
- require 'pathname'
2
- require_relative './base'
3
-
4
- module SfCli
5
- class Sf
6
- # ==== description
7
- # The class representing *sf* *project*
8
- #
9
- # command reference: https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_project_commands_unified.htm
10
- #
11
- class Project < Base
12
-
13
- #
14
- # generate a Salesforce project. It's equivalent to *sf* *project* *generate*.
15
- #
16
- # *name* --- project name
17
- #
18
- # *template* --- project template name
19
- #
20
- # *output_dir* --- output directory
21
- #
22
- def generate(name, template: 'standard', output_dir: nil)
23
- `sf project generate #{flag :name, name} #{flag :template, template} #{flag :"output-dir", output_dir} --manifest #{null_stderr_redirection}`
24
- raise StandardError.new(%|command 'sf project generate' failed.|) unless $?.success?
25
- end
26
-
27
- # generate the manifest file of a Salesforce project. It's equivalent to *sf* *project* *generate* *manifest*
28
- #
29
- # *metadata* --- an array that consists of metadata type like CustomObject, Layout and so on. (default: [])
30
- #
31
- # *api_verson* --- api version (default: nil)
32
- #
33
- # *project_dir* --- project directory which you want to create the manifest (default: nil)
34
- #
35
- # *output_dir* --- manifest's output directory in the project directory. You can use relative path from the project root (default: nil)
36
- #
37
- # *from_org* --- username or alias of the org that contains the metadata components from which to build a manifest (default: nil)
38
- #
39
- # *source_dir* --- paths to the local source files to include in the manifest (default: nil)
40
- #
41
- # ==== examples
42
- # sf.project.generate_manifest metadata: %w[CustomObject Layout] # creates a package.xml, which is initialized with CustomObject and Layout
43
- # sf.project.generate_manifest from_org: <org_name> # creates a package.xml, which is initialized with all metadata types in the org
44
- #
45
- def generate_manifest(metadata: [], api_version: nil, project_dir: nil, output_dir: nil, from_org: nil, source_dir: nil)
46
- raise StandardError.new('Exactly one of the following must be provided: --from-org, --metadata, --source-dir') if metadata.empty? && from_org.nil? && source_dir.nil?
47
- base_dir = Dir.pwd
48
-
49
- if project_dir
50
- raise StandardError.new('the project directory not found') unless FileTest.exist?(project_dir) && FileTest.directory?(project_dir)
51
- Dir.chdir project_dir
52
- end
53
-
54
- Dir.mkdir(output_dir) if output_dir && FileTest.exist?(output_dir) == false
55
-
56
- metdata_flags = metadata.empty? ? '' : metadata.map{|md| %|--metadata #{md}|}.join(' ')
57
- cmd =
58
- %|sf project generate manifest %{metdata} %{output_dir} %{api_version} %{from_org} %{source_dir}| %
59
- {
60
- metdata: metdata_flags,
61
- output_dir: flag(:"output-dir", output_dir),
62
- api_version: flag(:"api-version", api_version),
63
- from_org: flag(:"from-org", from_org),
64
- source_dir: flag(:"source-dir", source_dir),
65
- }
66
- `#{cmd} #{null_stderr_redirection}`
67
-
68
- raise StandardError.new(%|command 'sf project generate manifest' failed.|) unless $?.success?
69
- ensure
70
- Dir.chdir base_dir
71
- end
72
- end
73
- end
74
- end
1
+ require_relative './base'
2
+
3
+ module SfCli
4
+ class Sf
5
+ # ==== description
6
+ # The class representing *sf* *project*
7
+ #
8
+ # command reference: https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_project_commands_unified.htm
9
+ #
10
+ class Project < Base
11
+ GenerateResult = Struct.new(:output_dir, :files, :raw_output, :warnings)
12
+
13
+ #
14
+ # generate a Salesforce project. (equivalent to *sf* *project* *generate*)
15
+ #
16
+ # *name* --- project name<br>
17
+ # *template* --- project template name<br>
18
+ # *output_dir* --- output directory<br>
19
+ # *manifest* --- switch to create manifest file in the project directory (manifest/package.xml). default: false
20
+ #
21
+ def generate(name, manifest: false, template: nil, output_dir: nil)
22
+ flags = {
23
+ :name => name,
24
+ :template => template,
25
+ :"output-dir" => output_dir,
26
+ }
27
+ switches = {
28
+ manifest: manifest,
29
+ }
30
+ json = exec(__method__, flags: flags, switches: switches, redirection: :null_stderr)
31
+
32
+ GenerateResult.new(
33
+ output_dir: json['result']['outputDir'],
34
+ files: json['result']['created'],
35
+ raw_output: json['result']['rawOutput'],
36
+ warnings: json['warnings']
37
+ )
38
+ end
39
+
40
+ # generate the manifest file of a Salesforce project. (equivalent to *sf* *project* *generate* *manifest*)
41
+ #
42
+ # *metadata* --- an array that consists of metadata type like CustomObject, Layout and so on. (default: [])<br>
43
+ # *api_verson* --- api version (default: nil)<br>
44
+ # *output_dir* --- manifest's output directory in the project directory. You can use relative path from the project root (default: nil)<br>
45
+ # *from_org* --- username or alias of the org that contains the metadata components from which to build a manifest (default: nil)<br>
46
+ # *source_dir* --- paths to the local source files to include in the manifest (default: nil)
47
+ #
48
+ # ==== examples
49
+ # sf.project.generate_manifest metadata: %w[CustomObject Layout] # creates a package.xml, which is initialized with CustomObject and Layout
50
+ # sf.project.generate_manifest from_org: <org_name> # creates a package.xml, which is initialized with all metadata types in the org
51
+ #
52
+ def generate_manifest(name: nil, output_dir: nil, api_version: nil, metadata: [], from_org: nil, source_dir: nil)
53
+ flags = {
54
+ :name => name,
55
+ :"metadata" => (metadata.empty? ? nil : metadata.join(' ')),
56
+ :"from-org" => from_org,
57
+ :"source-dir" => source_dir,
58
+ :"output-dir" => output_dir,
59
+ :"api-version" => api_version,
60
+ }
61
+ action = __method__.to_s.tr('_', ' ')
62
+ json = exec(action, flags: flags, redirection: :null_stderr)
63
+
64
+ json['result']['path']
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,36 +1,41 @@
1
- require 'json'
2
- require_relative './base'
3
-
4
- module SfCli
5
- class Sf
6
- # ==== description
7
- # The class representing *sf* *sobject*
8
- #
9
- # command reference: https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_sobject_commands_unified.htm
10
- #
11
- class SObject < Base
12
-
13
- # get the schema information of a Salesforce object and returns a hash object. It's equivalent to use *sf* *sobject* *describe*
14
- #
15
- # *objectType* --- object type (ex: Account)
16
- #
17
- def describe(objectType)
18
- json = JSON.parse `sf sobject describe --json --sobject #{objectType} #{flag :"target-org", target_org} #{null_stderr_redirection}`
19
- raise StandardError.new(%|sf sobject describe: failed. (sobject: #{objectType})|) if json['status'] != 0
20
-
21
- json['result']
22
- end
23
-
24
- # returns a list of Salesforce object name (object's API name). It's equivalent to *sf* *sobject* *list*
25
- #
26
- # *object_type* --- all or custom (default: all)
27
- #
28
- def list(object_type: 'all')
29
- json = JSON.parse `sf sobject list --json --sobject #{object_type} #{flag :"target-org", target_org} #{null_stderr_redirection}`
30
- raise StandardError.new(%|sf sobject list: failed.|) if json['status'] != 0
31
-
32
- json['result']
33
- end
34
- end
35
- end
36
- end
1
+ require_relative './base'
2
+
3
+ module SfCli
4
+ class Sf
5
+ # ==== description
6
+ # The class representing *sf* *sobject*
7
+ #
8
+ # command reference: https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_sobject_commands_unified.htm
9
+ #
10
+ class Sobject < Base
11
+
12
+ # returns a hash object containing the Salesforce object schema. (equivalent to *sf* *sobject* *describe*)
13
+ #
14
+ # *objectType* --- object type (ex: Account)<br>
15
+ # *target_org* --- an alias of paticular org, not default one<br>
16
+ #
17
+ def describe(object_type, target_org: nil)
18
+ flags = {
19
+ :"sobject" => object_type,
20
+ :"target-org" => target_org,
21
+ }
22
+ json = exec(__method__, flags: flags, redirection: :null_stderr)
23
+ json['result']
24
+ end
25
+
26
+ # returns a list of Salesforce object API name. (equivalent to *sf* *sobject* *list*)
27
+ #
28
+ # *object_type* --- all or custom<br>
29
+ # *target_org* --- an alias of paticular org, not default one<br>
30
+ #
31
+ def list(object_type, target_org: nil)
32
+ flags = {
33
+ :"sobject" => (object_type.to_sym == :custom ? :custom : :all),
34
+ :"target-org" => target_org,
35
+ }
36
+ json = exec(__method__, flags: flags, redirection: :null_stderr)
37
+ json['result']
38
+ end
39
+ end
40
+ end
41
+ end
data/lib/sf_cli/sf.rb CHANGED
@@ -1,33 +1,87 @@
1
- require_relative './sf/operations'
2
-
3
- module SfCli
4
- # ==== description
5
- # The main object class of *sf* command.
6
- #
7
- #
8
- # https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_unified.htm
9
- #
10
- # ==== examples
11
- # sf = SfCli::Sf.new # use default org
12
- # sf = SfCli::Sf.new target_org: 'hoge' # use an alias "hoge" as target org
13
- #
14
- # # get the org connection infomation, which is equivalent to the result of 'sf org display'
15
- # sf.org.display
16
- #
17
- # # get Account records (equivalent to 'sf data query')
18
- # sf.data.query 'SELECT Id, Name FROM Account LIMIT 3' # => returns an array containing 3 records
19
- #
20
- class Sf
21
- attr_reader :target_org, :org, :data, :sobject, :project
22
-
23
- #
24
- # *\target_org* --- an org alias name, which is used for sf command operations (default is nil). If it is nil, the command uses the default org.
25
- #
26
- def initialize(target_org: nil)
27
- @org = Org.new(target_org)
28
- @data = Data.new(target_org)
29
- @sobject = SObject.new(target_org)
30
- @project = Project.new
31
- end
32
- end
33
- end
1
+ require 'json'
2
+
3
+ module SfCli
4
+ # ==== description
5
+ # The main class of *sf* command.
6
+ #
7
+ # https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_unified.htm
8
+ #
9
+ # ==== examples
10
+ # sf = SfCli::Sf.new # use default org
11
+ #
12
+ # # get the org connection infomation, as same as 'sf org display'
13
+ # sf.org.display
14
+ #
15
+ # # get Account records (equivalent to 'sf data query')
16
+ # sf.data.query 'SELECT Id, Name FROM Account LIMIT 3' # => returns an array containing 3 records
17
+ #
18
+ class Sf
19
+ OPERATION_CATEGORIES = %w[Org Sobject Data Project]
20
+
21
+ # load each operation class and define as a attribute
22
+ OPERATION_CATEGORIES.each do |category|
23
+ require_relative %(sf/#{category.downcase})
24
+ attr_reader category.downcase.to_sym
25
+ end
26
+
27
+ attr_reader :varbose
28
+
29
+ def initialize
30
+ OPERATION_CATEGORIES.each do |category|
31
+ instance_variable_set(:"@#{category.downcase}", Object.const_get(%|::SfCli::Sf::#{category}|).new(self))
32
+ end
33
+ end
34
+
35
+ def exec(category, action, flags: {}, switches: {}, redirection: nil)
36
+ cmd = %|sf #{category} #{action}#{as_flag_options(flags)}#{as_switch_options(switches)}#{redirect redirection}|
37
+
38
+ puts cmd if varbose
39
+
40
+ json = JSON.parse `#{cmd}`
41
+
42
+ puts json if varbose
43
+
44
+ raise StandardError.new(json['message']) if json['status'] != 0
45
+
46
+ json
47
+ end
48
+
49
+ private
50
+
51
+ def as_flag_options(hash)
52
+ flag_options = hash.map{|k,v| flag k, v}.reject(&:nil?).join(' ')
53
+ flag_options = ' ' + flag_options unless flag_options.empty?
54
+
55
+ flag_options
56
+ end
57
+
58
+ def as_switch_options(hash)
59
+ ' ' + {json: true}.merge(hash).each_with_object([]){|(k,v), arr| arr << %(--#{k}) if v}.join(' ')
60
+ end
61
+
62
+ def flag(name, arg)
63
+ arg ? %(--#{name} #{arg}) : nil
64
+ end
65
+
66
+ def os
67
+ @os ||= ENV['OS']
68
+ end
69
+
70
+ def redirect(option)
71
+ case option
72
+ when :null_stderr
73
+ null_stderr_redirection
74
+ else
75
+ end
76
+ end
77
+
78
+ def null_stderr_redirection
79
+ @null_stderr_redirection ||=
80
+ if os.eql?('Windows_NT')
81
+ ' 2>nul'
82
+ else
83
+ ' 2> /dev/null'
84
+ end
85
+ end
86
+ end
87
+ end
metadata CHANGED
@@ -1,35 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sf_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takanobu Maekawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-17 00:00:00.000000000 Z
11
+ date: 2024-08-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A thin wrapper classes representing salesforce CLI. Currenty only sf
14
- command is implemented, but not all functions are supported.
13
+ description: This is a class library for introducing Salesforce CLI.Currenty only
14
+ sf command is the target of development.
15
15
  email:
16
16
  executables: []
17
17
  extensions: []
18
- extra_rdoc_files: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
19
20
  files:
21
+ - README.rdoc
20
22
  - lib/sf_cli.rb
21
- - lib/sf_cli/helper_methods.rb
22
23
  - lib/sf_cli/sf.rb
23
24
  - lib/sf_cli/sf/base.rb
24
25
  - lib/sf_cli/sf/data.rb
25
- - lib/sf_cli/sf/operations.rb
26
26
  - lib/sf_cli/sf/org.rb
27
27
  - lib/sf_cli/sf/project.rb
28
28
  - lib/sf_cli/sf/sobject.rb
29
29
  homepage: https://github.com/tmkw/sf_cli
30
30
  licenses:
31
31
  - MIT
32
- metadata: {}
32
+ metadata:
33
+ homepage_url: https://github.com/tmkw/sf_cli
33
34
  post_install_message:
34
35
  rdoc_options: []
35
36
  require_paths:
@@ -38,15 +39,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
39
  requirements:
39
40
  - - ">="
40
41
  - !ruby/object:Gem::Version
41
- version: '0'
42
+ version: 3.3.3
42
43
  required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  requirements:
44
45
  - - ">="
45
46
  - !ruby/object:Gem::Version
46
47
  version: '0'
47
- requirements: []
48
+ requirements:
49
+ - 'Salesforce CLI ( >= 2.54.0): https://developer.salesforce.com/tools/salesforcecli'
48
50
  rubygems_version: 3.5.11
49
51
  signing_key:
50
52
  specification_version: 4
51
- summary: A wrapper class library for Salesforce CLI
53
+ summary: A library for using Salesforce CLI in Ruby
52
54
  test_files: []
@@ -1,26 +0,0 @@
1
- module SfCli
2
- module HelperMethods
3
- def os
4
- @os ||= ENV['OS']
5
- end
6
-
7
- def null_stderr_redirection
8
- @null_stderr_redirection ||=
9
- if os.nil? || os.eql?('Windows_NT')
10
- '2>nul'
11
- else
12
- '2> dev/null'
13
- end
14
- end
15
-
16
- def flag(name, arg = nil, use_argument: true, long_flag: true)
17
- flag_indicator = long_flag ? '--' : '-'
18
-
19
- if use_argument
20
- arg ? %(#{flag_indicator}#{name} #{arg}) : ''
21
- else
22
- %(#{flag_indicator}#{name})
23
- end
24
- end
25
- end
26
- end
@@ -1,4 +0,0 @@
1
- require_relative './org'
2
- require_relative './data'
3
- require_relative './sobject'
4
- require_relative './project'