Skip to content

Commit

Permalink
unable loading config from git
Browse files Browse the repository at this point in the history
  • Loading branch information
Walid Lezzar committed Mar 12, 2020
1 parent 97d1ed2 commit ffbaa32
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 6 deletions.
29 changes: 29 additions & 0 deletions docs/configuration/init.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Config initialization

## From scratch

The simplest way to initialize zoe configuration from scratch is to use the zoe CLI:

```bash tab="command"
zoe config init
```

This will create a `default.yml` file in `~/.zoe/config` by default.

## From an existing local directory

If you already have locally a directory that contains ready to use configuration files you can use:

```bash tab="command"
zoe config init --from local --path /path/to/config
```

This will copy the yaml files from the target directory to `~/.zoe/config`.

## From a git repository

You can also copy the configuration from an existing git repository. For example, to copy the zoe config used in the tutorials in the official repository:

```bash tab="command"
zoe config init --from git --url 'https://github.com/adevinta/zoe.git' --dir tutorials/simple/config
```
1 change: 1 addition & 0 deletions docs/configuration/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Zoe loads its configuration from a variety of sources. It also has a complex ove

In this guide, we will discover the following points:

- [Config initialization](init.md): We will start with how to initialize zoe configuration files by either creating them from scratch or loading them from an existing source.
- [Environments](environments.md): We will see how zoe allows us to separate clusters' configuration into multiple environments and how we can select the appropriate environment when using zoe.
- [Configuration loading chain](chain.md): We will then tackle zoe's complex configuration loading chain and how configuration values can be overridden with environment variables.
- [Configuration reference](reference.md): This section contains the list of all possible values in zoe's configuration file.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nav:
- 'Operations': basics/operations.md
- 'Configuration':
- 'Overview': configuration/overview.md
- 'Config initialization': configuration/init.md
- 'Environments': configuration/environments.md
- 'Configuration loading chain': configuration/chain.md
- 'Reference': configuration/reference.md
Expand Down
1 change: 1 addition & 0 deletions zoe-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies {
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.7")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.7")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.eclipse.jgit:org.eclipse.jgit:5.7.0.202003090808-r")

implementation("org.koin:koin-core:2.0.1")
implementation("com.jakewharton.picnic:picnic:0.2.0")
Expand Down
81 changes: 75 additions & 6 deletions zoe-cli/src/commands/config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.adevinta.oss.zoe.cli.commands

import com.adevinta.oss.zoe.cli.config.*
import com.adevinta.oss.zoe.cli.utils.globalTermColors
import com.adevinta.oss.zoe.cli.utils.yaml
import com.adevinta.oss.zoe.core.utils.json
import com.adevinta.oss.zoe.core.utils.logger
Expand All @@ -17,11 +18,17 @@ import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.JsonNode
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.groups.OptionGroup
import com.github.ajalt.clikt.parameters.groups.groupChoice
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
import org.koin.core.KoinComponent
import org.koin.core.inject
import java.io.File
Expand All @@ -35,7 +42,31 @@ class ConfigCommand : CliktCommand(name = "config", help = "Initialize zoe") {

@ExperimentalCoroutinesApi
@FlowPreview
class ConfigInit : CliktCommand(name = "init", help = "Initialize zoe config"), KoinComponent {
class ConfigInit : CliktCommand(
name = "init",
help = "Initialize zoe config",
epilog = with(globalTermColors) {
"""```
|Examples:
|
| Init config with a default configuration file:
| > ${bold("zoe config init")}
|
| Load config from a local directory:
| > ${bold("""zoe config init --from local --path /path/to/existing/config""")}
|
| Load config from a git repository:
| > ${bold("""zoe config init --from git --url 'https://github.com/adevinta/zoe.git' --dir tutorials/simple/config""")}
|
| Load config from a git repository with authentication:
| > ${bold("""zoe config init --from git --url 'https://github.company.com/example/config.git' --dir zoe-config --username user --password pass""")}
|
| You can also use a github token as a username:
| > ${bold("""zoe config init --from git --url 'https://github.company.com/example/config.git' --dir zoe-config --username gh-token""")}
|
|```""".trimMargin()
}
), KoinComponent {

private val ctx by inject<CliContext>()

Expand All @@ -47,15 +78,14 @@ class ConfigInit : CliktCommand(name = "init", help = "Initialize zoe config"),
default = false
)

private val from: File? by option("--from", help = "Import from an existing configuration folder").file(
mustExist = true,
canBeDir = true,
mustBeReadable = true
private val from by option("--from", help = "Import from an existing configuration folder").groupChoice(
"local" to LoadFrom.Local(),
"git" to LoadFrom.Git()
)

override fun run() {
val configDir = ctx.configDir
val fromDir = from
val fromDir = from?.getSourceDir()

if (recreate && configDir.exists()) {
logger.info("deleting existing config directory : ${configDir.absolutePath}")
Expand Down Expand Up @@ -125,6 +155,45 @@ class ConfigInit : CliktCommand(name = "init", help = "Initialize zoe config"),
}
}

sealed class LoadFrom(name: String) : OptionGroup(name) {
class Local : LoadFrom("Options to load from local") {
val path: File
by option("--path")
.file(mustExist = true, canBeDir = true, mustBeReadable = true)
.required()
}

class Git : LoadFrom("Options to load from git") {
val url: String by option("--url", help = "remote url of the repository").required()
val dir: String by option("--dir", help = "path to the config inside the repo").default(".")
val username: String? by option("-u", "--username")
val password: String? by option("--password")
}
}

fun LoadFrom.getSourceDir(): File = when (this) {
is LoadFrom.Local -> path

is LoadFrom.Git -> {
val temp = Files.createTempDirectory("tmp-zoe-config-init").toFile().also { it.deleteOnExit() }

Git
.cloneRepository()
.setURI(url)
.let {
if (password != null || username != null) it.setCredentialsProvider(
UsernamePasswordCredentialsProvider(username ?: "", password ?: "")
) else {
it
}
}
.setDirectory(temp)
.call()

temp.resolve(dir)
}
}

@FlowPreview
@ExperimentalCoroutinesApi
fun configCommands() = ConfigCommand().subcommands(
Expand Down

0 comments on commit ffbaa32

Please sign in to comment.