Skip to content

Commit

Permalink
added import and export methods
Browse files Browse the repository at this point in the history
  • Loading branch information
musanmaz committed Jun 26, 2024
1 parent 82bd959 commit 4378bb0
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 62 deletions.
81 changes: 31 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,61 @@
# Consul Uploader CLI
# Consul IO CLI

Consul Uploader is a CLI tool used to upload configuration files from a specified directory to the Consul KV store.
Consul IO is a CLI tool used to import and export configuration files from a specified directory to/from the Consul KV store.

## Installation

You can download the latest release from the [Releases](https://github.com/turknet/consul-uploader/releases) page.

### Using the Binary

Download the binary for your platform from the releases page and add it to your PATH.

#### Example for Linux
You can install the latest version using the `go install` command:

```sh
wget https://github.com/turknet/consul-uploader/releases/download/v1.0.1/consul-uploader-linux-amd64
chmod +x consul-uploader-linux-amd64
sudo mv consul-uploader-linux-amd64 /usr/local/bin/consul-uploader
go install github.com/turknet/consul-io@latest
```

#### Example for macOS
## Check Version

```sh
wget https://github.com/turknet/consul-uploader/releases/download/v1.0.0/consul-uploader-darwin-amd64
chmod +x consul-uploader-darwin-amd64
sudo mv consul-uploader-darwin-amd64 /usr/local/bin/consul-uploader
consul-io version
```

#### Example for macOS
Download the binary from the releases page and add it to your PATH.


## Usage

You can run the CLI tool using the following command:

```sh
consul-uploader --consul-addr=http://localhost:8500 /path/to/config/directory
consul-io help
```
### Available Commands

- `import [directory]` : Upload config files to Consul KV store
- `export [directory]` : Download config files from Consul KV store
- `version` : Print the version number of Consul IO
- `help` : Display help for consul-io

### Installing Dependencies for Local Development

To install the dependencies for the project, run the following commands:
### Command Line Arguments

```sh
go get github.com/hashicorp/consul/api
go get github.com/spf13/cobra
go mod tidy
```
- `--consul-addr` : Specifies the address of the Consul server. The default value is `http://localhost:8500`.

## Usage for Local Development
- `[directory]` : The directory containing the configuration files you want to upload or the directory to which you want to export files.

You can run the CLI tool using the following command:
### Example Usage

#### Import
```sh
go run main.go --consul-addr=http://localhost:8500 /path/to/config/directory
consul-io --consul-addr=http://localhost:8500 import test
```
This command finds the files with the `.production` extension in the `test` directory and uploads them to the Consul KV store.

### Command Line Arguments

* `--consul-addr` : Specifies the address of the Consul server. The default value is `http://localhost:8500`.

* `/path/to/config/directory` : The directory containing the configuration files you want to upload.

## Example Usage

#### Export
```sh
go run main.go --consul-addr=http://localhost:8500 test
consul-io --consul-addr=http://localhost:8500 export test
```

This command finds the files with the `.production` extension in the `test` directory and uploads them to the Consul KV store.
This command downloads the configuration files from the Consul KV store and saves them in the `test` directory, maintaining the same structure.

## Example Directory Structure

```go
consul-uploader/
consul-io/
├── cmd/
│ └── root.go
├── test/
Expand All @@ -92,18 +73,18 @@ consul-uploader/
└── main.go
```

* `cmd/:` Contains the CLI command files.
- `cmd/` : Contains the CLI command files.

* `test/:` Example directory containing the configuration files you want to upload.
- `go.mod` : Contains Go module information.

* `go.mod:` Contains Go module information.
- `go.sum` : Contains the verification information for the Go modules.

* `go.sum:` Contains the verification information for the Go modules.
- `main.go` : The entry point of the program.

* `main.go:` The entry point of the program.
- `test/` : Example directory containing the configuration files you want to upload or the directory where you want to export files.

## Bağımlılıklar
## Dependencies

* [github.com/hashicorp/consul/api](https://github.com/hashicorp/consul/api) : Library used to interact with the Consul API.
- [github.com/hashicorp/consul/api](https://github.com/hashicorp/consul/api) : Library used to interact with the Consul API.

* [github.com/spf13/cobra](https://github.com/spf13/cobra) : Library used to create the command line interface.
- [github.com/spf13/cobra](https://github.com/spf13/cobra) : Library used to create the command line interface.
113 changes: 101 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,51 @@ import (
"github.com/spf13/cobra"
)

const version = "1.0.3"
const version = "1.0.7"

var consulAddr string
var directory string

var rootCmd = &cobra.Command{
Use: "consul-uploader [directory]",
Short: "Upload config files to Consul KV store",
Long: `Consul Uploader is a CLI tool used to upload configuration files from a specified directory to the Consul KV store.
Use: "consul-io",
Short: "Import and export config files to/from Consul KV store",
Long: `Consul IO is a CLI tool used to import and export configuration files from a specified directory to the Consul KV store and vice versa.
Available commands are:
- version: Print the version number of Consul Uploader`,
Args: cobra.MinimumNArgs(1),
- import: Upload config files to Consul KV store
- export: Download config files from Consul KV store`,
}

var importCmd = &cobra.Command{
Use: "import [directory]",
Short: "Import config files to Consul KV store",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
directory := args[0]
processDirectory(directory)
directory = args[0]
processDirectory(directory, uploadToConsul)
},
}

var exportCmd = &cobra.Command{
Use: "export [directory]",
Short: "Export config files from Consul KV store",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
directory = args[0]
exportFromConsul(directory)
},
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Consul Uploader",
Short: "Print the version number of Consul IO",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Consul Uploader v" + version)
fmt.Println("Consul IO v" + version)
},
}

func init() {
rootCmd.AddCommand(importCmd)
rootCmd.AddCommand(exportCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.PersistentFlags().StringVar(&consulAddr, "consul-addr", "http://localhost:8500", "Consul address")
}
Expand Down Expand Up @@ -73,7 +92,77 @@ func uploadToConsul(filePath, kvPath string) {
fmt.Printf("Uploaded %s to %s\n", filePath, kvPath)
}

func processDirectory(directory string) {
func downloadFromConsul(kvPath, filePath string) {
config := api.DefaultConfig()
config.Address = consulAddr
client, err := api.NewClient(config)
if err != nil {
fmt.Println("Error creating Consul client:", err)
return
}

kv := client.KV()
pair, _, err := kv.Get(kvPath, nil)
if err != nil {
fmt.Println("Error fetching from Consul:", err)
return
}

if pair == nil {
fmt.Printf("No data found at %s\n", kvPath)
return
}

err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
return
}

err = ioutil.WriteFile(filePath, pair.Value, 0644)
if err != nil {
fmt.Println("Error writing file:", err)
return
}

fmt.Printf("Downloaded %s to %s\n", kvPath, filePath)
}

func exportFromConsul(directory string) {
config := api.DefaultConfig()
config.Address = consulAddr
client, err := api.NewClient(config)
if err != nil {
fmt.Println("Error creating Consul client:", err)
return
}

kv := client.KV()
pairs, _, err := kv.List("/", nil)
if err != nil {
fmt.Println("Error fetching keys from Consul:", err)
return
}

for _, pair := range pairs {
filePath := filepath.Join(directory, pair.Key)
err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
continue
}

err = ioutil.WriteFile(filePath, pair.Value, 0644)
if err != nil {
fmt.Println("Error writing file:", err)
continue
}

fmt.Printf("Downloaded %s to %s\n", pair.Key, filePath)
}
}

func processDirectory(directory string, processFunc func(string, string)) {
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -84,7 +173,7 @@ func processDirectory(directory string) {
if err != nil {
return err
}
uploadToConsul(path, kvPath)
processFunc(path, kvPath)
}

return nil
Expand Down

0 comments on commit 4378bb0

Please sign in to comment.