aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/pkgs/desktops/pantheon/nixpkgs_github_update/lib/nixpkgs_github_update/cli.ex
blob: e735c4285169f30a1cf3080ec4c3df88f427c08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
defmodule NixpkgsGitHubUpdate.CLI do
  @moduledoc """
  Run updates on Nix Expressions that use fetchFromGitHub.

  Arguments the script accepts:
  --attribute <attribute_path>

  Example usage:
  ```
  ./nixpkgs_github_update --attribute "notes-up"
  ```
  """
  alias NixpkgsGitHubUpdate.{Nix, GitHubLatestVersion}

  def help do
    IO.puts("""
    Run updates on Nix Expressions that use fetchFromGitHub.

    Arguments the script accepts:
    --attribute <attribute_path>

    Example usage:
    ./nixpkgs_github_update --attribute "notes-up"
    """)
  end

  def main([]) do
    help()
  end

  def main(args) do
    opts = parse_args(args)

    attribute = opts[:attribute]

    case Nix.attribute_exists?(attribute) do
      true -> update(attribute)
      _ -> exit("Requested attribute doesn't exist.")
    end
  end

  def parse_args(args) do
    {options, _, _} =
      args
      |> OptionParser.parse(strict: [attribute: :string])

    options
  end

  def update(attribute) do
    version =
      Nix.get_owner_repo(attribute)
      |> GitHubLatestVersion.fetch()
      |> decode_response()
      |> construct_version()

    Nix.update_source_version(attribute, version)
  end

  def decode_response({:ok, response}), do: response

  def decode_response({:error, error}) do
    IO.puts("Error getting latest release from GitHub: #{error["message"]}")
    System.halt(2)
  end

  def construct_version(response) do
    Map.get(response, "tag_name")
    |> String.trim_leading("v")
  end
end