Skip to content

Commit

Permalink
Merge pull request #4 from aditya7iyengar/feature/wrap_around_c
Browse files Browse the repository at this point in the history
Feature/wrap around ruby
  • Loading branch information
thebugcatcher authored Jul 8, 2018
2 parents c42a276 + 62c9a1e commit bdbd0b0
Show file tree
Hide file tree
Showing 20 changed files with 667 additions and 369 deletions.
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
elixir 1.5.2
erlang 21.0
elixir 1.6.6
2 changes: 2 additions & 0 deletions adifier/askpass.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
zenity --password --title=Authentication
31 changes: 14 additions & 17 deletions adifier/lib/adifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ defmodule Adifier do
Documentation for Adifier.
"""

@defaultmods ~w(PackageManagerUpdater ToolsInstaller Configurations)
@default_os :ubuntu
@appliers ~w(PackageManagerUpdater ToolsInstaller Configurations)
@default_os "ubuntu"

def adify(on: os, apply: mods) do
mods
|> modules()
|> Enum.each(&apply(&1, :run, [os || @default_os]))
end
def adify(opts) do
os = opts
|> Keyword.get(:os, @default_os)
|> String.to_atom()

defp modules(nil), do: modules(@defaultmods)
defp modules(concatenatedmods) when is_binary(concatenatedmods) do
concatenatedmods
|> String.split(",")
|> Enum.each(&String.trim/1)
|> Enum.uniq()
|> modules()
end
defp modules(mods) do
Enum.map(mods, &Module.concat("Adifier.Applier", &1))
noconfirm = Keyword.get(opts, :noconfirm, false)

@appliers
|> Enum.map(&Module.concat("Adifier.Applier", &1))
|> Enum.reduce({:ok, :done}, fn
applier, {:ok, :done} -> applier.run(os, noconfirm)
applier, {:error, reason} -> {:error, reason}
end)
end
end
17 changes: 17 additions & 0 deletions adifier/lib/adifier/invoker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Adifier.Invoker do
@moduledoc """
This module invokes a command with specific command options.
"""

@cmd_opts [into: IO.stream(:stdio, :line)]

@doc false
def call("sudo" <> cmd) do
System.cmd("sh", ["-c" , "sudo -A #{cmd}"],
env: [{"SUDO_ASKPASS", "./askpass.sh"}],
into: IO.stream(:stdio, :line))
end
def call(cmd) do
System.cmd("sh", ["-c", cmd], @cmdopts)
end
end
32 changes: 15 additions & 17 deletions adifier/lib/adifier/package_manager.ex
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
defmodule Adifier.PackageManager do
@moduledoc """
This module's responsibility is to return command to install a given `package`
on a given `os`, or return command to update a package manager.`Adifier.Tool`
and `Adifier.PackageManager` delegate to this module by default.
"""

@needsudo ~w(apt apt-get yum)
@needyes ~w(apt apt-get yum)
@cmdopts [into: IO.stream(:stdio, :line)]

def package_managers(:ubuntu), do: ~w(apt apt-get)
def package_managers(:centos), do: ~w(yum)
def package_managers(:mac), do: ~w(brew)

def invoke_cmd(pm, with: str) when pm in @needsudo do
System.cmd("sudo", ["-A", pm | String.split(str)],
[env: [{"SUDO_ASKPASS", "/usr/bin/ssh-askpass"}]] ++ @cmdopts)
def install_cmd(:arch_linux, pkg), do: "sudo pacman -S #{pkg} --no-confirm"
def install_cmd(:mac, pkg), do: "brew install #{pkg}"
def install_cmd(:debian, pkg), do: "sudo apt -y install #{pkg}"
def install_cmd(os, pkg) when os in [:ubuntu, :pop_os] do
"sudo apt-get -y install #{pkg}"
end
def invoke_cmd(pm, with: str) do
System.cmd(pm, String.split(str), @cmdopts)
def install_cmd(os, pkg) when os in [:centos, :fedora] do
"sudo yum -y install #{pkg}"
end

def update_cmd(pm), do: invoke_cmd(pm, with: "update")

def install_pkg(pm, pkg) when pm in @needyes, do: invoke_cmd(pm, with: "install -y #{pkg}")
def install_pkg(pm, pkg), do: invoke_cmd(pm, with: "install #{pkg}")
def pm_update_cmd(:arch_linux), do: "sudo pacman -Syu --noconfirm"
def pm_update_cmd(:debian), do: "sudo apt -y update"
def pm_update_cmd(:mac), do: "brew update"
def pm_update_cmd(os) when os in [:ubuntu, :pop_os], do: "sudo apt-get -y update"
def pm_update_cmd(os) when os in [:centos, :fedora], do: "sudo yum -y update"
end
82 changes: 71 additions & 11 deletions adifier/lib/adifier/tool.ex
Original file line number Diff line number Diff line change
@@ -1,29 +1,89 @@
defmodule Adifier.Tool do
@moduledoc """
Represents metadata related to a tool.
Defines a `struct` with following variables:
* `ran_at`: `Datetime`
* `name` : `String`
* `install_cmd`: `String`
* `description` : `String`
* `errors` : `String`
Defines a `behaviour` with two `callbacks`:
* `install_cmd/1`: This function returns the command used to install the
tool on the `os` (given as the argument)
* `description/0`: This function returns a the description of the `tool`.
* `__name__/0`: This function returns the `name` of the `tool` being installed.
"""
@enforce_keys ~w{ran_at name install_cmd description}a

defstruct ~w{errors}a ++ @enforce_keys

import Adifier.PackageManager
@type t :: %__MODULE__{
ran_at: Datetime.t(),
name: String.t(),
install_cmd: String.t(),
description: String.t()
}

@callback install_cmd(Atom.t()) :: {:ok, term} | {:error, String.t()}
@callback description() :: String.t
@callback install_cmd(Atom.t()) :: {:ok | :error, __MODULE__.t()}
@callback description() :: String.t()
@callback __name__() :: String.t()

defmacro __using__(_opts) do
quote do
import Adifier.PackageManager
@doc """
This macro allows us to exploit the default behaviour of
`Adifier.Tool` module.
def install_cmd(os) do
raise "#{__MODULE__}.install_cmd/1 not implemented for os: #{os}"
## USAGE
```elixir
defmodule Test.Tool do
use Adifier.Tool, name: "tool"
def description do
"This tool runs `tool` tool"
end
end
```
"""
defmacro __using__(opts \\ []) do
name = Keyword.get(opts, :name, :default)

quote location: :keep do
import unquote(__MODULE__)
alias Adifier.PackageManager, as: PacMan

@behaviour unquote(__MODULE__)

@impl true
def install_cmd(os), do: PacMan.install_cmd(os, __name__())

@impl true
def __name__ do
case unquote(name)do
:default -> unquote(__MODULE__).__get_name__(__MODULE__)
name -> name
end
end

# TODO: Move to module attribute
def description() do
@impl true
def description do
raise """
No description given for module: #{__MODULE__}..
No description given for module: #{__MODULE__}.
Please define the function #{__MODULE__}.description/0
"""
end

defoverridable [install_cmd: 1, description: 0]
end
end

@doc false
def __get_name__(module) do
module
|> Module.split()
|> Enum.at(-1)
|> String.downcase()
end
end
12 changes: 0 additions & 12 deletions adifier/lib/adifier/tool/basic_tools.ex

This file was deleted.

22 changes: 10 additions & 12 deletions adifier/lib/adifier/tool/chromium.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
defmodule Adifier.Tool.Chromium do
@moduledoc """
Chromium is an open-source browser project that aims to build a safer,
faster, and more stable way for all Internet users to experience the web.
"""

use Adifier.Tool
use Adifier.Tool, name: "chromium-browser"

def install_cmd(:ubuntu) do
@impl true
def install_cmd(:mac) do
"""
sudo apt install chromium-browser
brew tap domt4/chromium
brew cask install mac-chromium
"""
end
def install_cmd(os), do: super(os)

def description() do
"""
Chromium is an open-source browser project that aims to build a safer,
faster, and more stable way for all Internet users to experience the web.
This is my default browser, as I don't have to worry about the RAM usage
a lot
"""
end
@impl true
def description, do: @moduledoc
end
6 changes: 6 additions & 0 deletions adifier/lib/adifier/tool/clang.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Adifier.Tool.Clang do
@moduledoc """
"""

use Adifier.Tool, name: "clang"
end
18 changes: 18 additions & 0 deletions adifier/lib/adifier/tool/curl.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Adifier.Tool.Curl do
@moduledoc """
CUrl is a tool to transfer data from or to a server, using one of the
supported protocols (`DICT`, `FILE`, `FTP`, `FTPS`, `GOPHER`, `HTTP`, `HTTPS`,
`IMAP`, `IMAPS`, `LDAP`, `LDAPS`, `POP3`, `POP3S`, `RTMP`, `RTSP`, `SCP`,
`SFTP`, `SMB`, `SMBS`, `SMTP`, `SMTPS`, `TELNET` and `TFTP`). The command is
designed to work without user interaction.
CUrl offers a busload of useful tricks like proxy support, user
authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer
resume, Met‐ alink, and more.
"""

use Adifier.Tool, name: "curl"

@impl true
def description, do: @moduledoc
end
20 changes: 10 additions & 10 deletions adifier/lib/adifier/tool/docker.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
defmodule Adifier.Tool.Docker do
@moduledoc """
Docker enables true independence between applications and infrastructure
and developers and IT ops to unlock their potential and creates a model
for better collaboration and innovation.
"""

use Adifier.Tool
use Adifier.Tool, name: "docker"

@impl true
def install_cmd(:mac), do: "brew install docker"
def install_cmd(:ubuntu) do
"""
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Expand All @@ -14,14 +19,9 @@ defmodule Adifier.Tool.Docker do
sudo systemctl status docker
"""
end
def install_cmd(:debian), do: "sudo apt install -y docker"
def install_cmd(os), do: super(os)

def description() do
"""
Docker enables true independence between applications and infrastructure
and developers and IT ops to unlock their potential and creates a model
for better collaboration and innovation.
I use docker for many things! Including elixir builds
"""
end
@impl true
def description, do: @moduledoc
end
28 changes: 20 additions & 8 deletions adifier/lib/adifier/tool/google_chrome.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
defmodule Adifier.Tool.GoogleChrome do
@moduledoc """
Google Chrome is a fast, secure, and free web browser, built for the modern web.
"""

use Adifier.Tool
use Adifier.Tool, name: "google-chrome"

@impl true
def install_cmd(:ubuntu) do
"""
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt-get update
sudo apt-get install google-chrome-stable<Paste>
sudo apt-get install google-chrome-stable
"""
end

def description() do
def install_cmd(:arch_linux) do
"""
git clone https://aur.archlinux.org/google-chrome.git ~/.adify/temp/tools/google-chrome/
cd ~/.adify/temp/tools/google-chrome/
makepkg -s --noconfirm
sudo pacman -U --noconfirm google-chrome-*.pkg.tar.xz
"""
One fast, simple, and secure browser for all your devices.
This is my browser for personal, non-work related usage.
"""
end
def install_cmd(:mac) do
"""
brew install brew-cask
brew cask install google-chrome
"""
end
def install_cmd(os), do: super(os)

@impl true
def description, do: @moduledoc
end
18 changes: 8 additions & 10 deletions adifier/lib/adifier/tool/mysql.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
defmodule Adifier.Tool.Mysql do
@moduledoc """
The most popular Open-source Relational Database Mangement System.
"""

use Adifier.Tool
use Adifier.Tool, name: "mysql"

@impl true
def install_cmd(:ubuntu) do
"""
sudo apt-get update sudo apt-get install mysql-server
sudo apt-get -y install mysql-server
/usr/bin/mysql_secure_installation
sudo service mysql start
sudo systemctl start mysql
"""
end
def install_cmd(os), do: super(os)

def description() do
"""
The most popular RDMS!
You WILL need it!
"""
end
@impl true
def description, do: @moduledoc
end
Loading

0 comments on commit bdbd0b0

Please sign in to comment.