Skip to content

Commit

Permalink
added colored log output
Browse files Browse the repository at this point in the history
  • Loading branch information
musanmaz committed Aug 13, 2024
1 parent 4568100 commit 369d177
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
2 changes: 2 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/turknet/consul-io/internal/consul"
)
Expand All @@ -12,6 +13,7 @@ var exportCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
directory := args[0]
consul.ExportFromConsul(directory, consulAddr)
color.Green("Export process completed successfully.")
},
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sync"
"time"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/turknet/consul-io/internal/consul"
"github.com/turknet/consul-io/internal/file"
Expand All @@ -27,6 +28,7 @@ var importCmd = &cobra.Command{
file.ProcessDirectory(directory, consulAddr, rateLimit, retryLimit, ignorePaths, sem, &wg, ticker, consul.UploadToConsul)
wg.Wait()
close(sem)
color.Green("Import process completed successfully.")
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cmd

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
)

const version = "1.2.0"
const version = "1.2.2"

var (
consulAddr string
Expand All @@ -27,7 +27,7 @@ Available commands are:

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
color.Red("Error: %v", err)
os.Exit(1)
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (

require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
Expand Down
36 changes: 19 additions & 17 deletions internal/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/fatih/color"
"github.com/hashicorp/consul/api"
"github.com/turknet/consul-io/internal/file"
)
Expand Down Expand Up @@ -53,15 +54,16 @@ func CheckForSensitiveData(filePath string, content string) {
if len(problems) > 0 {
f, err := os.OpenFile("problems.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening problems.txt:", err)
color.Red("Error opening problems.txt: %v", err)
return
}
defer f.Close()

for _, problem := range problems {
if _, err := f.WriteString(problem + "\n"); err != nil {
fmt.Println("Error writing to problems.txt:", err)
color.Red("Error writing to problems.txt: %v", err)
}
color.Yellow(problem)
}
}
}
Expand All @@ -70,34 +72,34 @@ func UploadToConsul(filePath, kvPath, consulAddr string, retryLimit, rateLimit i
defer wg.Done()
client, err := getClient(consulAddr)
if err != nil {
fmt.Println(err)
color.Red("Error: %v", err)
<-sem
return
}

consulValue, err := GetKV(client, kvPath, retryLimit, rateLimit, ticker)
if err != nil {
fmt.Println("Error getting KV from Consul:", err)
color.Red("Error getting KV from Consul: %v", err)
<-sem
return
}

isSame, err := file.CompareFiles(filePath, consulValue)
if err != nil {
fmt.Println("Error comparing files:", err)
color.Red("Error comparing files: %v", err)
<-sem
return
}

if isSame {
fmt.Printf("No changes detected for file: %s\n", filePath)
color.Green("No changes detected for file: %s", filePath)
<-sem
return
}

data, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading file:", err)
color.Red("Error reading file: %v", err)
<-sem
return
}
Expand All @@ -112,32 +114,32 @@ func UploadToConsul(filePath, kvPath, consulAddr string, retryLimit, rateLimit i
if err == nil {
break
}
fmt.Println("Error uploading to Consul, retrying:", err)
color.Yellow("Error uploading to Consul, retrying: %v", err)
time.Sleep(time.Duration(rateLimit) * time.Millisecond)
}

if err != nil {
fmt.Println("Error uploading to Consul:", err)
color.Red("Error uploading to Consul: %v", err)
<-sem
return
}

fmt.Printf("Uploaded %s to %s\n", filePath, kvPath)
color.Green("Uploaded %s to %s", filePath, kvPath)
time.Sleep(time.Duration(rateLimit) * time.Millisecond)
<-sem
}

func ExportFromConsul(directory, consulAddr string) {
client, err := getClient(consulAddr)
if err != nil {
fmt.Println(err)
color.Red("Error: %v", err)
return
}

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

Expand All @@ -146,26 +148,26 @@ func ExportFromConsul(directory, consulAddr string) {
dirPath := filepath.Join(directory, pair.Key)
err = os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
color.Red("Error creating directory: %v", err)
continue
}
fmt.Printf("Downloaded %s to %s\n", pair.Key, dirPath)
color.Green("Downloaded %s to %s", pair.Key, dirPath)
} else {
filePath := filepath.Join(directory, pair.Key)
err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
color.Red("Error creating directory: %v", err)
continue
}
err = ioutil.WriteFile(filePath, pair.Value, 0644)
if err != nil {
fmt.Println("Error writing file:", err)
color.Red("Error writing file: %v", err)
continue
}

CheckForSensitiveData(filePath, string(pair.Value))

fmt.Printf("Downloaded %s to %s\n", pair.Key, filePath)
color.Green("Downloaded %s to %s", pair.Key, filePath)
}
}
}
1 change: 0 additions & 1 deletion internal/file/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func CompareFiles(filePath string, consulValue string) (bool, error) {
return false, fmt.Errorf("failed to read file: %v", err)
}

// Hesaplanan hash değerlerini karşılaştırma
fileHash := md5.Sum(content)
consulHash := md5.Sum([]byte(consulValue))

Expand Down

0 comments on commit 369d177

Please sign in to comment.