Skip to content

Commit 7fc3287

Browse files
committed
Add mix format config, run format check for CI
1 parent a53466a commit 7fc3287

File tree

18 files changed

+205
-142
lines changed

18 files changed

+205
-142
lines changed

.formatter.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["mix.exs", "{lib,test}/**/*.{ex,exs}"]
4+
]

.github/workflows/ci.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ jobs:
2020
- name: Install dependencies
2121
run: mix deps.get
2222

23-
# TODO: Enable in next release
24-
#
25-
# At the moment, we still have pending PRs etc.
26-
# and introducing auto-formatting now would cause
27-
# a ton of conflicts.
28-
#
29-
# - name: Check mix format
30-
# run: mix format --check-formatted
23+
- name: Check mix format
24+
run: mix format --check-formatted
3125

32-
- name: Compile with warnings as errors
33-
run: mix compile --warnings-as-errors
26+
# TODO: Enable check before finalizing release
27+
# - name: Compile with warnings as errors
28+
# run: mix compile --warnings-as-errors
3429

3530
analysis:
3631
name: Run static code analysis
@@ -71,7 +66,6 @@ jobs:
7166
elixir: "1.6.6"
7267
- erlang: "18.3"
7368
elixir: "1.5.3"
74-
7569
steps:
7670
- uses: actions/checkout@v1
7771

