Skip to content

Commit 8b011ef

Browse files
committed
- Added Head task.
- Added verbose parameter to GenericTask in order to provide task scoped output control.
1 parent 7d786ad commit 8b011ef

File tree

6 files changed

+66
-22
lines changed

6 files changed

+66
-22
lines changed

tasks-dsl/src/main/scala/net/codejitsu/tasks/Download.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import net.codejitsu.tasks.dsl._
1717
* @param user user.
1818
*/
1919
final case class Download[S <: Stage](hosts: Hosts, url: URL,
20-
destinationPath: String, usingSudo: Boolean = false,
20+
destinationPath: Option[String] = None, usingSudo: Boolean = false,
2121
usingPar: Boolean = false,
2222
exec: String = Download.getExec(),
23-
params: List[String] = List("-P"))(implicit user: User, stage: S, rights: S Allow Download[S])
24-
extends GenericTask("wget", "download url", hosts, exec, params ++ List(url.toString) ++ List(destinationPath),
25-
usingSudo, usingPar, taskRepr = s"download url '${url.toString}'") with UsingSudo[Download[S]] with UsingParallelExecution[Download[S]] {
23+
params: List[String] = List("-P"),
24+
verbose: VerbosityLevel = NoOutput)(implicit user: User, stage: S, rights: S Allow Download[S])
25+
extends GenericTask("wget", "download url", hosts, exec, params ++ List(url.toString) ++ Download.getDestination(destinationPath),
26+
usingSudo, usingPar, taskRepr = s"download url '${url.toString}'", verbose = Option(verbose)) with UsingSudo[Download[S]] with UsingParallelExecution[Download[S]] {
2627

2728
override def sudo: Download[S] = copy[S](usingSudo = true)
2829
override def par: Download[S] = copy[S](usingPar = true)
@@ -34,4 +35,7 @@ object Download {
3435
case MacOS => "/usr/local/bin/wget"
3536
case _ => throw new IllegalArgumentException("Not supported OS")
3637
}
38+
39+
def getDestination(destinationPath: Option[String]): List[String] =
40+
destinationPath.fold(List.empty[String])(d => List(d))
3741
}

tasks-dsl/src/main/scala/net/codejitsu/tasks/GenericTask.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import net.codejitsu.tasks.dsl.Tasks._
88
abstract class GenericTask(name: String, desc: String, hosts: Hosts, exec: String,
99
params: List[String] = Nil, usingSudo: Boolean = false,
1010
usingPar: Boolean = false, cmd: Command = Start, taskRepr: String = "",
11-
pipeCmd: Seq[String] = Seq[String]())(implicit user: User) extends TaskM[Boolean] {
11+
pipeCmd: Seq[String] = Seq[String](), verbose: Option[VerbosityLevel] = None)(implicit user: User) extends TaskM[Boolean] {
1212
override def description: String = desc
1313

1414
protected val procs: Processes = name on hosts ~> {
@@ -28,15 +28,15 @@ abstract class GenericTask(name: String, desc: String, hosts: Hosts, exec: Strin
2828
procs ! cmd
2929
}
3030

31-
override def run(verbose: VerbosityLevel = NoOutput, input: Option[TaskResult[_]] = None): TaskResult[Boolean] =
31+
override def run(verbose: VerbosityLevel, input: Option[TaskResult[_]] = None): TaskResult[Boolean] =
3232
LoggedRun(
33-
verbose,
33+
this.verbose.fold(verbose)(v => v),
3434
usingSudo,
3535
usingPar,
3636
hosts,
3737
taskRepr,
3838
task,
3939
input
40-
)(verbose)
40+
)(this.verbose.fold(verbose)(v => v))
4141
}
4242

