Skip to content

Commit

Permalink
Merge pull request #5 from rundeck/feature/readme
Browse files Browse the repository at this point in the history
Add `rd projects readme` subcommand
  • Loading branch information
gschueler authored Sep 15, 2016
2 parents 06f95e2 + d80a461 commit 89ab95c
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/java/org/rundeck/client/api/ReadmeFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.rundeck.client.api;

/**
* Created by greg on 9/15/16.
*/
public enum ReadmeFile {
README("readme.md"),
MOTD("motd.md");

String name;

ReadmeFile(String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}
}
47 changes: 47 additions & 0 deletions src/main/java/org/rundeck/client/api/RundeckApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,51 @@ Call<Void> deleteToken(
@Path("id") String id
);


/**
* <a href="http://rundeck.org/docs/api/index.html#get-readme-file">Get Readme File</a>
*
* @param project project
* @param file type of readme file
*
* @return readme contents
*/
@Headers("Accept: application/json")
@GET("project/{project}/{file}")
Call<ProjectReadme> getReadme(
@Path("project") String project,
@Path("file") ReadmeFile file
);

/**
* <a href="http://rundeck.org/docs/api/index.html#put-readme-file">Put Readme File</a>
*
* @param project project
* @param file type of readme file
*
* @return readme contents
*/
@Headers("Accept: application/json")
@PUT("project/{project}/{file}")
Call<ProjectReadme> putReadme(
@Path("project") String project,
@Path("file") ReadmeFile file,
@Body RequestBody contents
);

/**
* <a href="http://rundeck.org/docs/api/index.html#get-readme-file">Get Readme File</a>
*
* @param project project
* @param file type of readme file
*
* @return readme contents
*/
@Headers("Accept: application/json")
@DELETE("project/{project}/{file}")
Call<Void> deleteReadme(
@Path("project") String project,
@Path("file") ReadmeFile file
);

}
17 changes: 17 additions & 0 deletions src/main/java/org/rundeck/client/api/model/ProjectReadme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.rundeck.client.api.model;

/**
* Created by greg on 9/15/16.
*/
public class ProjectReadme {

private String contents;

public String getContents() {
return contents;
}

public void setContents(String contents) {
this.contents = contents;
}
}
4 changes: 3 additions & 1 deletion src/main/java/org/rundeck/client/tool/commands/Projects.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.rundeck.client.api.RundeckApi;
import org.rundeck.client.api.model.ProjectItem;
import org.rundeck.client.tool.commands.projects.ACLs;
import org.rundeck.client.tool.commands.projects.Readme;
import org.rundeck.client.tool.commands.projects.SCM;
import org.rundeck.client.tool.options.ProjectCreateOptions;
import org.rundeck.client.tool.options.ProjectNameOptions;
Expand All @@ -32,7 +33,8 @@ public Projects(final Client<RundeckApi> client) {
public List<Object> getSubCommands() {
return Arrays.asList(
new ACLs(client),
new SCM(client)
new SCM(client),
new Readme(client)
);
}

Expand Down
109 changes: 109 additions & 0 deletions src/main/java/org/rundeck/client/tool/commands/projects/Readme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.rundeck.client.tool.commands.projects;

import com.lexicalscope.jewel.cli.Option;
import com.simplifyops.toolbelt.Command;
import com.simplifyops.toolbelt.CommandOutput;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.rundeck.client.api.ReadmeFile;
import org.rundeck.client.api.RundeckApi;
import org.rundeck.client.api.model.ProjectReadme;
import org.rundeck.client.tool.commands.ApiCommand;
import org.rundeck.client.tool.options.ProjectNameOptions;
import org.rundeck.client.util.Client;

import java.io.File;
import java.io.IOException;

/**
* Created by greg on 9/15/16.
*/

@Command(description = "Manage Project readme.md/motd.md")
public class Readme extends ApiCommand {
public Readme(final Client<RundeckApi> client) {
super(client);
}

public interface GetOptions extends ProjectNameOptions {
/**
* @return
*/
@Option(shortName = "m",
longName = "motd",
description = "Choose the 'motd.md' file. If unset, choose 'readme.md'.")
public boolean isMotd();

}

public ReadmeFile getReadmeFile(GetOptions options) {
return options.isMotd() ? ReadmeFile.MOTD : ReadmeFile.README;
}

@Command(description = "get project readme/motd file")
public void get(GetOptions options, CommandOutput output) throws IOException {
ProjectReadme readme = client.checkError(client.getService()
.getReadme(
options.getProject(),
getReadmeFile(options)
));
output.output(readme.getContents());
}


public interface SetOptions extends GetOptions {
/**
* @return
*/
@Option(shortName = "f", longName = "file", description = "Path to a file to read for readme/motd contents.")
public File getFile();

public boolean isFile();

/**
* @return
*/
@Option(shortName = "t", longName = "text", description = "Text to use for readme/motd contents.")
public String getText();

public boolean isText();
}


@Command(description = "set project readme/motd file")
public void put(SetOptions options, CommandOutput output) throws IOException {
if (!options.isText() && !options.isFile()) {
throw new IllegalArgumentException("-f/--file or -t/--text is required");
}
RequestBody requestBody;
if (options.isFile()) {
requestBody = RequestBody.create(
MediaType.parse("text/plain"),
options.getFile()
);
} else {
requestBody = RequestBody.create(
MediaType.parse("text/plain"),
options.getText()
);
}
ProjectReadme readme = client.checkError(client.getService()
.putReadme(
options.getProject(),
getReadmeFile(options),
requestBody
));
output.output(readme.getContents());
}


@Command(description = "delete project readme/motd file")
public void delete(GetOptions options, CommandOutput output) throws IOException {
Void readme = client.checkError(client.getService()
.deleteReadme(
options.getProject(),
getReadmeFile(options)
));

}
}

0 comments on commit 89ab95c

Please sign in to comment.