lib/sshkit.ex

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ defmodule SSHKit do
4646
# TODO: Separate options for open/exec/recv
4747
Stream.resource(
4848
fn ->
49-
{:ok, chan} = Channel.open(conn, options) # TODO: handle {:error, reason} and raise custom error struct?
50-
:success = Channel.exec(chan, command) # TODO: timeout?, TODO: Handle :failure and {:error, reason} and raise custom error struct?
49+
# TODO: handle {:error, reason} and raise custom error struct?
50+
{:ok, chan} = Channel.open(conn, options)
51+
52+
# TODO: timeout?, TODO: Handle :failure and {:error, reason} and raise custom error struct?
53+
:success = Channel.exec(chan, command)
5154
chan
5255
end,
5356
fn chan ->
54-
{:ok, msg} = Channel.recv(chan) # TODO: timeout?, TODO: handle {:error, reason} and raise custom error struct?
57+
# TODO: timeout?, TODO: handle {:error, reason} and raise custom error struct?
58+
{:ok, msg} = Channel.recv(chan)
5559

5660
# TODO: Adjust channel window size?
5761

@@ -115,16 +119,18 @@ defmodule SSHKit do
115119
def run!(conn, command, options \\ []) do
116120
stream = exec!(conn, command, options)
117121

118-
{status, output} = Enum.reduce(stream, {nil, []}, fn
119-
{:stdout, _, data}, {status, output} -> {status, [{:stdout, data} | output]}
120-
{:stderr, _, data}, {status, output} -> {status, [{:stderr, data} | output]}
121-
{:exit, _, status}, {_, output} -> {status, output}
122-
_, acc -> acc
123-
end)
122+
{status, output} =
123+
Enum.reduce(stream, {nil, []}, fn
124+
{:stdout, _, data}, {status, output} -> {status, [{:stdout, data} | output]}
125+
{:stderr, _, data}, {status, output} -> {status, [{:stderr, data} | output]}
126+
{:exit, _, status}, {_, output} -> {status, output}
127+
_, acc -> acc
128+
end)
124129

125130
output = Enum.reverse(output)
126131

127-
if status != 0, do: raise "Non-zero exit code: #{status}" # TODO: Proper file struct?
132+
# TODO: Proper file struct?
133+
if status != 0, do: raise("Non-zero exit code: #{status}")
128134

129135
output
130136
end

lib/sshkit/channel.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ defmodule SSHKit.Channel do
117117

118118
def send(channel, type, data, timeout) do
119119
Enum.reduce_while(data, :ok, fn
120-
(datum, :ok) -> {:cont, send(channel, type, datum, timeout)}
121-
(_, err) -> {:halt, err}
120+
datum, :ok -> {:cont, send(channel, type, datum, timeout)}
121+
_, err -> {:halt, err}
122122
end)
123123
end
124124

lib/sshkit/connection.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@ defmodule SSHKit.Connection do
3838
Returns `{:ok, conn}` on success, `{:error, reason}` otherwise.
3939
"""
4040
def open(host, options \\ [])
41+
4142
def open(nil, _) do
4243
{:error, "No host given."}
4344
end
45+
4446
def open(host, options) when is_binary(host) do
4547
open(to_charlist(host), options)
4648
end
49+
4750
def open(host, options) do
4851
{details, opts} = extract(options)
4952

50-
port = details[:port]
53+
port = details[:port]
5154
timeout = details[:timeout]
52-
impl = details[:impl]
55+
impl = details[:impl]
5356

5457
case impl.connect(host, port, opts, timeout) do
5558
{:ok, ref} -> {:ok, new(host, port, opts, ref, impl)}

lib/sshkit/context.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule SSHKit.Context do
1414

1515
import SSHKit.Utils
1616

17-
defstruct [env: nil, path: nil, umask: nil, user: nil, group: nil]
17+
defstruct env: nil, path: nil, umask: nil, user: nil, group: nil
1818

1919
@type t() :: %__MODULE__{}
2020

@@ -207,12 +207,19 @@ defmodule SSHKit.Context do
207207
end
208208

209209
defp add(command, :sudo, nil, nil), do: command
210-
defp add(command, :sudo, username, nil), do: "sudo -H -n -u #{username} -- sh -c #{shellquote(command)}"
211-
defp add(command, :sudo, nil, groupname), do: "sudo -H -n -g #{groupname} -- sh -c #{shellquote(command)}"
212-
defp add(command, :sudo, username, groupname), do: "sudo -H -n -u #{username} -g #{groupname} -- sh -c #{shellquote(command)}"
210+
211+
defp add(command, :sudo, username, nil),
212+
do: "sudo -H -n -u #{username} -- sh -c #{shellquote(command)}"
213+
214+
defp add(command, :sudo, nil, groupname),
215+
do: "sudo -H -n -g #{groupname} -- sh -c #{shellquote(command)}"
216+
217+
defp add(command, :sudo, username, groupname),
218+
do: "sudo -H -n -u #{username} -g #{groupname} -- sh -c #{shellquote(command)}"
213219

214220
defp add(command, :export, nil), do: command
215221
defp add(command, :export, env) when env == %{}, do: command
222+
216223
defp add(command, :export, env) do
217224
exports = Enum.map_join(env, " ", fn {name, value} -> "#{name}=\"#{value}\"" end)
218225
"(export #{exports} && #{command})"

lib/sshkit/download.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule SSHKit.Download do
1717
end
1818

1919
def stop(%__MODULE__{channel: nil} = download), do: {:ok, download}
20+
2021
def stop(%__MODULE__{channel: chan} = download) do
2122
with :ok <- Channel.stop(chan) do
2223
{:ok, %{download | channel: nil}}

lib/sshkit/upload.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ defmodule SSHKit.Upload do
2828

2929
defp prepare(%__MODULE__{source: source, options: options} = upload) do
3030
if !Keyword.get(options, :recursive, false) && File.dir?(source) do
31-
{:error, "Option :recursive not specified, but local file is a directory (#{source})"} # TODO: Better error
31+
# TODO: Better error
32+
{:error, "Option :recursive not specified, but local file is a directory (#{source})"}
3233
else
3334
{:ok, %{upload | cwd: Path.dirname(source), stack: [[Path.basename(source)]]}}
3435
end
3536
end
3637

3738
def stop(%__MODULE__{channel: nil} = upload), do: {:ok, upload}
39+
3840
def stop(%__MODULE__{channel: chan} = upload) do
3941
with :ok <- Channel.stop(chan) do
4042
{:ok, %{upload | channel: nil}}

lib/sshkit/utils.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ defmodule SSHKit.Utils do
88
def charlistify(value) when is_list(value) do
99
Enum.map(value, &charlistify/1)
1010
end
11+
1112
def charlistify(value) when is_tuple(value) do
1213
value
1314
|> Tuple.to_list()
1415
|> charlistify()
1516
|> List.to_tuple()
1617
end
18+
1719
def charlistify(value) when is_binary(value) do
1820
to_charlist(value)
1921
end
22+
2023
def charlistify(value) do
2124
value
2225
end

mix.exs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,47 @@ defmodule SSHKit.Mixfile do
66
@source "https://github.com/bitcrowd/sshkit.ex"
77

88
def project do
9-
[app: :sshkit,
10-
name: @name,
11-
version: @version,
12-
elixir: "~> 1.5",
13-
elixirc_paths: elixirc_paths(Mix.env),
14-
build_embedded: Mix.env == :prod,
15-
start_permanent: Mix.env == :prod,
16-
source_url: @source,
17-
docs: [source_ref: "v#{@version}", main: "readme", extras: ["README.md"]],
18-
description: description(),
19-
deps: deps(),
20-
package: package()]
9+
[
10+
app: :sshkit,
11+
name: @name,
12+
version: @version,
13+
elixir: "~> 1.5",
14+
elixirc_paths: elixirc_paths(Mix.env()),
15+
build_embedded: Mix.env() == :prod,
16+
start_permanent: Mix.env() == :prod,
17+
source_url: @source,
18+
docs: [source_ref: "v#{@version}", main: "readme", extras: ["README.md"]],
19+
description: description(),
20+
deps: deps(),
21+
package: package()
22+
]
2123
end
2224

2325
def application do
2426
[extra_applications: [:logger, :ssh]]
2527
end
2628

2729
defp deps do
28-
[{:credo, "~> 1.5", runtime: false, only: [:dev, :test]},
29-
{:ex_doc, "~> 0.23.0", runtime: false, only: [:dev]},
30-
{:inch_ex, "~> 2.0", runtime: false, only: [:dev, :test]},
31-
{:mox, "~> 1.0", only: [:test]}]
30+
[
31+
{:credo, "~> 1.5", runtime: false, only: [:dev]},
32+
{:ex_doc, "~> 0.23.0", runtime: false, only: [:dev]},
33+
{:inch_ex, "~> 2.0", runtime: false, only: [:dev]},
34+
{:mox, "~> 1.0", only: [:test]}
35+
]
3236
end
3337

3438
defp description do
3539
"A toolkit for performing tasks on one or more servers, built on top of Erlang’s SSH application"
3640
end
3741

3842
defp package do
39-
[maintainers: ["bitcrowd", "Paul Meinhardt", "Paulo Diniz", "Philipp Tessenow"],
40-
licenses: ["MIT"],
41-
links: %{"GitHub" => @source}]
43+
[
44+
maintainers: ["bitcrowd", "Paul Meinhardt", "Paulo Diniz", "Philipp Tessenow"],
45+
licenses: ["MIT"],
46+
links: %{"GitHub" => @source}
47+
]
4248
end
4349

4450
defp elixirc_paths(:test), do: ["lib", "test/support"]
45-
defp elixirc_paths(_), do: ["lib"]
51+
defp elixirc_paths(_), do: ["lib"]
4652
end

0 commit comments

Comments
 (0)