sf_cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: de348eca269f611892a10afe7064a6db06d701cc349d92bf5092543e4372e176
4
+ data.tar.gz: 33057527c8449b9f6a39e88a23bfaa5179084267243501b04dbaa17ce8f7d9c4
5
+ SHA512:
6
+ metadata.gz: b1918b2fca8857f3a7520fec1c7dc0fc596d0ae272997edf24eb4335189a1ea62dd9d2fbeb80ed9dec91341fe2d85f48293d499e4873d3a4ae8fc6d59266b9cc
7
+ data.tar.gz: fae662b7c7534da4c59bed8a3f1dbe8fb4191224b8fdb79810f507bb81f617892ae0b6252dff007512750845c91d90425779fe8306c7fc16d2203fc9bdd24aad
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,38 @@
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
+
@@ -0,0 +1,4 @@
1
+ require_relative './org'
2
+ require_relative './data'
3
+ require_relative './sobject'
4
+ require_relative './project'
@@ -0,0 +1,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(: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
+
@@ -0,0 +1,74 @@
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
@@ -0,0 +1,36 @@
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
data/lib/sf_cli/sf.rb ADDED
@@ -0,0 +1,33 @@
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
data/lib/sf_cli.rb ADDED
@@ -0,0 +1 @@
1
+ require 'sf_cli/sf'
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sf_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Takanobu Maekawa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A thin wrapper classes representing salesforce CLI. Currenty only sf
14
+ command is implemented, but not all functions are supported.
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sf_cli.rb
21
+ - lib/sf_cli/helper_methods.rb
22
+ - lib/sf_cli/sf.rb
23
+ - lib/sf_cli/sf/base.rb
24
+ - lib/sf_cli/sf/data.rb
25
+ - lib/sf_cli/sf/operations.rb
26
+ - lib/sf_cli/sf/org.rb
27
+ - lib/sf_cli/sf/project.rb
28
+ - lib/sf_cli/sf/sobject.rb
29
+ homepage:
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.5.11
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: A wrapper class library for Salesforce CLI
52
+ test_files: []