|
1 | 1 | #' A Dockerfile template |
2 | 2 | #' |
3 | | -#' @return A dockerfile template |
4 | | -#' |
5 | | -#' @section Methods: |
6 | | -#' \describe{ |
7 | | -#' \item{\code{RUN}}{add a RUN command} |
8 | | -#' \item{\code{ADD}}{add a ADD command} |
9 | | -#' \item{\code{COPY}}{add a COPY command} |
10 | | -#' \item{\code{WORKDIR}}{add a WORKDIR command} |
11 | | -#' \item{\code{EXPOSE}}{add an EXPOSE command} |
12 | | -#' \item{\code{VOLUME}}{add a VOLUME command} |
13 | | -#' \item{\code{CMD}}{add a CMD command} |
14 | | -#' \item{\code{LABEL}}{add a LABEL command} |
15 | | -#' \item{\code{ENV}}{add a ENV command} |
16 | | -#' \item{\code{ENTRYPOINT}}{add a ENTRYPOINT command} |
17 | | -#' \item{\code{VOLUME}}{add a VOLUME command} |
18 | | -#' \item{\code{USER}}{add a USER command} |
19 | | -#' \item{\code{ARG}}{add an ARG command} |
20 | | -#' \item{\code{ONBUILD}}{add a ONBUILD command} |
21 | | -#' \item{\code{STOPSIGNAL}}{add a STOPSIGNAL command} |
22 | | -#' \item{\code{HEALTHCHECK}}{add a HEALTHCHECK command} |
23 | | -#' \item{\code{STOPSIGNAL}}{add a STOPSIGNAL command} |
24 | | -#' \item{\code{SHELL}}{add a SHELL command} |
25 | | -#' \item{\code{MAINTAINER}}{add a MAINTAINER command} |
26 | | -#' \item{\code{custom}}{add a custom command} |
27 | | -#' \item{\code{write}}{save the Dockerfile} |
28 | | -#' \item{\code{switch_cmd}}{switch two command} |
29 | | -#' \item{\code{remove_cmd}}{remove_cmd one or more command(s)} |
30 | | -#' } |
31 | | -#' |
32 | | -#' @importFrom R6 R6Class |
33 | 3 | #' @export |
34 | | -#' |
| 4 | +#' @importFrom R6 R6Class |
35 | 5 | #' @examples |
36 | 6 | #' my_dock <- Dockerfile$new() |
37 | 7 | Dockerfile <- R6::R6Class( |
38 | | - "Dockerfile", |
39 | | - public = list( |
40 | | - Dockerfile = character(), |
41 | | - ## Either from a file, or from a character vector |
42 | | - initialize = function( |
43 | | - FROM = "rocker/r-base", |
44 | | - AS = NULL |
45 | | - ) { |
46 | | - self$Dockerfile <- create_dockerfile(FROM, AS) |
47 | | - }, |
48 | | - RUN = function(cmd) { |
49 | | - self$Dockerfile <- c(self$Dockerfile, add_run(cmd)) |
50 | | - }, |
51 | | - ADD = function(from, to, force = TRUE) { |
52 | | - self$Dockerfile <- c(self$Dockerfile, add_add(from, to, force)) |
53 | | - }, |
54 | | - COPY = function(from, to, force = TRUE) { |
55 | | - self$Dockerfile <- c(self$Dockerfile, add_copy(from, to, force)) |
56 | | - }, |
57 | | - WORKDIR = function(where) { |
58 | | - self$Dockerfile <- c(self$Dockerfile, add_workdir(where)) |
59 | | - }, |
60 | | - EXPOSE = function(port) { |
61 | | - self$Dockerfile <- c(self$Dockerfile, add_expose(port)) |
62 | | - }, |
63 | | - VOLUME = function(volume) { |
64 | | - self$Dockerfile <- c(self$Dockerfile, add_volume(volume)) |
65 | | - }, |
66 | | - CMD = function(cmd) { |
67 | | - self$Dockerfile <- c(self$Dockerfile, add_cmd(cmd)) |
68 | | - }, |
69 | | - LABEL = function(key, value) { |
70 | | - self$Dockerfile <- c(self$Dockerfile, add_label(key, value)) |
71 | | - }, |
72 | | - ENV = function(key, value) { |
73 | | - self$Dockerfile <- c(self$Dockerfile, add_env(key, value)) |
74 | | - }, |
75 | | - ENTRYPOINT = function(cmd) { |
76 | | - self$Dockerfile <- c(self$Dockerfile, add_entrypoint(cmd)) |
77 | | - }, |
78 | | - USER = function(user) { |
79 | | - self$Dockerfile <- c(self$Dockerfile, add_user(user)) |
80 | | - }, |
81 | | - ARG = function(arg, ahead = FALSE) { |
82 | | - if (ahead) { |
83 | | - self$Dockerfile <- c(add_arg(arg), self$Dockerfile) |
84 | | - } else { |
85 | | - self$Dockerfile <- c(self$Dockerfile, add_arg(arg)) |
86 | | - } |
87 | | - }, |
88 | | - ONBUILD = function(cmd) { |
89 | | - self$Dockerfile <- c(self$Dockerfile, add_onbuild(cmd)) |
90 | | - }, |
91 | | - STOPSIGNAL = function(signal) { |
92 | | - self$Dockerfile <- c(self$Dockerfile, add_stopsignal(signal)) |
93 | | - }, |
94 | | - HEALTHCHECK = function(check) { |
95 | | - self$Dockerfile <- c(self$Dockerfile, add_healthcheck(check)) |
96 | | - }, |
97 | | - SHELL = function(shell) { |
98 | | - self$Dockerfile <- c(self$Dockerfile, add_shell(shell)) |
99 | | - }, |
100 | | - MAINTAINER = function(name, email) { |
101 | | - self$Dockerfile <- c(self$Dockerfile, add_maintainer(name, email)) |
102 | | - }, |
103 | | - custom = function(base, cmd) { |
104 | | - self$Dockerfile <- c(self$Dockerfile, add_custom(base, cmd)) |
105 | | - }, |
106 | | - print = function() { |
107 | | - cat(self$Dockerfile, sep = "\n") |
108 | | - }, |
109 | | - write = function(as = "Dockerfile") { |
110 | | - base::write(self$Dockerfile, file = as) |
111 | | - }, |
112 | | - switch_cmd = function(a, b) { |
113 | | - self$Dockerfile <- switch_them(self$Dockerfile, a, b) |
114 | | - }, |
115 | | - remove_cmd = function(where) { |
116 | | - self$Dockerfile <- remove_from(self$Dockerfile, where) |
117 | | - }, |
118 | | - add_after = function(cmd, after) { |
119 | | - self$Dockerfile <- add_to(self$Dockerfile, cmd, after) |
120 | | - } |
121 | | - ) |
| 8 | +"Dockerfile", |
| 9 | +public = list( |
| 10 | +#' @field Dockerfile The dockerfile content. |
| 11 | +Dockerfile = character(), |
| 12 | +#' @description |
| 13 | +#' Create a new Dockerfile object. |
| 14 | +#' @param FROM The base image. |
| 15 | +#' @param AS The name of the image. |
| 16 | +#' @return A Dockerfile object. |
| 17 | +initialize = function(FROM = "rocker/r-base", |
| 18 | +AS = NULL) { |
| 19 | +self$Dockerfile <- create_dockerfile(FROM, AS) |
| 20 | +}, |
| 21 | +#' @description |
| 22 | +#' Add a RUN command. |
| 23 | +#' @param cmd The command to add. |
| 24 | +#' @return the Dockerfile object, invisibly. |
| 25 | +RUN = function(cmd) { |
| 26 | +self$Dockerfile <- c(self$Dockerfile, add_run(cmd)) |
| 27 | +}, |
| 28 | +#' @description |
| 29 | +#' Add a ADD command. |
| 30 | +#' @param from The source file. |
| 31 | +#' @param to The destination file. |
| 32 | +#' @param force If TRUE, overwrite the destination file. |
| 33 | +#' @return the Dockerfile object, invisibly. |
| 34 | +ADD = function(from, to, force = TRUE) { |
| 35 | +self$Dockerfile <- c(self$Dockerfile, add_add(from, to, force)) |
| 36 | +}, |
| 37 | +#' @description |
| 38 | +#' Add a COPY command. |
| 39 | +#' @param from The source file. |
| 40 | +#' @param to The destination file. |
| 41 | +#' @param force If TRUE, overwrite the destination file. |
| 42 | +#' @return the Dockerfile object, invisibly. |
| 43 | +COPY = function(from, to, force = TRUE) { |
| 44 | +self$Dockerfile <- c(self$Dockerfile, add_copy(from, to, force)) |
| 45 | +}, |
| 46 | +#' @description |
| 47 | +#' Add a WORKDIR command. |
| 48 | +#' @param where The working directory. |
| 49 | +#' @return the Dockerfile object, invisibly. |
| 50 | +WORKDIR = function(where) { |
| 51 | +self$Dockerfile <- c(self$Dockerfile, add_workdir(where)) |
| 52 | +}, |
| 53 | +#' @description |
| 54 | +#' Add a EXPOSE command. |
| 55 | +#' @param port The port to expose. |
| 56 | +#' @return the Dockerfile object, invisibly. |
| 57 | +EXPOSE = function(port) { |
| 58 | +self$Dockerfile <- c(self$Dockerfile, add_expose(port)) |
| 59 | +}, |
| 60 | +#' @description |
| 61 | +#' Add a VOLUME command. |
| 62 | +#' @param volume The volume to add. |
| 63 | +#' @return the Dockerfile object, invisibly. |
| 64 | +VOLUME = function(volume) { |
| 65 | +self$Dockerfile <- c(self$Dockerfile, add_volume(volume)) |
| 66 | +}, |
| 67 | +#' @description |
| 68 | +#' Add a CMD command. |
| 69 | +#' @param cmd The command to add. |
| 70 | +#' @return the Dockerfile object, invisibly. |
| 71 | +CMD = function(cmd) { |
| 72 | +self$Dockerfile <- c(self$Dockerfile, add_cmd(cmd)) |
| 73 | +}, |
| 74 | +#' @description |
| 75 | +#' Add a LABEL command. |
| 76 | +#' @param key,value The key and value of the label. |
| 77 | +#' @return the Dockerfile object, invisibly. |
| 78 | +LABEL = function(key, value) { |
| 79 | +self$Dockerfile <- c(self$Dockerfile, add_label(key, value)) |
| 80 | +}, |
| 81 | +#' @description |
| 82 | +#' Add a ENV command. |
| 83 | +#' @param key,value The key and value of the label. |
| 84 | +#' @return the Dockerfile object, invisibly. |
| 85 | +ENV = function(key, value) { |
| 86 | +self$Dockerfile <- c(self$Dockerfile, add_env(key, value)) |
| 87 | +}, |
| 88 | +#' @description |
| 89 | +#' Add a ENTRYPOINT command. |
| 90 | +#' @param cmd The command to add. |
| 91 | +#' @return the Dockerfile object, invisibly. |
| 92 | +ENTRYPOINT = function(cmd) { |
| 93 | +self$Dockerfile <- c(self$Dockerfile, add_entrypoint(cmd)) |
| 94 | +}, |
| 95 | +#' @description |
| 96 | +#' Add a USER command. |
| 97 | +#' @param user The user to add. |
| 98 | +#' @return the Dockerfile object, invisibly. |
| 99 | +USER = function(user) { |
| 100 | +self$Dockerfile <- c(self$Dockerfile, add_user(user)) |
| 101 | +}, |
| 102 | +#' @description |
| 103 | +#' Add a ARG command. |
| 104 | +#' @param arg The argument to add. |
| 105 | +#' @param ahead If TRUE, add the argument at the beginning of the Dockerfile. |
| 106 | +#' @return the Dockerfile object, invisibly. |
| 107 | +ARG = function(arg, ahead = FALSE) { |
| 108 | +if (ahead) { |
| 109 | +self$Dockerfile <- c(add_arg(arg), self$Dockerfile) |
| 110 | +} else { |
| 111 | +self$Dockerfile <- c(self$Dockerfile, add_arg(arg)) |
| 112 | +} |
| 113 | +}, |
| 114 | +#' @description |
| 115 | +#' Add a ONBUILD command. |
| 116 | +#' @param cmd The command to add. |
| 117 | +#' @return the Dockerfile object, invisibly. |
| 118 | +ONBUILD = function(cmd) { |
| 119 | +self$Dockerfile <- c(self$Dockerfile, add_onbuild(cmd)) |
| 120 | +}, |
| 121 | +#' @description |
| 122 | +#' Add a STOPSIGNAL command. |
| 123 | +#' @param signal The signal to add. |
| 124 | +#' @return the Dockerfile object, invisibly. |
| 125 | +STOPSIGNAL = function(signal) { |
| 126 | +self$Dockerfile <- c(self$Dockerfile, add_stopsignal(signal)) |
| 127 | +}, |
| 128 | +#' @description |
| 129 | +#' Add a HEALTHCHECK command. |
| 130 | +#' @param check The check to add. |
| 131 | +#' @return the Dockerfile object, invisibly. |
| 132 | +HEALTHCHECK = function(check) { |
| 133 | +self$Dockerfile <- c(self$Dockerfile, add_healthcheck(check)) |
| 134 | +}, |
| 135 | +#' @description |
| 136 | +#' Add a SHELL command. |
| 137 | +#' @param shell The shell to add. |
| 138 | +#' @return the Dockerfile object, invisibly. |
| 139 | +SHELL = function(shell) { |
| 140 | +self$Dockerfile <- c(self$Dockerfile, add_shell(shell)) |
| 141 | +}, |
| 142 | +#' @description |
| 143 | +#' Add a MAINTAINER command. |
| 144 | +#' @param name,email The name and email of the maintainer. |
| 145 | +#' @return the Dockerfile object, invisibly. |
| 146 | +MAINTAINER = function(name, email) { |
| 147 | +self$Dockerfile <- c(self$Dockerfile, add_maintainer(name, email)) |
| 148 | +}, |
| 149 | +#' @description |
| 150 | +#' Add a custom command. |
| 151 | +#' @param base,cmd The base and command to add. |
| 152 | +#' @return the Dockerfile object, invisibly. |
| 153 | +custom = function(base, cmd) { |
| 154 | +self$Dockerfile <- c(self$Dockerfile, add_custom(base, cmd)) |
| 155 | +}, |
| 156 | +#' @description |
| 157 | +#' Print the Dockerfile. |
| 158 | +#' @return used for side effect |
| 159 | +print = function() { |
| 160 | +cat(self$Dockerfile, sep = "\n") |
| 161 | +}, |
| 162 | +#' @description |
| 163 | +#' Write the Dockerfile to a file. |
| 164 | +#' @param as The file to write to. |
| 165 | +#' @return used for side effect |
| 166 | +write = function(as = "Dockerfile") { |
| 167 | +base::write(self$Dockerfile, file = as) |
| 168 | +}, |
| 169 | +#' @description |
| 170 | +#' Switch commands. |
| 171 | +#' @param a,b The commands to switch. |
| 172 | +#' @return the Dockerfile object, invisibly. |
| 173 | +switch_cmd = function(a, b) { |
| 174 | +self$Dockerfile <- switch_them(self$Dockerfile, a, b) |
| 175 | +}, |
| 176 | +#' @description |
| 177 | +#' Remove a command. |
| 178 | +#' @param where The commands to remove. |
| 179 | +#' @return the Dockerfile object, invisibly. |
| 180 | +remove_cmd = function(where) { |
| 181 | +self$Dockerfile <- remove_from(self$Dockerfile, where) |
| 182 | +}, |
| 183 | +#' @description |
| 184 | +#' Add a command after another. |
| 185 | +#' @param cmd The command to add. |
| 186 | +#' @param after Where to add the cmd |
| 187 | +#' @return the Dockerfile object, invisibly. |
| 188 | +add_after = function(cmd, after) { |
| 189 | +self$Dockerfile <- add_to(self$Dockerfile, cmd, after) |
| 190 | +} |
| 191 | +) |
122 | 192 | ) |
0 commit comments