Skip to content

Commit

Permalink
Add CLI flag for Sheets tokens directory
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalkin committed Jun 12, 2021
1 parent 57bd612 commit 7644b33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import q2s.qualtrics.QualtricsDatacenter
import q2s.qualtrics.checkApiToken
import q2s.qualtrics.downloadSurvey
import q2s.sheets.SheetsClient
import q2s.sheets.TOKENS_DIRECTORY_PATH
import q2s.sheets.uploadCSV
import q2s.util.toPath
import java.nio.file.Files
Expand Down Expand Up @@ -60,6 +61,11 @@ class ExportAndUploadCommand : Subcommand("run", "download a Qualtrics export an
fullName = "spreadsheet",
description = "the ID of the target spreadsheet",
).required()
val tokensDirectory by option(
ArgType.String,
fullName = "tokens-directory",
description = "directory for storing (and subsequently reading) the Sheets tokens",
).default(TOKENS_DIRECTORY_PATH)

override fun execute() {
DEFAULT_LOG_LEVEL = if (debug == true) LogLevel.DEBUG else LogLevel.INFO
Expand All @@ -86,7 +92,8 @@ class ExportAndUploadCommand : Subcommand("run", "download a Qualtrics export an

logger.debug { "determined the exported CSV file to be $csvFile" }

val client = SheetsClient(credentials.toPath()).getClient()
val tokensDirectoryPath = tokensDirectory.toPath().toFile()
val client = SheetsClient(credentials.toPath(), tokensDirectoryPath).getClient()
uploadCSV(client, spreadsheetID, csvFile)

logger.debug { "*not* cleaning up temporary directory $tmpDirectory" }
Expand Down
6 changes: 3 additions & 3 deletions qualtrics2sheets/src/main/kotlin/q2s/sheets/SheetsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ const val TOKENS_DIRECTORY_PATH = "tokens"
const val OAUTH_SERVER_PORT = 18800

class SheetsClient(
private val credentialsFilePath: Path
private val credentialsFilePath: Path,
private val tokensDirectory: File = File(TOKENS_DIRECTORY_PATH),
) {
private val applicationName: String = APPLICATION_NAME
private val jsonFactory: JsonFactory = GsonFactory.getDefaultInstance()
private val tokensDirectoryPath = TOKENS_DIRECTORY_PATH
private val userId = "user"

/**
Expand All @@ -58,7 +58,7 @@ class SheetsClient(
val flow = GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, scopes
)
.setDataStoreFactory(FileDataStoreFactory(File(tokensDirectoryPath)))
.setDataStoreFactory(FileDataStoreFactory(tokensDirectory))
.setAccessType("offline")
.build()
val receiver = LocalServerReceiver.Builder().setPort(OAUTH_SERVER_PORT).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ class UploadCSVCommand : Subcommand("upload_csv", "(only) upload a CSV file") {
shortName = "f",
description = "the path to the CSV file to upload"
).required()
val tokensDirectory by option(
ArgType.String,
fullName = "tokens-directory",
description = "directory for storing (and subsequently reading) the Sheets tokens",
).default(TOKENS_DIRECTORY_PATH)

override fun execute() {
val client = SheetsClient(credentials.toPath()).getClient()
val tokensDirectoryPath = tokensDirectory.toPath().toFile()
val client = SheetsClient(credentials.toPath(), tokensDirectoryPath).getClient()
uploadCSV(client, spreadsheetID, csvFile.toPath())

subcommandFinished = true
Expand Down

0 comments on commit 7644b33

Please sign in to comment.