tasks-dsl/src/main/scala/net/codejitsu/tasks/Grep.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ final case class Grep[S <: Stage](hosts: Hosts,
1919
usingPar: Boolean = false,
2020
exec: String = Grep.getExec(),
2121
params: List[String] = Nil,
22-
pattern: Option[String] = None)(implicit user: User, stage: S, rights: S Allow Grep[S])
22+
pattern: Option[String] = None,
23+
verbose: VerbosityLevel = NoOutput)(implicit user: User, stage: S, rights: S Allow Grep[S])
2324
extends GenericTask("grep", "searches any given input files, selecting lines that match one or more patterns", hosts, exec,
2425
Grep.makeCommandLine(params, pattern, target),
25-
usingSudo, usingPar, taskRepr = s"grep file '${target}' for patterns",
26-
pipeCmd = Grep.pipeCmd(pattern)) with UsingSudo[Grep[S]] with UsingParallelExecution[Grep[S]] {
26+
usingSudo, usingPar, taskRepr = s"grep file ${Grep.getFileDescription(target)} for patterns",
27+
pipeCmd = Grep.pipeCmd(pattern), verbose = Option(verbose)) with UsingSudo[Grep[S]] with UsingParallelExecution[Grep[S]] {
2728

2829
override def sudo: Grep[S] = copy[S](usingSudo = true)
2930
override def par: Grep[S] = copy[S](usingPar = true)
@@ -44,4 +45,7 @@ object Grep {
4445
}
4546

4647
def pipeCmd(pattern: Option[String]): Seq[String] = Seq(getExec(), "-o") ++ pattern.fold(Seq[String]())(p => Seq[String](p))
48+
49+
def getFileDescription(target: Option[String]): String =
50+
target.fold("")(f => s"'$f'")
4751
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (C) 2015, codejitsu.
2+
3+
package net.codejitsu.tasks
4+
5+
import net.codejitsu.tasks.dsl.Allow
6+
import net.codejitsu.tasks.dsl.Hosts
7+
import net.codejitsu.tasks.dsl.Stage
8+
import net.codejitsu.tasks.dsl.User
9+
import net.codejitsu.tasks.dsl.UsingParallelExecution
10+
import net.codejitsu.tasks.dsl.UsingSudo
11+
12+
/**
13+
* Head task.
14+
*
15+
* @param hosts target hosts
16+
* @param target target file
17+
* @param usingSudo true, if task have to be started with sudo
18+
* @param usingPar true, if parallel execution required.
19+
* @param user user
20+
*/
21+
final case class Head[S <: Stage](hosts: Hosts,
22+
target: Option[String] = None,
23+
usingSudo: Boolean = false,
24+
usingPar: Boolean = false,
25+
exec: String = "/usr/bin/head")(implicit user: User, stage: S, rights: S Allow Head[S])
26+
extends GenericTask("head", "the first part of file", hosts, exec, Nil,
27+
usingSudo, usingPar, taskRepr = s"display file ${Head.getFileDescription(target)}",
28+
pipeCmd = Head.pipeCmd()) with UsingSudo[Head[S]] with UsingParallelExecution[Head[S]] {
29+
30+
override def sudo: Head[S] = copy[S](usingSudo = true)
31+
override def par: Head[S] = copy[S](usingPar = true)
32+
}
33+
34+
object Head {
35+
def getFileDescription(target: Option[String]): String =
36+
target.fold("")(f => s"'$f'")
37+
38+
def pipeCmd(): Seq[String] = Seq("/usr/bin/head")
39+
}

tasks-dsl/src/main/scala/net/codejitsu/tasks/dsl/Allow.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ object Allow {
3535
implicit val devAllowTail: Dev Allow Tail[Dev] = new Allow[Dev, Tail[Dev]]
3636
implicit val devAllowClear: Dev Allow Clear[Dev] = new Allow[Dev, Clear[Dev]]
3737
implicit val devAllowGrep: Dev Allow Grep[Dev] = new Allow[Dev, Grep[Dev]]
38+
implicit val devAllowHead: Dev Allow Head[Dev] = new Allow[Dev, Head[Dev]]
3839
}

tasks-dsl/src/test/scala/GrepTest.scala

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,26 @@ class GrepTest extends FlatSpec with Matchers {
7272
taskResult.err should be (empty)
7373
taskResult.out.filter(_.startsWith("<title")).size should be (0)
7474
}
75-
/*
75+
7676
it should "support complex piping" in {
7777
val getRawFeed =
78-
Download(Localhost, new URL("http://stackoverflow.com"), "/feeds/tag?tagnames=scala&sort=newest"/*,
79-
params = List("-q", "-O-")*/)
78+
Download(Localhost, new URL("http://stackoverflow.com/feeds/tag?tagnames=scala&sort=newest"),
79+
params = List("-q", "-O-"))
8080

8181
val getRawTitles = Grep(Localhost, params = List("-o"), pattern = Option("<title type=\"text\">[^<]*"))
8282

8383
val getTitles = Grep(Localhost, params = List("-o"), pattern = Option("[^>]*$"))
8484

8585
val task =
86-
getRawFeed //pipeTo
87-
// getRawTitles //pipeTo
88-
// getTitles
86+
getRawFeed pipeTo
87+
getRawTitles pipeTo
88+
getTitles pipeTo
89+
Head(Localhost)
8990

9091
val taskResult = task.run(FullOutput)
9192

92-
println(taskResult)
93-
94-
//taskResult.out.foreach(println)
95-
9693
taskResult.res.isSuccess should be (true)
9794
taskResult.err should be (empty)
9895
taskResult.out.filter(_.startsWith("<title")).size should be (0)
9996
}
100-
*/
10197
}

0 commit comments

Comments
 (0)