Skip to content

Commit

Permalink
Refactoring cmd in download and upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Otávio Fernandes committed Mar 21, 2019
1 parent f5c0d88 commit bf0cb94
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 40 deletions.
43 changes: 43 additions & 0 deletions cmd/vault-handler/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"log"

vaulthandler "github.com/otaviof/vault-handler/pkg/vault-handler"
"github.com/spf13/cobra"
)

var downloadCmd = &cobra.Command{
Use: "download",
Run: runDownloadCmd,
Short: "",
Long: ``,
}

// runDownloadCmd execute the download of secrets from Vault.
func runDownloadCmd(cmd *cobra.Command, args []string) {
var manifest *vaulthandler.Manifest
var err error

handler := bootstrap()

for _, manifestFile := range args {
log.Printf("Handling manifest file: '%s'", manifestFile)

if manifest, err = vaulthandler.NewManifest(manifestFile); err != nil {
log.Fatalf("[ERROR] On parsing manifest: '%s'", err)
}

if err = handler.Download(manifest); err != nil {
log.Fatalf("[ERROR] During realization of manifest: '%s'", err)
}
}
}

func init() {
flags := downloadCmd.PersistentFlags()

flags.String("output-dir", ".", "Output directory.")

rootCmd.AddCommand(downloadCmd)
}
24 changes: 24 additions & 0 deletions cmd/vault-handler/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/spf13/cobra"
)

var uploadCmd = &cobra.Command{
Use: "upload",
Run: runUploadCmd,
Short: "",
Long: ``,
}

func runUploadCmd(cmd *cobra.Command, args []string) {

}

func init() {
flags := uploadCmd.PersistentFlags()

flags.String("input-dir", ".", "Input directory.")

rootCmd.AddCommand(uploadCmd)
}
72 changes: 32 additions & 40 deletions cmd/vault-handler/vault_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,34 @@ import (
"log"
"strings"

"github.com/spf13/viper"

"github.com/spf13/cobra"

vaulthandler "github.com/otaviof/vault-handler/pkg/vault-handler"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var rootCmd = &cobra.Command{
Use: "vault-handler",
Run: runVaultHandlerCmd,
Short: "",
Long: ``,
}

// init command-line flags and configuration coming from environment.
func init() {
var err error

flags := rootCmd.PersistentFlags()

// setting up rules for environment variables
viper.SetEnvPrefix("vault-handler")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// command-line flags
flags.String("output-dir", ".", "Output directory.")
flags.String("vault-addr", "http://127.0.0.1:8200", "Vault address")
flags.String("vault-token", "", "Vault access token")
flags.String("vault-role-id", "", "Vault AppRole role-id")
flags.String("vault-secret-id", "", "Vault AppRole secret-id")
flags.Bool("dry-run", false, "dry-run mode")

if err = viper.BindPFlags(flags); err != nil {
log.Fatal(err)
}
}

// configFromEnv creates a configuration object using Viper, which brings overwritten values from
// environment variables.
func configFromEnv() *vaulthandler.Config {
return &vaulthandler.Config{
DryRun: viper.GetBool("dry-run"),
OutputDir: viper.GetString("output-dir"),
InputDir: viper.GetString("input-dir"),
VaultAddr: viper.GetString("vault-addr"),
VaultToken: viper.GetString("vault-token"),
VaultRoleID: viper.GetString("vault-role-id"),
VaultSecretID: viper.GetString("vault-secret-id"),
}
}

// runVaultHandlerCmd execute the primary objective of this app, to realize the manifest files.
func runVaultHandlerCmd(cmd *cobra.Command, args []string) {
// bootstrap creates connection with vault, by instantiating Handler.
func bootstrap() *vaulthandler.Handler {
var handler *vaulthandler.Handler
var manifest *vaulthandler.Manifest
var err error

config := configFromEnv()
Expand All @@ -70,20 +43,39 @@ func runVaultHandlerCmd(cmd *cobra.Command, args []string) {
log.Fatalf("[ERROR] On instantiating Vault-API: '%s'", err)
}

for _, manifestFile := range args {
log.Printf("Handling manifest file: '%s'", manifestFile)
if err = handler.Authenticate(); err != nil {
log.Fatalf("[ERROR] On authenticating agasint Vault: '%s'", err)
}

return handler
}

// init command-line flags and configuration coming from environment.
func init() {
var err error

flags := rootCmd.PersistentFlags()

// setting up rules for environment variables
viper.SetEnvPrefix("vault-handler")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// command-line flags
flags.String("vault-addr", "http://127.0.0.1:8200", "Vault address")
flags.String("vault-token", "", "Vault access token")
flags.String("vault-role-id", "", "Vault AppRole role-id")
flags.String("vault-secret-id", "", "Vault AppRole secret-id")
flags.Bool("dry-run", false, "dry-run mode")

if manifest, err = vaulthandler.NewManifest(manifestFile); err != nil {
log.Fatalf("[ERROR] On parsing manifest: '%s'", err)
}
if err = handler.Run(manifest); err != nil {
log.Fatalf("[ERROR] During realization of manifest: '%s'", err)
}
if err = viper.BindPFlags(flags); err != nil {
log.Fatal(err)
}
}

func main() {
var err error

if err = rootCmd.Execute(); err != nil {
log.Fatalf("[MAIN] %s", err)
}
Expand Down

0 comments on commit bf0cb94

Please sign in to comment.